blob: 345e889ba6d2139fc7c56e6148517d600f0f87cb [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010065#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020066#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020070#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010071#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020072#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010073#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020075#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau522d6c02009-12-06 18:49:18 +010078const char HTTP_100[] =
79 "HTTP/1.1 100 Continue\r\n\r\n";
80
81const struct chunk http_100_chunk = {
82 .str = (char *)&HTTP_100,
83 .len = sizeof(HTTP_100)-1
84};
85
Willy Tarreaua9679ac2010-01-03 17:32:57 +010086/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400105
106/* same as 302 except that the browser MUST retry with the same method */
107const char *HTTP_307 =
108 "HTTP/1.1 307 Temporary Redirect\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Content-length: 0\r\n"
111 "Location: "; /* not terminated since it will be concatenated with the URL */
112
113/* same as 301 except that the browser MUST retry with the same method */
114const char *HTTP_308 =
115 "HTTP/1.1 308 Permanent Redirect\r\n"
116 "Content-length: 0\r\n"
117 "Location: "; /* not terminated since it will be concatenated with the URL */
118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
120const char *HTTP_401_fmt =
121 "HTTP/1.0 401 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200124 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129const char *HTTP_407_fmt =
130 "HTTP/1.0 407 Unauthorized\r\n"
131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
135 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800136 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100137
Willy Tarreau0f772532006-12-23 20:51:41 +0100138
139const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_400] = 400,
142 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400143 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100144 [HTTP_ERR_408] = 408,
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200145 [HTTP_ERR_421] = 421,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200146 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400147 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100148 [HTTP_ERR_500] = 500,
149 [HTTP_ERR_502] = 502,
150 [HTTP_ERR_503] = 503,
151 [HTTP_ERR_504] = 504,
152};
153
Willy Tarreau80587432006-12-24 17:47:20 +0100154static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200155 [HTTP_ERR_200] =
156 "HTTP/1.0 200 OK\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
162
Willy Tarreau0f772532006-12-23 20:51:41 +0100163 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100164 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
170
171 [HTTP_ERR_403] =
172 "HTTP/1.0 403 Forbidden\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
178
CJ Ess108b1dd2015-04-07 12:03:37 -0400179 [HTTP_ERR_405] =
180 "HTTP/1.0 405 Method Not Allowed\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>405 Method Not Allowed</h1>\nA request was made of a resource using a request method not supported by that resource\n</body></html>\n",
186
Willy Tarreau0f772532006-12-23 20:51:41 +0100187 [HTTP_ERR_408] =
188 "HTTP/1.0 408 Request Time-out\r\n"
189 "Cache-Control: no-cache\r\n"
190 "Connection: close\r\n"
191 "Content-Type: text/html\r\n"
192 "\r\n"
193 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
194
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200195 [HTTP_ERR_421] =
196 "HTTP/1.0 421 Misdirected Request\r\n"
197 "Cache-Control: no-cache\r\n"
198 "Connection: close\r\n"
199 "Content-Type: text/html\r\n"
200 "\r\n"
201 "<html><body><h1>421 Misdirected Request</h1>\nRequest sent to a non-authoritative server.\n</body></html>\n",
202
Olivier Houchard51a76d82017-10-02 16:12:07 +0200203 [HTTP_ERR_425] =
204 "HTTP/1.0 425 Too Early\r\n"
205 "Cache-Control: no-cache\r\n"
206 "Connection: close\r\n"
207 "Content-Type: text/html\r\n"
208 "\r\n"
209 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
210
CJ Ess108b1dd2015-04-07 12:03:37 -0400211 [HTTP_ERR_429] =
212 "HTTP/1.0 429 Too Many Requests\r\n"
213 "Cache-Control: no-cache\r\n"
214 "Connection: close\r\n"
215 "Content-Type: text/html\r\n"
216 "\r\n"
217 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
218
Willy Tarreau0f772532006-12-23 20:51:41 +0100219 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200220 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100221 "Cache-Control: no-cache\r\n"
222 "Connection: close\r\n"
223 "Content-Type: text/html\r\n"
224 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200225 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100226
227 [HTTP_ERR_502] =
228 "HTTP/1.0 502 Bad Gateway\r\n"
229 "Cache-Control: no-cache\r\n"
230 "Connection: close\r\n"
231 "Content-Type: text/html\r\n"
232 "\r\n"
233 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
234
235 [HTTP_ERR_503] =
236 "HTTP/1.0 503 Service Unavailable\r\n"
237 "Cache-Control: no-cache\r\n"
238 "Connection: close\r\n"
239 "Content-Type: text/html\r\n"
240 "\r\n"
241 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
242
243 [HTTP_ERR_504] =
244 "HTTP/1.0 504 Gateway Time-out\r\n"
245 "Cache-Control: no-cache\r\n"
246 "Connection: close\r\n"
247 "Content-Type: text/html\r\n"
248 "\r\n"
249 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
250
251};
252
Cyril Bonté19979e12012-04-04 12:57:21 +0200253/* status codes available for the stats admin page (strictly 4 chars length) */
254const char *stat_status_codes[STAT_STATUS_SIZE] = {
255 [STAT_STATUS_DENY] = "DENY",
256 [STAT_STATUS_DONE] = "DONE",
257 [STAT_STATUS_ERRP] = "ERRP",
258 [STAT_STATUS_EXCD] = "EXCD",
259 [STAT_STATUS_NONE] = "NONE",
260 [STAT_STATUS_PART] = "PART",
261 [STAT_STATUS_UNKN] = "UNKN",
262};
263
264
William Lallemand73025dd2014-04-24 14:38:37 +0200265/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200266struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200267 .list = LIST_HEAD_INIT(http_req_keywords.list)
268};
269
270/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200271struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200272 .list = LIST_HEAD_INIT(http_res_keywords.list)
273};
274
Willy Tarreau80587432006-12-24 17:47:20 +0100275/* We must put the messages here since GCC cannot initialize consts depending
276 * on strlen().
277 */
278struct chunk http_err_chunks[HTTP_ERR_SIZE];
279
Willy Tarreaua890d072013-04-02 12:01:06 +0200280/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100281static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200282
Willy Tarreau42250582007-04-01 01:30:43 +0200283#define FD_SETS_ARE_BITFIELDS
284#ifdef FD_SETS_ARE_BITFIELDS
285/*
286 * This map is used with all the FD_* macros to check whether a particular bit
287 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
288 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
289 * byte should be encoded. Be careful to always pass bytes from 0 to 255
290 * exclusively to the macros.
291 */
292fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
293fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100294fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200295
296#else
297#error "Check if your OS uses bitfields for fd_sets"
298#endif
299
Willy Tarreau87b09662015-04-03 00:22:06 +0200300static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200301
David Carlier7365f7d2016-04-04 11:54:42 +0100302static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
303static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100304
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200305/* This function returns a reason associated with the HTTP status.
306 * This function never fails, a message is always returned.
307 */
308const char *get_reason(unsigned int status)
309{
310 switch (status) {
311 case 100: return "Continue";
312 case 101: return "Switching Protocols";
313 case 102: return "Processing";
314 case 200: return "OK";
315 case 201: return "Created";
316 case 202: return "Accepted";
317 case 203: return "Non-Authoritative Information";
318 case 204: return "No Content";
319 case 205: return "Reset Content";
320 case 206: return "Partial Content";
321 case 207: return "Multi-Status";
322 case 210: return "Content Different";
323 case 226: return "IM Used";
324 case 300: return "Multiple Choices";
325 case 301: return "Moved Permanently";
326 case 302: return "Moved Temporarily";
327 case 303: return "See Other";
328 case 304: return "Not Modified";
329 case 305: return "Use Proxy";
330 case 307: return "Temporary Redirect";
331 case 308: return "Permanent Redirect";
332 case 310: return "Too many Redirects";
333 case 400: return "Bad Request";
334 case 401: return "Unauthorized";
335 case 402: return "Payment Required";
336 case 403: return "Forbidden";
337 case 404: return "Not Found";
338 case 405: return "Method Not Allowed";
339 case 406: return "Not Acceptable";
340 case 407: return "Proxy Authentication Required";
341 case 408: return "Request Time-out";
342 case 409: return "Conflict";
343 case 410: return "Gone";
344 case 411: return "Length Required";
345 case 412: return "Precondition Failed";
346 case 413: return "Request Entity Too Large";
347 case 414: return "Request-URI Too Long";
348 case 415: return "Unsupported Media Type";
349 case 416: return "Requested range unsatisfiable";
350 case 417: return "Expectation failed";
351 case 418: return "I'm a teapot";
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200352 case 421: return "Misdirected Request";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200353 case 422: return "Unprocessable entity";
354 case 423: return "Locked";
355 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200356 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200357 case 426: return "Upgrade Required";
358 case 428: return "Precondition Required";
359 case 429: return "Too Many Requests";
360 case 431: return "Request Header Fields Too Large";
361 case 449: return "Retry With";
362 case 450: return "Blocked by Windows Parental Controls";
363 case 451: return "Unavailable For Legal Reasons";
364 case 456: return "Unrecoverable Error";
365 case 499: return "client has closed connection";
366 case 500: return "Internal Server Error";
367 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200368 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200369 case 503: return "Service Unavailable";
370 case 504: return "Gateway Time-out";
371 case 505: return "HTTP Version not supported";
372 case 506: return "Variant also negociate";
373 case 507: return "Insufficient storage";
374 case 508: return "Loop detected";
375 case 509: return "Bandwidth Limit Exceeded";
376 case 510: return "Not extended";
377 case 511: return "Network authentication required";
378 case 520: return "Web server is returning an unknown error";
379 default:
380 switch (status) {
381 case 100 ... 199: return "Informational";
382 case 200 ... 299: return "Success";
383 case 300 ... 399: return "Redirection";
384 case 400 ... 499: return "Client Error";
385 case 500 ... 599: return "Server Error";
386 default: return "Other";
387 }
388 }
389}
390
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200391/* This function returns HTTP_ERR_<num> (enum) matching http status code.
392 * Returned value should match codes from http_err_codes.
393 */
394static const int http_get_status_idx(unsigned int status)
395{
396 switch (status) {
397 case 200: return HTTP_ERR_200;
398 case 400: return HTTP_ERR_400;
399 case 403: return HTTP_ERR_403;
400 case 405: return HTTP_ERR_405;
401 case 408: return HTTP_ERR_408;
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200402 case 421: return HTTP_ERR_421;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200403 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200404 case 429: return HTTP_ERR_429;
405 case 500: return HTTP_ERR_500;
406 case 502: return HTTP_ERR_502;
407 case 503: return HTTP_ERR_503;
408 case 504: return HTTP_ERR_504;
409 default: return HTTP_ERR_500;
410 }
411}
412
Willy Tarreau80587432006-12-24 17:47:20 +0100413void init_proto_http()
414{
Willy Tarreau42250582007-04-01 01:30:43 +0200415 int i;
416 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100417 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200418
Willy Tarreau80587432006-12-24 17:47:20 +0100419 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
420 if (!http_err_msgs[msg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100421 ha_alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
Willy Tarreau80587432006-12-24 17:47:20 +0100422 abort();
423 }
424
425 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
426 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
427 }
Willy Tarreau42250582007-04-01 01:30:43 +0200428
429 /* initialize the log header encoding map : '{|}"#' should be encoded with
430 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
431 * URL encoding only requires '"', '#' to be encoded as well as non-
432 * printable characters above.
433 */
434 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
435 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100436 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200437 for (i = 0; i < 32; i++) {
438 FD_SET(i, hdr_encode_map);
439 FD_SET(i, url_encode_map);
440 }
441 for (i = 127; i < 256; i++) {
442 FD_SET(i, hdr_encode_map);
443 FD_SET(i, url_encode_map);
444 }
445
446 tmp = "\"#{|}";
447 while (*tmp) {
448 FD_SET(*tmp, hdr_encode_map);
449 tmp++;
450 }
451
452 tmp = "\"#";
453 while (*tmp) {
454 FD_SET(*tmp, url_encode_map);
455 tmp++;
456 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200457
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100458 /* initialize the http header encoding map. The draft httpbis define the
459 * header content as:
460 *
461 * HTTP-message = start-line
462 * *( header-field CRLF )
463 * CRLF
464 * [ message-body ]
465 * header-field = field-name ":" OWS field-value OWS
466 * field-value = *( field-content / obs-fold )
467 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
468 * obs-fold = CRLF 1*( SP / HTAB )
469 * field-vchar = VCHAR / obs-text
470 * VCHAR = %x21-7E
471 * obs-text = %x80-FF
472 *
473 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
474 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
475 * "obs-fold" is volontary forgotten because haproxy remove this.
476 */
477 memset(http_encode_map, 0, sizeof(http_encode_map));
478 for (i = 0x00; i <= 0x08; i++)
479 FD_SET(i, http_encode_map);
480 for (i = 0x0a; i <= 0x1f; i++)
481 FD_SET(i, http_encode_map);
482 FD_SET(0x7f, http_encode_map);
483
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200484 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
486 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100487}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488
Willy Tarreau53b6c742006-12-17 13:37:46 +0100489/*
490 * We have 26 list of methods (1 per first letter), each of which can have
491 * up to 3 entries (2 valid, 1 null).
492 */
493struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100494 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100495 int len;
496 const char text[8];
497};
498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100499const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100500 ['C' - 'A'] = {
501 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
502 },
503 ['D' - 'A'] = {
504 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
505 },
506 ['G' - 'A'] = {
507 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
508 },
509 ['H' - 'A'] = {
510 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
511 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200512 ['O' - 'A'] = {
513 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
514 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100515 ['P' - 'A'] = {
516 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
517 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
518 },
519 ['T' - 'A'] = {
520 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
521 },
522 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200523 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100524 */
525};
526
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100527const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100528 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
529 [HTTP_METH_GET] = { "GET", 3 },
530 [HTTP_METH_HEAD] = { "HEAD", 4 },
531 [HTTP_METH_POST] = { "POST", 4 },
532 [HTTP_METH_PUT] = { "PUT", 3 },
533 [HTTP_METH_DELETE] = { "DELETE", 6 },
534 [HTTP_METH_TRACE] = { "TRACE", 5 },
535 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
536};
537
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100538/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100539 * Adds a header and its CRLF at the tail of the message's buffer, just before
540 * the last CRLF. Text length is measured first, so it cannot be NULL.
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 Tarreau6acf7c92012-03-09 13:30:45 +0100545int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100546{
547 int bytes, len;
548
549 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200550 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100551 if (!bytes)
552 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100553 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100554 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
555}
556
557/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100558 * Adds a header and its CRLF at the tail of the message's buffer, just before
559 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100560 * the buffer is only opened and the space reserved, but nothing is copied.
561 * The header is also automatically added to the index <hdr_idx>, and the end
562 * of headers is automatically adjusted. The number of bytes added is returned
563 * on success, otherwise <0 is returned indicating an error.
564 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100565int http_header_add_tail2(struct http_msg *msg,
566 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100567{
568 int bytes;
569
Willy Tarreau9b28e032012-10-12 23:49:43 +0200570 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100571 if (!bytes)
572 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100573 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100574 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
575}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576
577/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100578 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
579 * If so, returns the position of the first non-space character relative to
580 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
581 * to return a pointer to the place after the first space. Returns 0 if the
582 * header name does not match. Checks are case-insensitive.
583 */
584int http_header_match2(const char *hdr, const char *end,
585 const char *name, int len)
586{
587 const char *val;
588
589 if (hdr + len >= end)
590 return 0;
591 if (hdr[len] != ':')
592 return 0;
593 if (strncasecmp(hdr, name, len) != 0)
594 return 0;
595 val = hdr + len + 1;
596 while (val < end && HTTP_IS_SPHT(*val))
597 val++;
598 if ((val >= end) && (len + 2 <= end - hdr))
599 return len + 2; /* we may replace starting from second space */
600 return val - hdr;
601}
602
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200603/* Find the first or next occurrence of header <name> in message buffer <sol>
604 * using headers index <idx>, and return it in the <ctx> structure. This
605 * structure holds everything necessary to use the header and find next
606 * occurrence. If its <idx> member is 0, the header is searched from the
607 * beginning. Otherwise, the next occurrence is returned. The function returns
608 * 1 when it finds a value, and 0 when there is no more. It is very similar to
609 * http_find_header2() except that it is designed to work with full-line headers
610 * whose comma is not a delimiter but is part of the syntax. As a special case,
611 * if ctx->val is NULL when searching for a new values of a header, the current
612 * header is rescanned. This allows rescanning after a header deletion.
613 */
614int http_find_full_header2(const char *name, int len,
615 char *sol, struct hdr_idx *idx,
616 struct hdr_ctx *ctx)
617{
618 char *eol, *sov;
619 int cur_idx, old_idx;
620
621 cur_idx = ctx->idx;
622 if (cur_idx) {
623 /* We have previously returned a header, let's search another one */
624 sol = ctx->line;
625 eol = sol + idx->v[cur_idx].len;
626 goto next_hdr;
627 }
628
629 /* first request for this header */
630 sol += hdr_idx_first_pos(idx);
631 old_idx = 0;
632 cur_idx = hdr_idx_first_idx(idx);
633 while (cur_idx) {
634 eol = sol + idx->v[cur_idx].len;
635
636 if (len == 0) {
637 /* No argument was passed, we want any header.
638 * To achieve this, we simply build a fake request. */
639 while (sol + len < eol && sol[len] != ':')
640 len++;
641 name = sol;
642 }
643
644 if ((len < eol - sol) &&
645 (sol[len] == ':') &&
646 (strncasecmp(sol, name, len) == 0)) {
647 ctx->del = len;
648 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100649 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200650 sov++;
651
652 ctx->line = sol;
653 ctx->prev = old_idx;
654 ctx->idx = cur_idx;
655 ctx->val = sov - sol;
656 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100657 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200658 eol--;
659 ctx->tws++;
660 }
661 ctx->vlen = eol - sov;
662 return 1;
663 }
664 next_hdr:
665 sol = eol + idx->v[cur_idx].cr + 1;
666 old_idx = cur_idx;
667 cur_idx = idx->v[cur_idx].next;
668 }
669 return 0;
670}
671
Willy Tarreauc90dc232015-02-20 13:51:36 +0100672/* Find the first or next header field in message buffer <sol> using headers
673 * index <idx>, and return it in the <ctx> structure. This structure holds
674 * everything necessary to use the header and find next occurrence. If its
675 * <idx> member is 0, the first header is retrieved. Otherwise, the next
676 * occurrence is returned. The function returns 1 when it finds a value, and
677 * 0 when there is no more. It is equivalent to http_find_full_header2() with
678 * no header name.
679 */
680int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
681{
682 char *eol, *sov;
683 int cur_idx, old_idx;
684 int len;
685
686 cur_idx = ctx->idx;
687 if (cur_idx) {
688 /* We have previously returned a header, let's search another one */
689 sol = ctx->line;
690 eol = sol + idx->v[cur_idx].len;
691 goto next_hdr;
692 }
693
694 /* first request for this header */
695 sol += hdr_idx_first_pos(idx);
696 old_idx = 0;
697 cur_idx = hdr_idx_first_idx(idx);
698 while (cur_idx) {
699 eol = sol + idx->v[cur_idx].len;
700
701 len = 0;
702 while (1) {
703 if (len >= eol - sol)
704 goto next_hdr;
705 if (sol[len] == ':')
706 break;
707 len++;
708 }
709
710 ctx->del = len;
711 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100712 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100713 sov++;
714
715 ctx->line = sol;
716 ctx->prev = old_idx;
717 ctx->idx = cur_idx;
718 ctx->val = sov - sol;
719 ctx->tws = 0;
720
Willy Tarreau2235b262016-11-05 15:50:20 +0100721 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100722 eol--;
723 ctx->tws++;
724 }
725 ctx->vlen = eol - sov;
726 return 1;
727
728 next_hdr:
729 sol = eol + idx->v[cur_idx].cr + 1;
730 old_idx = cur_idx;
731 cur_idx = idx->v[cur_idx].next;
732 }
733 return 0;
734}
735
Lukas Tribus23953682017-04-28 13:24:30 +0000736/* Find the end of the header value contained between <s> and <e>. See RFC7230,
737 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100738 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100740char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200741{
742 int quoted, qdpair;
743
744 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100745
746#if defined(__x86_64__) || \
747 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
748 defined(__ARM_ARCH_7A__)
749 /* speedup: skip everything not a comma nor a double quote */
750 for (; s <= e - sizeof(int); s += sizeof(int)) {
751 unsigned int c = *(int *)s; // comma
752 unsigned int q = c; // quote
753
754 c ^= 0x2c2c2c2c; // contains one zero on a comma
755 q ^= 0x22222222; // contains one zero on a quote
756
757 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
758 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
759
760 if ((c | q) & 0x80808080)
761 break; // found a comma or a quote
762 }
763#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 for (; s < e; s++) {
765 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200766 else if (quoted) {
767 if (*s == '\\') qdpair = 1;
768 else if (*s == '"') quoted = 0;
769 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200770 else if (*s == '"') quoted = 1;
771 else if (*s == ',') return s;
772 }
773 return s;
774}
775
776/* Find the first or next occurrence of header <name> in message buffer <sol>
777 * using headers index <idx>, and return it in the <ctx> structure. This
778 * structure holds everything necessary to use the header and find next
779 * occurrence. If its <idx> member is 0, the header is searched from the
780 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100781 * 1 when it finds a value, and 0 when there is no more. It is designed to work
782 * with headers defined as comma-separated lists. As a special case, if ctx->val
783 * is NULL when searching for a new values of a header, the current header is
784 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200785 */
786int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100787 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200788 struct hdr_ctx *ctx)
789{
Willy Tarreau68085d82010-01-18 14:54:04 +0100790 char *eol, *sov;
791 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200792
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 cur_idx = ctx->idx;
794 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200795 /* We have previously returned a value, let's search
796 * another one on the same line.
797 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200799 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100800 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 eol = sol + idx->v[cur_idx].len;
802
803 if (sov >= eol)
804 /* no more values in this header */
805 goto next_hdr;
806
Willy Tarreau68085d82010-01-18 14:54:04 +0100807 /* values remaining for this header, skip the comma but save it
808 * for later use (eg: for header deletion).
809 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200810 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100811 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200812 sov++;
813
814 goto return_hdr;
815 }
816
817 /* first request for this header */
818 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100819 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200820 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200821 while (cur_idx) {
822 eol = sol + idx->v[cur_idx].len;
823
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200824 if (len == 0) {
825 /* No argument was passed, we want any header.
826 * To achieve this, we simply build a fake request. */
827 while (sol + len < eol && sol[len] != ':')
828 len++;
829 name = sol;
830 }
831
Willy Tarreau33a7e692007-06-10 19:45:56 +0200832 if ((len < eol - sol) &&
833 (sol[len] == ':') &&
834 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100835 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200836 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100837 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200838 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100839
Willy Tarreau33a7e692007-06-10 19:45:56 +0200840 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100841 ctx->prev = old_idx;
842 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200843 ctx->idx = cur_idx;
844 ctx->val = sov - sol;
845
846 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200847 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100848 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200849 eol--;
850 ctx->tws++;
851 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200852 ctx->vlen = eol - sov;
853 return 1;
854 }
855 next_hdr:
856 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100857 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858 cur_idx = idx->v[cur_idx].next;
859 }
860 return 0;
861}
862
863int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100864 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200865 struct hdr_ctx *ctx)
866{
867 return http_find_header2(name, strlen(name), sol, idx, ctx);
868}
869
Willy Tarreau68085d82010-01-18 14:54:04 +0100870/* Remove one value of a header. This only works on a <ctx> returned by one of
871 * the http_find_header functions. The value is removed, as well as surrounding
872 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100873 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100874 * message <msg>. The new index is returned. If it is zero, it means there is
875 * no more header, so any processing may stop. The ctx is always left in a form
876 * that can be handled by http_find_header2() to find next occurrence.
877 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100878int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100879{
880 int cur_idx = ctx->idx;
881 char *sol = ctx->line;
882 struct hdr_idx_elem *hdr;
883 int delta, skip_comma;
884
885 if (!cur_idx)
886 return 0;
887
888 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200889 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100890 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200891 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100892 http_msg_move_end(msg, delta);
893 idx->used--;
894 hdr->len = 0; /* unused entry */
895 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100896 if (idx->tail == ctx->idx)
897 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100898 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100899 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100900 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200901 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100902 return ctx->idx;
903 }
904
905 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200906 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
907 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100908 */
909
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200910 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200911 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200912 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100913 NULL, 0);
914 hdr->len += delta;
915 http_msg_move_end(msg, delta);
916 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200917 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100918 return ctx->idx;
919}
920
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100921/* This function handles a server error at the stream interface level. The
922 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100923 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100924 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100925 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200926 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200927static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100928 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929{
Willy Tarreau2019f952017-03-14 11:07:31 +0100930 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100931 channel_auto_read(si_oc(si));
932 channel_abort(si_oc(si));
933 channel_auto_close(si_oc(si));
934 channel_erase(si_oc(si));
935 channel_auto_close(si_ic(si));
936 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200937 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200938 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200939 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200940 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200941 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200942 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943}
944
Willy Tarreau87b09662015-04-03 00:22:06 +0200945/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100946 * and message.
947 */
948
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200949struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100950{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200951 const int msgnum = http_get_status_idx(s->txn->status);
952
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200953 if (s->be->errmsg[msgnum].str)
954 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200955 else if (strm_fe(s)->errmsg[msgnum].str)
956 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100957 else
958 return &http_err_chunks[msgnum];
959}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200960
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100961void
962http_reply_and_close(struct stream *s, short status, struct chunk *msg)
963{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200964 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100965 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100966 stream_int_retnclose(&s->si[0], msg);
967}
968
Willy Tarreau53b6c742006-12-17 13:37:46 +0100969/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200970 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
971 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100973enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974{
975 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100976 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977
978 m = ((unsigned)*str - 'A');
979
980 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100981 for (h = http_methods[m]; h->len > 0; h++) {
982 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100983 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100984 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100985 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100986 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100987 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200988 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100989}
990
Willy Tarreau21d2af32008-02-14 20:25:24 +0100991/* Parse the URI from the given transaction (which is assumed to be in request
992 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
993 * It is returned otherwise.
994 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200995char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100996{
997 char *ptr, *end;
998
Willy Tarreau9b28e032012-10-12 23:49:43 +0200999 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001000 end = ptr + txn->req.sl.rq.u_l;
1001
1002 if (ptr >= end)
1003 return NULL;
1004
Lukas Tribus23953682017-04-28 13:24:30 +00001005 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +01001006 * Request-URI = "*" | absuri | abspath | authority
1007 */
1008
1009 if (*ptr == '*')
1010 return NULL;
1011
1012 if (isalpha((unsigned char)*ptr)) {
1013 /* this is a scheme as described by RFC3986, par. 3.1 */
1014 ptr++;
1015 while (ptr < end &&
1016 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1017 ptr++;
1018 /* skip '://' */
1019 if (ptr == end || *ptr++ != ':')
1020 return NULL;
1021 if (ptr == end || *ptr++ != '/')
1022 return NULL;
1023 if (ptr == end || *ptr++ != '/')
1024 return NULL;
1025 }
1026 /* skip [user[:passwd]@]host[:[port]] */
1027
1028 while (ptr < end && *ptr != '/')
1029 ptr++;
1030
1031 if (ptr == end)
1032 return NULL;
1033
1034 /* OK, we got the '/' ! */
1035 return ptr;
1036}
1037
William Lallemand65ad6e12014-01-31 15:08:02 +01001038/* Parse the URI from the given string and look for the "/" beginning the PATH.
1039 * If not found, return NULL. It is returned otherwise.
1040 */
1041static char *
1042http_get_path_from_string(char *str)
1043{
1044 char *ptr = str;
1045
1046 /* RFC2616, par. 5.1.2 :
1047 * Request-URI = "*" | absuri | abspath | authority
1048 */
1049
1050 if (*ptr == '*')
1051 return NULL;
1052
1053 if (isalpha((unsigned char)*ptr)) {
1054 /* this is a scheme as described by RFC3986, par. 3.1 */
1055 ptr++;
1056 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1057 ptr++;
1058 /* skip '://' */
1059 if (*ptr == '\0' || *ptr++ != ':')
1060 return NULL;
1061 if (*ptr == '\0' || *ptr++ != '/')
1062 return NULL;
1063 if (*ptr == '\0' || *ptr++ != '/')
1064 return NULL;
1065 }
1066 /* skip [user[:passwd]@]host[:[port]] */
1067
1068 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1069 ptr++;
1070
1071 if (*ptr == '\0' || *ptr == ' ')
1072 return NULL;
1073
1074 /* OK, we got the '/' ! */
1075 return ptr;
1076}
1077
Willy Tarreau71241ab2012-12-27 11:30:54 +01001078/* Returns a 302 for a redirectable request that reaches a server working in
1079 * in redirect mode. This may only be called just after the stream interface
1080 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1081 * follow normal proxy processing. NOTE: this function is designed to support
1082 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001084void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085{
1086 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001087 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001089 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001090
1091 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001092 trash.len = strlen(HTTP_302);
1093 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001094
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001095 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001096
Willy Tarreauefb453c2008-10-26 20:49:47 +01001097 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001098 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001099 return;
1100
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001101 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001102 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001103 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1104 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001105 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001106
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001107 /* 3: add the request URI. Since it was already forwarded, we need
1108 * to temporarily rewind the buffer.
1109 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001110 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001111 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001112
Willy Tarreauefb453c2008-10-26 20:49:47 +01001113 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001114 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001115
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001116 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001117
Willy Tarreauefb453c2008-10-26 20:49:47 +01001118 if (!path)
1119 return;
1120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001121 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001122 return;
1123
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001124 memcpy(trash.str + trash.len, path, len);
1125 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001126
1127 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001128 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1129 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001130 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001131 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1132 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001133 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001134
1135 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001136 si_shutr(si);
1137 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001138 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001139 si->state = SI_ST_CLO;
1140
1141 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001142 txn->status = 302;
1143 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001144
1145 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001146 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001147 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001148}
1149
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001150/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001151 * that the server side is closed. Note that err_type is actually a
1152 * bitmask, where almost only aborts may be cumulated with other
1153 * values. We consider that aborted operations are more important
1154 * than timeouts or errors due to the fact that nobody else in the
1155 * logs might explain incomplete retries. All others should avoid
1156 * being cumulated. It should normally not be possible to have multiple
1157 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001158 * Note that connection errors appearing on the second request of a keep-alive
1159 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001160 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001161void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001162{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001163 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001164
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001165 /* set s->txn->status for http_error_message(s) */
1166 s->txn->status = 503;
1167
Willy Tarreauefb453c2008-10-26 20:49:47 +01001168 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001170 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001171 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001173 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001174 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001175 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001177 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001178 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001180 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001181 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001182 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001183 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001184 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001185 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001186 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001187 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001188 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001189 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001190 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001191 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001192 http_error_message(s));
1193 else { /* SI_ET_CONN_OTHER and others */
1194 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001195 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001196 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001197 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001198}
1199
Willy Tarreau42250582007-04-01 01:30:43 +02001200extern const char sess_term_cond[8];
1201extern const char sess_fin_state[8];
1202extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001203struct pool_head *pool_head_http_txn;
1204struct pool_head *pool_head_requri;
1205struct pool_head *pool_head_capture = NULL;
1206struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001207
Willy Tarreau117f59e2007-03-04 18:17:17 +01001208/*
1209 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001210 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001211 */
1212void capture_headers(char *som, struct hdr_idx *idx,
1213 char **cap, struct cap_hdr *cap_hdr)
1214{
1215 char *eol, *sol, *col, *sov;
1216 int cur_idx;
1217 struct cap_hdr *h;
1218 int len;
1219
1220 sol = som + hdr_idx_first_pos(idx);
1221 cur_idx = hdr_idx_first_idx(idx);
1222
1223 while (cur_idx) {
1224 eol = sol + idx->v[cur_idx].len;
1225
1226 col = sol;
1227 while (col < eol && *col != ':')
1228 col++;
1229
1230 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001231 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001232 sov++;
1233
1234 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001235 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001236 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1237 if (cap[h->index] == NULL)
1238 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001239 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001240
1241 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001242 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001243 continue;
1244 }
1245
1246 len = eol - sov;
1247 if (len > h->len)
1248 len = h->len;
1249
1250 memcpy(cap[h->index], sov, len);
1251 cap[h->index][len]=0;
1252 }
1253 }
1254 sol = eol + idx->v[cur_idx].cr + 1;
1255 cur_idx = idx->v[cur_idx].next;
1256 }
1257}
1258
Willy Tarreaubaaee002006-06-26 02:48:02 +02001259/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001260 * Returns the data from Authorization header. Function may be called more
1261 * than once so data is stored in txn->auth_data. When no header is found
1262 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001263 * searching again for something we are unable to find anyway. However, if
1264 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001265 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001266 */
1267
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001268int
Willy Tarreau87b09662015-04-03 00:22:06 +02001269get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001270{
1271
Willy Tarreaueee5b512015-04-03 23:46:31 +02001272 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001273 struct chunk auth_method;
1274 struct hdr_ctx ctx;
1275 char *h, *p;
1276 int len;
1277
1278#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001279 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001280#endif
1281
1282 if (txn->auth.method == HTTP_AUTH_WRONG)
1283 return 0;
1284
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001285 txn->auth.method = HTTP_AUTH_WRONG;
1286
1287 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001288
1289 if (txn->flags & TX_USE_PX_CONN) {
1290 h = "Proxy-Authorization";
1291 len = strlen(h);
1292 } else {
1293 h = "Authorization";
1294 len = strlen(h);
1295 }
1296
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001297 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001298 return 0;
1299
1300 h = ctx.line + ctx.val;
1301
1302 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001303 len = p - h;
1304 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001305 return 0;
1306
David Carlier7365f7d2016-04-04 11:54:42 +01001307 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1308 return 0;
1309
Willy Tarreau5c557d12016-03-13 08:17:02 +01001310 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001311
1312 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001313 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001314
1315 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001316 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317
1318 if (len < 0)
1319 return 0;
1320
1321
Christopher Faulet6988f672017-07-27 15:18:52 +02001322 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001323
Christopher Faulet6988f672017-07-27 15:18:52 +02001324 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001325
1326 if (!p)
1327 return 0;
1328
Christopher Faulet6988f672017-07-27 15:18:52 +02001329 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001330 *p = '\0';
1331 txn->auth.pass = p+1;
1332
1333 txn->auth.method = HTTP_AUTH_BASIC;
1334 return 1;
1335 }
1336
1337 return 0;
1338}
1339
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001340
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001341/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1342 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1343 * nothing is done and 1 is returned.
1344 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001345static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001346{
1347 int delta;
1348 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001349 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001350
1351 if (msg->sl.rq.v_l != 0)
1352 return 1;
1353
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001354 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1355 if (txn->meth != HTTP_METH_GET)
1356 return 0;
1357
Willy Tarreau9b28e032012-10-12 23:49:43 +02001358 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001359
1360 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001361 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1362 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001363 }
1364 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001365 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001366 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001367 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001368 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001369 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001370 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001371 NULL, NULL);
1372 if (unlikely(!cur_end))
1373 return 0;
1374
1375 /* we have a full HTTP/1.0 request now and we know that
1376 * we have either a CR or an LF at <ptr>.
1377 */
1378 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1379 return 1;
1380}
1381
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001382/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001383 * and "keep-alive" values. If we already know that some headers may safely
1384 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001385 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1386 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001387 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001388 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1389 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1390 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001391 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001392 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001393void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001394{
Willy Tarreau5b154472009-12-21 20:11:07 +01001395 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001396 const char *hdr_val = "Connection";
1397 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001398
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001399 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001400 return;
1401
Willy Tarreau88d349d2010-01-25 12:15:43 +01001402 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1403 hdr_val = "Proxy-Connection";
1404 hdr_len = 16;
1405 }
1406
Willy Tarreau5b154472009-12-21 20:11:07 +01001407 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001408 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001409 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001410 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1411 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001412 if (to_del & 2)
1413 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001414 else
1415 txn->flags |= TX_CON_KAL_SET;
1416 }
1417 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1418 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001419 if (to_del & 1)
1420 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001421 else
1422 txn->flags |= TX_CON_CLO_SET;
1423 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001424 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1425 txn->flags |= TX_HDR_CONN_UPG;
1426 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001427 }
1428
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001429 txn->flags |= TX_HDR_CONN_PRS;
1430 return;
1431}
Willy Tarreau5b154472009-12-21 20:11:07 +01001432
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001433/* Apply desired changes on the Connection: header. Values may be removed and/or
1434 * added depending on the <wanted> flags, which are exclusively composed of
1435 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1436 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1437 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001438void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001439{
1440 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001441 const char *hdr_val = "Connection";
1442 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001443
1444 ctx.idx = 0;
1445
Willy Tarreau88d349d2010-01-25 12:15:43 +01001446
1447 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1448 hdr_val = "Proxy-Connection";
1449 hdr_len = 16;
1450 }
1451
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001452 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001453 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001454 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1455 if (wanted & TX_CON_KAL_SET)
1456 txn->flags |= TX_CON_KAL_SET;
1457 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001458 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001459 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001460 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1461 if (wanted & TX_CON_CLO_SET)
1462 txn->flags |= TX_CON_CLO_SET;
1463 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001464 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001465 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001466 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001467
1468 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1469 return;
1470
1471 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1472 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001473 hdr_val = "Connection: close";
1474 hdr_len = 17;
1475 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1476 hdr_val = "Proxy-Connection: close";
1477 hdr_len = 23;
1478 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001479 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001480 }
1481
1482 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1483 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001484 hdr_val = "Connection: keep-alive";
1485 hdr_len = 22;
1486 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1487 hdr_val = "Proxy-Connection: keep-alive";
1488 hdr_len = 28;
1489 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001490 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001491 }
1492 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001493}
1494
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001495/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1496 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1497 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1498 * Unparsable qvalues return 1000 as "q=1.000".
1499 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001500int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001501{
1502 int q = 1000;
1503
Willy Tarreau506c69a2014-07-08 00:59:48 +02001504 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001505 goto out;
1506 q = (*qvalue++ - '0') * 1000;
1507
1508 if (*qvalue++ != '.')
1509 goto out;
1510
Willy Tarreau506c69a2014-07-08 00:59:48 +02001511 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001512 goto out;
1513 q += (*qvalue++ - '0') * 100;
1514
Willy Tarreau506c69a2014-07-08 00:59:48 +02001515 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001516 goto out;
1517 q += (*qvalue++ - '0') * 10;
1518
Willy Tarreau506c69a2014-07-08 00:59:48 +02001519 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001520 goto out;
1521 q += (*qvalue++ - '0') * 1;
1522 out:
1523 if (q > 1000)
1524 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001525 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001526 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001527 return q;
1528}
William Lallemand82fe75c2012-10-23 10:25:10 +02001529
Willy Tarreau87b09662015-04-03 00:22:06 +02001530void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001531{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001532 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001533 int tmp = TX_CON_WANT_KAL;
1534
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001535 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1536 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001537 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1538 tmp = TX_CON_WANT_TUN;
1539
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001540 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001541 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1542 tmp = TX_CON_WANT_TUN;
1543 }
1544
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001545 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001546 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1547 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001548 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001549 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1550 tmp = TX_CON_WANT_CLO;
1551 else
1552 tmp = TX_CON_WANT_SCL;
1553 }
1554
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001555 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001556 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1557 tmp = TX_CON_WANT_CLO;
1558
1559 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1560 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1561
1562 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1563 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1564 /* parse the Connection header and possibly clean it */
1565 int to_del = 0;
1566 if ((msg->flags & HTTP_MSGF_VER_11) ||
1567 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001568 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001569 to_del |= 2; /* remove "keep-alive" */
1570 if (!(msg->flags & HTTP_MSGF_VER_11))
1571 to_del |= 1; /* remove "close" */
1572 http_parse_connection_header(txn, msg, to_del);
1573 }
1574
1575 /* check if client or config asks for explicit close in KAL/SCL */
1576 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1577 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1578 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1579 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1580 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001581 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001582 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1583}
William Lallemand82fe75c2012-10-23 10:25:10 +02001584
Willy Tarreaud787e662009-07-07 10:14:51 +02001585/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1586 * processing can continue on next analysers, or zero if it either needs more
1587 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001588 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001589 * when it has nothing left to do, and may remove any analyser when it wants to
1590 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001592int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593{
Willy Tarreau59234e92008-11-30 23:51:27 +01001594 /*
1595 * We will parse the partial (or complete) lines.
1596 * We will check the request syntax, and also join multi-line
1597 * headers. An index of all the lines will be elaborated while
1598 * parsing.
1599 *
1600 * For the parsing, we use a 28 states FSM.
1601 *
1602 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001603 * req->buf->p = beginning of request
1604 * req->buf->p + msg->eoh = end of processed headers / start of current one
1605 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001606 * msg->eol = end of current header or line (LF or CRLF)
1607 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001608 *
1609 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001610 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001611 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1612 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001613 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau59234e92008-11-30 23:51:27 +01001615 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001616 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001617 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001618 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001619 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001620
Willy Tarreau87b09662015-04-03 00:22:06 +02001621 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01001622 now_ms, __FUNCTION__,
1623 s,
1624 req,
1625 req->rex, req->wex,
1626 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001627 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001628 req->analysers);
1629
Willy Tarreau52a0c602009-08-16 22:45:38 +02001630 /* we're speaking HTTP here, so let's speak HTTP to the client */
1631 s->srv_error = http_return_srv_error;
1632
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001633 /* If there is data available for analysis, log the end of the idle time. */
1634 if (buffer_not_empty(req->buf) && s->logs.t_idle == -1)
1635 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1636
Willy Tarreau83e3af02009-12-28 17:39:57 +01001637 /* There's a protected area at the end of the buffer for rewriting
1638 * purposes. We don't want to start to parse the request if the
1639 * protected area is affected, because we may have to move processed
1640 * data later, which is much more complicated.
1641 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001642 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001643 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001644 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001645 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001646 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001647 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001648 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001649 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001650 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001651 return 0;
1652 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001653 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1654 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1655 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001656 }
1657
Willy Tarreau9b28e032012-10-12 23:49:43 +02001658 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001659 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001660 }
1661
Willy Tarreau59234e92008-11-30 23:51:27 +01001662 /* 1: we might have to print this header in debug mode */
1663 if (unlikely((global.mode & MODE_DEBUG) &&
1664 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001665 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001666 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau9b28e032012-10-12 23:49:43 +02001668 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001669 /* this is a bit complex : in case of error on the request line,
1670 * we know that rq.l is still zero, so we display only the part
1671 * up to the end of the line (truncated by debug_hdr).
1672 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001673 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001674 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001675
Willy Tarreau59234e92008-11-30 23:51:27 +01001676 sol += hdr_idx_first_pos(&txn->hdr_idx);
1677 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001678
Willy Tarreau59234e92008-11-30 23:51:27 +01001679 while (cur_idx) {
1680 eol = sol + txn->hdr_idx.v[cur_idx].len;
1681 debug_hdr("clihdr", s, sol, eol);
1682 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1683 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001685 }
1686
Willy Tarreau58f10d72006-12-04 02:26:12 +01001687
Willy Tarreau59234e92008-11-30 23:51:27 +01001688 /*
1689 * Now we quickly check if we have found a full valid request.
1690 * If not so, we check the FD and buffer states before leaving.
1691 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001692 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001693 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001694 * on a keep-alive stream, if we encounter and error, close, t/o,
1695 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001696 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001697 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001698 * Last, we may increase some tracked counters' http request errors on
1699 * the cases that are deliberately the client's fault. For instance,
1700 * a timeout or connection reset is not counted as an error. However
1701 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001702 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001703
Willy Tarreau655dce92009-11-08 13:10:58 +01001704 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001705 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001706 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001707 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001708 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001709 stream_inc_http_req_ctr(s);
1710 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001711 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001712 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001713 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001714
Willy Tarreau59234e92008-11-30 23:51:27 +01001715 /* 1: Since we are in header mode, if there's no space
1716 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001717 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001718 * must terminate it now.
1719 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001720 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001721 /* FIXME: check if URI is set and return Status
1722 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001723 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001724 stream_inc_http_req_ctr(s);
1725 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001726 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001727 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001728 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001729 goto return_bad_req;
1730 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001731
Willy Tarreau59234e92008-11-30 23:51:27 +01001732 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001733 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001734 if (!(s->flags & SF_ERR_MASK))
1735 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001736
Willy Tarreaufcffa692010-01-10 14:21:19 +01001737 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001738 goto failed_keep_alive;
1739
Willy Tarreau0f228a02015-05-01 15:37:53 +02001740 if (sess->fe->options & PR_O_IGNORE_PRB)
1741 goto failed_keep_alive;
1742
Willy Tarreau59234e92008-11-30 23:51:27 +01001743 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001744 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001745 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001746 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001747 }
1748
Willy Tarreaudc979f22012-12-04 10:39:01 +01001749 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001750 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001751 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001752 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001753 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001754 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001755 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001756 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001757 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001758 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001759
Willy Tarreaue7dff022015-04-03 01:14:29 +02001760 if (!(s->flags & SF_FINST_MASK))
1761 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001762 return 0;
1763 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001764
Willy Tarreau59234e92008-11-30 23:51:27 +01001765 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001766 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001767 if (!(s->flags & SF_ERR_MASK))
1768 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001769
Willy Tarreaufcffa692010-01-10 14:21:19 +01001770 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001771 goto failed_keep_alive;
1772
Willy Tarreau0f228a02015-05-01 15:37:53 +02001773 if (sess->fe->options & PR_O_IGNORE_PRB)
1774 goto failed_keep_alive;
1775
Willy Tarreau59234e92008-11-30 23:51:27 +01001776 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001777 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001778 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001779 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001780 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001781 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001782 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001783 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001784 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001785 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001786
Willy Tarreau87b09662015-04-03 00:22:06 +02001787 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001788 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001789 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001790 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001791 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001792
Willy Tarreaue7dff022015-04-03 01:14:29 +02001793 if (!(s->flags & SF_FINST_MASK))
1794 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001795 return 0;
1796 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001797
Willy Tarreau59234e92008-11-30 23:51:27 +01001798 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001799 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001800 if (!(s->flags & SF_ERR_MASK))
1801 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001802
Willy Tarreaufcffa692010-01-10 14:21:19 +01001803 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001804 goto failed_keep_alive;
1805
Willy Tarreau0f228a02015-05-01 15:37:53 +02001806 if (sess->fe->options & PR_O_IGNORE_PRB)
1807 goto failed_keep_alive;
1808
Willy Tarreau4076a152009-04-02 15:18:36 +02001809 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001810 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001811 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001812 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001813 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001814 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001815 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001816 stream_inc_http_err_ctr(s);
1817 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001818 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001819 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001820 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001821 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001822
Willy Tarreaue7dff022015-04-03 01:14:29 +02001823 if (!(s->flags & SF_FINST_MASK))
1824 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001825 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001826 }
1827
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001828 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001829 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001830 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001831#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001832 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1833 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001834 /* We need more data, we have to re-enable quick-ack in case we
1835 * previously disabled it, otherwise we might cause the client
1836 * to delay next data.
1837 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001838 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001839 }
1840#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001841
Willy Tarreaufcffa692010-01-10 14:21:19 +01001842 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1843 /* If the client starts to talk, let's fall back to
1844 * request timeout processing.
1845 */
1846 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001847 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001848 }
1849
Willy Tarreau59234e92008-11-30 23:51:27 +01001850 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001851 if (!tick_isset(req->analyse_exp)) {
1852 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1853 (txn->flags & TX_WAIT_NEXT_RQ) &&
1854 tick_isset(s->be->timeout.httpka))
1855 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1856 else
1857 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001859
Willy Tarreau59234e92008-11-30 23:51:27 +01001860 /* we're not ready yet */
1861 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001862
1863 failed_keep_alive:
1864 /* Here we process low-level errors for keep-alive requests. In
1865 * short, if the request is not the first one and it experiences
1866 * a timeout, read error or shutdown, we just silently close so
1867 * that the client can try again.
1868 */
1869 txn->status = 0;
1870 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001871 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001872 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001873 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001874 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001875 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001876 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001877 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001878
Willy Tarreaud787e662009-07-07 10:14:51 +02001879 /* OK now we have a complete HTTP request with indexed headers. Let's
1880 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001881 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001882 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001883 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001884 * byte after the last LF. msg->sov points to the first byte of data.
1885 * msg->eol cannot be trusted because it may have been left uninitialized
1886 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001887 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001888
Willy Tarreau87b09662015-04-03 00:22:06 +02001889 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001890 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001891
Willy Tarreaub16a5742010-01-10 14:46:16 +01001892 if (txn->flags & TX_WAIT_NEXT_RQ) {
1893 /* kill the pending keep-alive timeout */
1894 txn->flags &= ~TX_WAIT_NEXT_RQ;
1895 req->analyse_exp = TICK_ETERNITY;
1896 }
1897
1898
Willy Tarreaud787e662009-07-07 10:14:51 +02001899 /* Maybe we found in invalid header name while we were configured not
1900 * to block on that, so we have to capture it now.
1901 */
1902 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001903 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001904
Willy Tarreau59234e92008-11-30 23:51:27 +01001905 /*
1906 * 1: identify the method
1907 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001908 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001909
1910 /* we can make use of server redirect on GET and HEAD */
1911 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001912 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001913 else if (txn->meth == HTTP_METH_OTHER &&
1914 msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) {
1915 /* PRI is reserved for the HTTP/2 preface */
1916 msg->err_pos = 0;
1917 goto return_bad_req;
1918 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001919
Willy Tarreau59234e92008-11-30 23:51:27 +01001920 /*
1921 * 2: check if the URI matches the monitor_uri.
1922 * We have to do this for every request which gets in, because
1923 * the monitor-uri is defined by the frontend.
1924 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001925 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1926 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001927 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001928 sess->fe->monitor_uri,
1929 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001930 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001931 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001932 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001934
Willy Tarreaue7dff022015-04-03 01:14:29 +02001935 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001936 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001937
Willy Tarreau59234e92008-11-30 23:51:27 +01001938 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001939 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001940 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001941
Willy Tarreau59234e92008-11-30 23:51:27 +01001942 ret = acl_pass(ret);
1943 if (cond->pol == ACL_COND_UNLESS)
1944 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001945
Willy Tarreau59234e92008-11-30 23:51:27 +01001946 if (ret) {
1947 /* we fail this request, let's return 503 service unavail */
1948 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001949 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001950 if (!(s->flags & SF_ERR_MASK))
1951 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001952 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001953 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001954 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001955
Willy Tarreau59234e92008-11-30 23:51:27 +01001956 /* nothing to fail, let's reply normaly */
1957 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001958 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001959 if (!(s->flags & SF_ERR_MASK))
1960 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001961 goto return_prx_cond;
1962 }
1963
1964 /*
1965 * 3: Maybe we have to copy the original REQURI for the logs ?
1966 * Note: we cannot log anymore if the request has been
1967 * classified as invalid.
1968 */
1969 if (unlikely(s->logs.logwait & LW_REQ)) {
1970 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001971 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001972 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001974 if (urilen >= global.tune.requri_len )
1975 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001976 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001977 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001978
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001979 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001980 s->do_log(s);
1981 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001982 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001983 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001984 }
Willy Tarreau06619262006-12-17 08:37:22 +01001985
Willy Tarreau91852eb2015-05-01 13:26:00 +02001986 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1987 * exactly one digit "." one digit. This check may be disabled using
1988 * option accept-invalid-http-request.
1989 */
1990 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1991 if (msg->sl.rq.v_l != 8) {
1992 msg->err_pos = msg->sl.rq.v;
1993 goto return_bad_req;
1994 }
1995
1996 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1997 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1998 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1999 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2000 msg->err_pos = msg->sl.rq.v + 4;
2001 goto return_bad_req;
2002 }
2003 }
Willy Tarreau13317662015-05-01 13:47:08 +02002004 else {
2005 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2006 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2007 goto return_bad_req;
2008 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002009
Willy Tarreau5b154472009-12-21 20:11:07 +01002010 /* ... and check if the request is HTTP/1.1 or above */
2011 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002012 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2013 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2014 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002015 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002016
2017 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002018 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 +01002019
Willy Tarreau88d349d2010-01-25 12:15:43 +01002020 /* if the frontend has "option http-use-proxy-header", we'll check if
2021 * we have what looks like a proxied connection instead of a connection,
2022 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2023 * Note that this is *not* RFC-compliant, however browsers and proxies
2024 * happen to do that despite being non-standard :-(
2025 * We consider that a request not beginning with either '/' or '*' is
2026 * a proxied connection, which covers both "scheme://location" and
2027 * CONNECT ip:port.
2028 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002029 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002030 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031 txn->flags |= TX_USE_PX_CONN;
2032
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002033 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002034 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002035
Willy Tarreau59234e92008-11-30 23:51:27 +01002036 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002037 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002038 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002039 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002040
Willy Tarreau557f1992015-05-01 10:05:17 +02002041 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2042 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002043 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002044 * The length of a message body is determined by one of the following
2045 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002046 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002047 * 1. Any response to a HEAD request and any response with a 1xx
2048 * (Informational), 204 (No Content), or 304 (Not Modified) status
2049 * code is always terminated by the first empty line after the
2050 * header fields, regardless of the header fields present in the
2051 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002052 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002053 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2054 * the connection will become a tunnel immediately after the empty
2055 * line that concludes the header fields. A client MUST ignore any
2056 * Content-Length or Transfer-Encoding header fields received in
2057 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002058 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002059 * 3. If a Transfer-Encoding header field is present and the chunked
2060 * transfer coding (Section 4.1) is the final encoding, the message
2061 * body length is determined by reading and decoding the chunked
2062 * data until the transfer coding indicates the data is complete.
2063 *
2064 * If a Transfer-Encoding header field is present in a response and
2065 * the chunked transfer coding is not the final encoding, the
2066 * message body length is determined by reading the connection until
2067 * it is closed by the server. If a Transfer-Encoding header field
2068 * is present in a request and the chunked transfer coding is not
2069 * the final encoding, the message body length cannot be determined
2070 * reliably; the server MUST respond with the 400 (Bad Request)
2071 * status code and then close the connection.
2072 *
2073 * If a message is received with both a Transfer-Encoding and a
2074 * Content-Length header field, the Transfer-Encoding overrides the
2075 * Content-Length. Such a message might indicate an attempt to
2076 * perform request smuggling (Section 9.5) or response splitting
2077 * (Section 9.4) and ought to be handled as an error. A sender MUST
2078 * remove the received Content-Length field prior to forwarding such
2079 * a message downstream.
2080 *
2081 * 4. If a message is received without Transfer-Encoding and with
2082 * either multiple Content-Length header fields having differing
2083 * field-values or a single Content-Length header field having an
2084 * invalid value, then the message framing is invalid and the
2085 * recipient MUST treat it as an unrecoverable error. If this is a
2086 * request message, the server MUST respond with a 400 (Bad Request)
2087 * status code and then close the connection. If this is a response
2088 * message received by a proxy, the proxy MUST close the connection
2089 * to the server, discard the received response, and send a 502 (Bad
2090 * Gateway) response to the client. If this is a response message
2091 * received by a user agent, the user agent MUST close the
2092 * connection to the server and discard the received response.
2093 *
2094 * 5. If a valid Content-Length header field is present without
2095 * Transfer-Encoding, its decimal value defines the expected message
2096 * body length in octets. If the sender closes the connection or
2097 * the recipient times out before the indicated number of octets are
2098 * received, the recipient MUST consider the message to be
2099 * incomplete and close the connection.
2100 *
2101 * 6. If this is a request message and none of the above are true, then
2102 * the message body length is zero (no message body is present).
2103 *
2104 * 7. Otherwise, this is a response message without a declared message
2105 * body length, so the message body length is determined by the
2106 * number of octets received prior to the server closing the
2107 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002108 */
2109
Willy Tarreau32b47f42009-10-18 20:55:02 +02002110 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002111 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002112 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002113 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002114 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002115 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002116 /* chunked not last, return badreq */
2117 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002118 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002119 }
2120
Willy Tarreau1c913912015-04-30 10:57:51 +02002121 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002122 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002123 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2124 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2125 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2126 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002127 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002128 signed long long cl;
2129
Willy Tarreauad14f752011-09-02 20:33:27 +02002130 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002131 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002132 goto return_bad_req;
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 (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002136 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002137 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002138 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002139
Willy Tarreauad14f752011-09-02 20:33:27 +02002140 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002141 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002142 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002143 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002144
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002145 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002146 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002147 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002148 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002149
Christopher Fauletbe821b92017-03-30 11:21:53 +02002150 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002151 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002152 }
2153
Willy Tarreau34dfc602015-05-01 10:09:49 +02002154 /* even bodyless requests have a known length */
2155 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002156
Willy Tarreau179085c2014-04-28 16:48:56 +02002157 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2158 * only change if both the request and the config reference something else.
2159 * Option httpclose by itself sets tunnel mode where headers are mangled.
2160 * However, if another mode is set, it will affect it (eg: server-close/
2161 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2162 * if FE and BE have the same settings (common). The method consists in
2163 * checking if options changed between the two calls (implying that either
2164 * one is non-null, or one of them is non-null and we are there for the first
2165 * time.
2166 */
2167 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002168 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002169 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002170
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002171 /* we may have to wait for the request's body */
2172 if ((s->be->options & PR_O_WREQ_BODY) &&
2173 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2174 req->analysers |= AN_REQ_HTTP_BODY;
2175
Willy Tarreau83ece462017-12-21 15:13:09 +01002176 /*
2177 * RFC7234#4:
2178 * A cache MUST write through requests with methods
2179 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2180 * the origin server; i.e., a cache is not allowed
2181 * to generate a reply to such a request before
2182 * having forwarded the request and having received
2183 * a corresponding response.
2184 *
2185 * RFC7231#4.2.1:
2186 * Of the request methods defined by this
2187 * specification, the GET, HEAD, OPTIONS, and TRACE
2188 * methods are defined to be safe.
2189 */
2190 if (likely(txn->meth == HTTP_METH_GET ||
2191 txn->meth == HTTP_METH_HEAD ||
2192 txn->meth == HTTP_METH_OPTIONS ||
2193 txn->meth == HTTP_METH_TRACE))
2194 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2195
Willy Tarreaud787e662009-07-07 10:14:51 +02002196 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002197 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002198 req->analyse_exp = TICK_ETERNITY;
2199 return 1;
2200
2201 return_bad_req:
2202 /* We centralize bad requests processing here */
2203 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2204 /* we detected a parsing error. We want to archive this request
2205 * in the dedicated proxy area for later troubleshooting.
2206 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002207 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002208 }
2209
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002210 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002211 txn->req.msg_state = HTTP_MSG_ERROR;
2212 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002213 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002214
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002215 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002216 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002217 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002218
2219 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002220 if (!(s->flags & SF_ERR_MASK))
2221 s->flags |= SF_ERR_PRXCOND;
2222 if (!(s->flags & SF_FINST_MASK))
2223 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002224
Christopher Faulet0184ea72017-01-05 14:06:34 +01002225 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002226 req->analyse_exp = TICK_ETERNITY;
2227 return 0;
2228}
2229
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002230
Willy Tarreau347a35d2013-11-22 17:51:09 +01002231/* This function prepares an applet to handle the stats. It can deal with the
2232 * "100-continue" expectation, check that admin rules are met for POST requests,
2233 * and program a response message if something was unexpected. It cannot fail
2234 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002235 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002236 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002237 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002238 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002239int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002240{
2241 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002242 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002243 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002244 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002245 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002246 struct uri_auth *uri_auth = s->be->uri_auth;
2247 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002248 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002249
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002250 appctx = si_appctx(si);
2251 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2252 appctx->st1 = appctx->st2 = 0;
2253 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2254 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002255 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002256 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002257
2258 uri = msg->chn->buf->p + msg->sl.rq.u;
2259 lookup = uri + uri_auth->uri_len;
2260
2261 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2262 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002263 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002264 break;
2265 }
2266 }
2267
2268 if (uri_auth->refresh) {
2269 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2270 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002271 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002272 break;
2273 }
2274 }
2275 }
2276
2277 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2278 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002279 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002280 break;
2281 }
2282 }
2283
Willy Tarreau1e62df92016-01-11 18:57:53 +01002284 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2285 if (memcmp(h, ";typed", 6) == 0) {
2286 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2287 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2288 break;
2289 }
2290 }
2291
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002292 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2293 if (memcmp(h, ";st=", 4) == 0) {
2294 int i;
2295 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002296 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002297 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2298 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002299 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002300 break;
2301 }
2302 }
2303 break;
2304 }
2305 }
2306
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002307 appctx->ctx.stats.scope_str = 0;
2308 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002309 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2310 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2311 int itx = 0;
2312 const char *h2;
2313 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2314 const char *err;
2315
2316 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2317 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002318 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002319 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2320 itx++;
2321 h++;
2322 }
2323
2324 if (itx > STAT_SCOPE_TXT_MAXLEN)
2325 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002326 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002327
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002328 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002329 memcpy(scope_txt, h2, itx);
2330 scope_txt[itx] = '\0';
2331 err = invalid_char(scope_txt);
2332 if (err) {
2333 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002334 appctx->ctx.stats.scope_str = 0;
2335 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002336 }
2337 break;
2338 }
2339 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002340
2341 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002342 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002343 int ret = 1;
2344
2345 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002346 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 +01002347 ret = acl_pass(ret);
2348 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2349 ret = !ret;
2350 }
2351
2352 if (ret) {
2353 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002354 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002355 break;
2356 }
2357 }
2358
2359 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002360 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002361 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002362 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002363 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2364 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002366 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002367 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002368 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2369 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002370 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002371 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002372 else {
2373 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002375 }
2376
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002377 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002378 return 1;
2379}
2380
Lukas Tribus67db8df2013-06-23 17:37:13 +02002381/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2382 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2383 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002384void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002385{
2386#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002387 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002388 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2389#endif
2390#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002391 if (from->ss_family == AF_INET6) {
2392 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002393 /* v4-mapped addresses need IP_TOS */
2394 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2395 else
2396 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2397 }
2398#endif
2399}
2400
Willy Tarreau87b09662015-04-03 00:22:06 +02002401int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002402 const char* name, unsigned int name_len,
2403 const char *str, struct my_regex *re,
2404 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002405{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002406 struct hdr_ctx ctx;
2407 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002408 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002409 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2410 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002411 struct chunk *output = get_trash_chunk();
2412
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002413 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002414
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002415 /* Choose the header browsing function. */
2416 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002417 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002418 http_find_hdr_func = http_find_header2;
2419 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002420 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002421 http_find_hdr_func = http_find_full_header2;
2422 break;
2423 default: /* impossible */
2424 return -1;
2425 }
2426
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002427 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2428 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002429 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002430 char *val = ctx.line + ctx.val;
2431 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002432
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002433 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2434 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002435
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002436 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002437 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002438 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002439
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002440 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002441
2442 hdr->len += delta;
2443 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002444
2445 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002446 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002447 }
2448
2449 return 0;
2450}
2451
Willy Tarreau87b09662015-04-03 00:22:06 +02002452static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002453 const char* name, unsigned int name_len,
2454 struct list *fmt, struct my_regex *re,
2455 int action)
2456{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002457 struct chunk *replace;
2458 int ret = -1;
2459
2460 replace = alloc_trash_chunk();
2461 if (!replace)
2462 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002463
2464 replace->len = build_logline(s, replace->str, replace->size, fmt);
2465 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002466 goto leave;
2467
2468 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002469
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002470 leave:
2471 free_trash_chunk(replace);
2472 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002473}
2474
Willy Tarreau87b09662015-04-03 00:22:06 +02002475/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002476 * transaction <txn>. Returns the verdict of the first rule that prevents
2477 * further processing of the request (auth, deny, ...), and defaults to
2478 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2479 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002480 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2481 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2482 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002483 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002484enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002485http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002486{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002487 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002488 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002489 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002490 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002491 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002492 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002493 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002494 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002495
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002496 /* If "the current_rule_list" match the executed rule list, we are in
2497 * resume condition. If a resume is needed it is always in the action
2498 * and never in the ACL or converters. In this case, we initialise the
2499 * current rule, and go to the action execution point.
2500 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002501 if (s->current_rule) {
2502 rule = s->current_rule;
2503 s->current_rule = NULL;
2504 if (s->current_rule_list == rules)
2505 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002506 }
2507 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002508
Willy Tarreauff011f22011-01-06 17:51:27 +01002509 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002510
Willy Tarreau96257ec2012-12-27 10:46:37 +01002511 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002512 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002513 int ret;
2514
Willy Tarreau192252e2015-04-04 01:47:55 +02002515 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002516 ret = acl_pass(ret);
2517
Willy Tarreauff011f22011-01-06 17:51:27 +01002518 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002519 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002520
2521 if (!ret) /* condition not matched */
2522 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002523 }
2524
Willy Tarreauacc98002015-09-27 23:34:39 +02002525 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002526resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002527 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002528 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002529 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002530
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002531 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002532 if (deny_status)
2533 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002534 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002535
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002536 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002537 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002538 if (deny_status)
2539 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002540 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002541
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002542 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002543 /* Auth might be performed on regular http-req rules as well as on stats */
2544 auth_realm = rule->arg.auth.realm;
2545 if (!auth_realm) {
2546 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2547 auth_realm = STATS_DEFAULT_REALM;
2548 else
2549 auth_realm = px->id;
2550 }
2551 /* send 401/407 depending on whether we use a proxy or not. We still
2552 * count one error, because normal browsing won't significantly
2553 * increase the counter but brute force attempts will.
2554 */
2555 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2556 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002557 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002558 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002559 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002560
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002561 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002562 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2563 return HTTP_RULE_RES_BADREQ;
2564 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002565
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002566 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002567 s->task->nice = rule->arg.nice;
2568 break;
2569
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002570 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002571 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002572 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002573 break;
2574
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002575 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002576#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002577 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002578 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002579#endif
2580 break;
2581
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002582 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002583 s->logs.level = rule->arg.loglevel;
2584 break;
2585
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002586 case ACT_HTTP_REPLACE_HDR:
2587 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002588 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2589 rule->arg.hdr_add.name_len,
2590 &rule->arg.hdr_add.fmt,
2591 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002592 return HTTP_RULE_RES_BADREQ;
2593 break;
2594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002595 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002596 ctx.idx = 0;
2597 /* remove all occurrences of the header */
2598 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2599 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2600 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002601 }
Willy Tarreau85603282015-01-21 20:39:27 +01002602 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002603
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002604 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002605 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002606 /* The scope of the trash buffer must be limited to this function. The
2607 * build_logline() function can execute a lot of other function which
2608 * can use the trash buffer. So for limiting the scope of this global
2609 * buffer, we build first the header value using build_logline, and
2610 * after we store the header name.
2611 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002612 struct chunk *replace;
2613
2614 replace = alloc_trash_chunk();
2615 if (!replace)
2616 return HTTP_RULE_RES_BADREQ;
2617
Thierry Fournier4b788f72016-06-01 13:35:36 +02002618 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002619 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2620 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2621 replace->str[rule->arg.hdr_add.name_len] = ':';
2622 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2623 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002624
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002625 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002626 /* remove all occurrences of the header */
2627 ctx.idx = 0;
2628 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2629 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2630 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2631 }
2632 }
2633
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002634 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2635
2636 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002637 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002638 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002639
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002640 case ACT_HTTP_DEL_ACL:
2641 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002642 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002643 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002644
2645 /* collect reference */
2646 ref = pat_ref_lookup(rule->arg.map.ref);
2647 if (!ref)
2648 continue;
2649
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002650 /* allocate key */
2651 key = alloc_trash_chunk();
2652 if (!key)
2653 return HTTP_RULE_RES_BADREQ;
2654
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002655 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002656 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2657 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002658
2659 /* perform update */
2660 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002661 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002662 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002663 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002664
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002665 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002666 break;
2667 }
2668
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002669 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002670 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002671 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002672
2673 /* collect reference */
2674 ref = pat_ref_lookup(rule->arg.map.ref);
2675 if (!ref)
2676 continue;
2677
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002678 /* allocate key */
2679 key = alloc_trash_chunk();
2680 if (!key)
2681 return HTTP_RULE_RES_BADREQ;
2682
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002683 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002684 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2685 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002686
2687 /* perform update */
2688 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002689 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002690 if (pat_ref_find_elt(ref, key->str) == NULL)
2691 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002692 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002693
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002694 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002695 break;
2696 }
2697
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002698 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002699 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002700 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002701
2702 /* collect reference */
2703 ref = pat_ref_lookup(rule->arg.map.ref);
2704 if (!ref)
2705 continue;
2706
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002707 /* allocate key */
2708 key = alloc_trash_chunk();
2709 if (!key)
2710 return HTTP_RULE_RES_BADREQ;
2711
2712 /* allocate value */
2713 value = alloc_trash_chunk();
2714 if (!value) {
2715 free_trash_chunk(key);
2716 return HTTP_RULE_RES_BADREQ;
2717 }
2718
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002719 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002720 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2721 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002722
2723 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002724 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2725 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002726
2727 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002728 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002729 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002730 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002731 else
2732 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002733 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002734
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002735 free_trash_chunk(key);
2736 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002737 break;
2738 }
William Lallemand73025dd2014-04-24 14:38:37 +02002739
Thierry FOURNIER42148732015-09-02 17:17:33 +02002740 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002741 if ((s->req.flags & CF_READ_ERROR) ||
2742 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2743 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2744 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002745 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002746
Willy Tarreauacc98002015-09-27 23:34:39 +02002747 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002748 case ACT_RET_ERR:
2749 case ACT_RET_CONT:
2750 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002751 case ACT_RET_STOP:
2752 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002753 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002754 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002755 return HTTP_RULE_RES_YIELD;
2756 }
William Lallemand73025dd2014-04-24 14:38:37 +02002757 break;
2758
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002759 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002760 /* Note: only the first valid tracking parameter of each
2761 * applies.
2762 */
2763
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002764 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002765 struct stktable *t;
2766 struct stksess *ts;
2767 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002768 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002769
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002770 t = rule->arg.trk_ctr.table.t;
2771 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 +02002772
2773 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002774 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002775
2776 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002777 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2778 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2779 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002780 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002781
Emeric Brun819fc6f2017-06-13 19:37:32 +02002782 if (ptr1)
2783 stktable_data_cast(ptr1, http_req_cnt)++;
2784
2785 if (ptr2)
2786 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2787 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2788
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002789 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002790
2791 /* If data was modified, we need to touch to re-schedule sync */
2792 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002793 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002794
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002795 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002796 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002797 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002798 }
2799 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002800 break;
2801
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002802 /* other flags exists, but normaly, they never be matched. */
2803 default:
2804 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002805 }
2806 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002807
2808 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002809 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002810}
2811
Willy Tarreau71241ab2012-12-27 11:30:54 +01002812
Willy Tarreau51d861a2015-05-22 17:30:48 +02002813/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2814 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2815 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2816 * is returned, the process can continue the evaluation of next rule list. If
2817 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2818 * is returned, it means the operation could not be processed and a server error
2819 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2820 * deny rule. If *YIELD is returned, the caller must call again the function
2821 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002822 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002823static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002824http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002825{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002826 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002827 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002828 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002829 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002830 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002831 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002832
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002833 /* If "the current_rule_list" match the executed rule list, we are in
2834 * resume condition. If a resume is needed it is always in the action
2835 * and never in the ACL or converters. In this case, we initialise the
2836 * current rule, and go to the action execution point.
2837 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002838 if (s->current_rule) {
2839 rule = s->current_rule;
2840 s->current_rule = NULL;
2841 if (s->current_rule_list == rules)
2842 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002843 }
2844 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002845
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002846 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002847
2848 /* check optional condition */
2849 if (rule->cond) {
2850 int ret;
2851
Willy Tarreau192252e2015-04-04 01:47:55 +02002852 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002853 ret = acl_pass(ret);
2854
2855 if (rule->cond->pol == ACL_COND_UNLESS)
2856 ret = !ret;
2857
2858 if (!ret) /* condition not matched */
2859 continue;
2860 }
2861
Willy Tarreauacc98002015-09-27 23:34:39 +02002862 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002863resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002864 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002865 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002866 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002867
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002868 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002869 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002870 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002872 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002873 s->task->nice = rule->arg.nice;
2874 break;
2875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002876 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002877 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002878 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002879 break;
2880
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002881 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002882#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002883 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002884 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002885#endif
2886 break;
2887
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002888 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002889 s->logs.level = rule->arg.loglevel;
2890 break;
2891
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002892 case ACT_HTTP_REPLACE_HDR:
2893 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002894 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2895 rule->arg.hdr_add.name_len,
2896 &rule->arg.hdr_add.fmt,
2897 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002898 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002899 break;
2900
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002901 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002902 ctx.idx = 0;
2903 /* remove all occurrences of the header */
2904 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2905 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2906 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2907 }
Willy Tarreau85603282015-01-21 20:39:27 +01002908 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002909
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002910 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002911 case ACT_HTTP_ADD_HDR: {
2912 struct chunk *replace;
2913
2914 replace = alloc_trash_chunk();
2915 if (!replace)
2916 return HTTP_RULE_RES_BADREQ;
2917
2918 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2919 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2920 replace->len = rule->arg.hdr_add.name_len;
2921 replace->str[replace->len++] = ':';
2922 replace->str[replace->len++] = ' ';
2923 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2924 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002925
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002926 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002927 /* remove all occurrences of the header */
2928 ctx.idx = 0;
2929 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2930 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2931 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2932 }
2933 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002934 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2935
2936 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002937 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002938 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002939
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002940 case ACT_HTTP_DEL_ACL:
2941 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002942 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002943 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002944
2945 /* collect reference */
2946 ref = pat_ref_lookup(rule->arg.map.ref);
2947 if (!ref)
2948 continue;
2949
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002950 /* allocate key */
2951 key = alloc_trash_chunk();
2952 if (!key)
2953 return HTTP_RULE_RES_BADREQ;
2954
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002955 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002956 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2957 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002958
2959 /* perform update */
2960 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002961 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002962 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002963 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002964
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002965 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002966 break;
2967 }
2968
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002969 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002970 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002971 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002972
2973 /* collect reference */
2974 ref = pat_ref_lookup(rule->arg.map.ref);
2975 if (!ref)
2976 continue;
2977
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002978 /* allocate key */
2979 key = alloc_trash_chunk();
2980 if (!key)
2981 return HTTP_RULE_RES_BADREQ;
2982
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002983 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002984 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2985 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002986
2987 /* perform update */
2988 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002989 if (pat_ref_find_elt(ref, key->str) == NULL)
2990 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002991
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002992 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002993 break;
2994 }
2995
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002996 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002997 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002998 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002999
3000 /* collect reference */
3001 ref = pat_ref_lookup(rule->arg.map.ref);
3002 if (!ref)
3003 continue;
3004
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003005 /* allocate key */
3006 key = alloc_trash_chunk();
3007 if (!key)
3008 return HTTP_RULE_RES_BADREQ;
3009
3010 /* allocate value */
3011 value = alloc_trash_chunk();
3012 if (!value) {
3013 free_trash_chunk(key);
3014 return HTTP_RULE_RES_BADREQ;
3015 }
3016
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003017 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003018 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3019 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003020
3021 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003022 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
3023 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003024
3025 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003026 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003027 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003028 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003029 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003030 else
3031 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003032 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003033 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003034 free_trash_chunk(key);
3035 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003036 break;
3037 }
William Lallemand73025dd2014-04-24 14:38:37 +02003038
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003039 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003040 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3041 return HTTP_RULE_RES_BADREQ;
3042 return HTTP_RULE_RES_DONE;
3043
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003044 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3045 /* Note: only the first valid tracking parameter of each
3046 * applies.
3047 */
3048
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003049 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003050 struct stktable *t;
3051 struct stksess *ts;
3052 struct stktable_key *key;
3053 void *ptr;
3054
3055 t = rule->arg.trk_ctr.table.t;
3056 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3057
3058 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003059 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003060
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003061 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003062
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003063 /* let's count a new HTTP request as it's the first time we do it */
3064 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3065 if (ptr)
3066 stktable_data_cast(ptr, http_req_cnt)++;
3067
3068 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3069 if (ptr)
3070 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3071 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3072
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003073 /* When the client triggers a 4xx from the server, it's most often due
3074 * to a missing object or permission. These events should be tracked
3075 * because if they happen often, it may indicate a brute force or a
3076 * vulnerability scan. Normally this is done when receiving the response
3077 * but here we're tracking after this ought to have been done so we have
3078 * to do it on purpose.
3079 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003080 if ((unsigned)(txn->status - 400) < 100) {
3081 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3082 if (ptr)
3083 stktable_data_cast(ptr, http_err_cnt)++;
3084
3085 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3086 if (ptr)
3087 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3088 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3089 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003090
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003091 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003092
Emeric Brun0fed0b02017-11-29 16:15:07 +01003093 /* If data was modified, we need to touch to re-schedule sync */
3094 stktable_touch_local(t, ts, 0);
3095
Emeric Brun819fc6f2017-06-13 19:37:32 +02003096 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3097 if (sess->fe != s->be)
3098 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3099
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003100 }
3101 }
3102 break;
3103
Thierry FOURNIER42148732015-09-02 17:17:33 +02003104 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003105 if ((s->req.flags & CF_READ_ERROR) ||
3106 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3107 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3108 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003109 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003110
Willy Tarreauacc98002015-09-27 23:34:39 +02003111 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003112 case ACT_RET_ERR:
3113 case ACT_RET_CONT:
3114 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003115 case ACT_RET_STOP:
3116 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003117 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003118 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003119 return HTTP_RULE_RES_YIELD;
3120 }
William Lallemand73025dd2014-04-24 14:38:37 +02003121 break;
3122
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003123 /* other flags exists, but normaly, they never be matched. */
3124 default:
3125 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003126 }
3127 }
3128
3129 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003130 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003131}
3132
3133
Willy Tarreau71241ab2012-12-27 11:30:54 +01003134/* Perform an HTTP redirect based on the information in <rule>. The function
3135 * returns non-zero on success, or zero in case of a, irrecoverable error such
3136 * as too large a request to build a valid response.
3137 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003138static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003139{
Willy Tarreaub329a312015-05-22 16:27:37 +02003140 struct http_msg *req = &txn->req;
3141 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003142 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003143 struct chunk *chunk;
3144 int ret = 0;
3145
3146 chunk = alloc_trash_chunk();
3147 if (!chunk)
3148 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003149
3150 /* build redirect message */
3151 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003152 case 308:
3153 msg_fmt = HTTP_308;
3154 break;
3155 case 307:
3156 msg_fmt = HTTP_307;
3157 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003158 case 303:
3159 msg_fmt = HTTP_303;
3160 break;
3161 case 301:
3162 msg_fmt = HTTP_301;
3163 break;
3164 case 302:
3165 default:
3166 msg_fmt = HTTP_302;
3167 break;
3168 }
3169
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003170 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3171 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003172
3173 switch(rule->type) {
3174 case REDIRECT_TYPE_SCHEME: {
3175 const char *path;
3176 const char *host;
3177 struct hdr_ctx ctx;
3178 int pathlen;
3179 int hostlen;
3180
3181 host = "";
3182 hostlen = 0;
3183 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003184 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003185 host = ctx.line + ctx.val;
3186 hostlen = ctx.vlen;
3187 }
3188
3189 path = http_get_path(txn);
3190 /* build message using path */
3191 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003192 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003193 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3194 int qs = 0;
3195 while (qs < pathlen) {
3196 if (path[qs] == '?') {
3197 pathlen = qs;
3198 break;
3199 }
3200 qs++;
3201 }
3202 }
3203 } else {
3204 path = "/";
3205 pathlen = 1;
3206 }
3207
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003208 if (rule->rdr_str) { /* this is an old "redirect" rule */
3209 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003210 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3211 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003212
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003213 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003214 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3215 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003216 }
3217 else {
3218 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003219 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003220
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003221 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003222 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3223 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003224 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003225 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003226 memcpy(chunk->str + chunk->len, "://", 3);
3227 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003228
3229 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003230 memcpy(chunk->str + chunk->len, host, hostlen);
3231 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003232
3233 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003234 memcpy(chunk->str + chunk->len, path, pathlen);
3235 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003236
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003237 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003238 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003239 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003240 if (chunk->len > chunk->size - 5)
3241 goto leave;
3242 chunk->str[chunk->len] = '/';
3243 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003244 }
3245
3246 break;
3247 }
3248 case REDIRECT_TYPE_PREFIX: {
3249 const char *path;
3250 int pathlen;
3251
3252 path = http_get_path(txn);
3253 /* build message using path */
3254 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003255 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003256 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3257 int qs = 0;
3258 while (qs < pathlen) {
3259 if (path[qs] == '?') {
3260 pathlen = qs;
3261 break;
3262 }
3263 qs++;
3264 }
3265 }
3266 } else {
3267 path = "/";
3268 pathlen = 1;
3269 }
3270
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003271 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003272 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3273 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003274
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003275 /* add prefix. Note that if prefix == "/", we don't want to
3276 * add anything, otherwise it makes it hard for the user to
3277 * configure a self-redirection.
3278 */
3279 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003280 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3281 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003282 }
3283 }
3284 else {
3285 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003286 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003287
3288 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003289 if (chunk->len + pathlen > chunk->size - 4)
3290 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003291 }
3292
3293 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003294 memcpy(chunk->str + chunk->len, path, pathlen);
3295 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003296
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003297 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003298 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003299 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003300 if (chunk->len > chunk->size - 5)
3301 goto leave;
3302 chunk->str[chunk->len] = '/';
3303 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003304 }
3305
3306 break;
3307 }
3308 case REDIRECT_TYPE_LOCATION:
3309 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003310 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003311 if (chunk->len + rule->rdr_len > chunk->size - 4)
3312 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003313
3314 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003315 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3316 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003317 }
3318 else {
3319 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003320 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003321
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003322 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003323 if (chunk->len > chunk->size - 4)
3324 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003325 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003326 break;
3327 }
3328
3329 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003330 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3331 chunk->len += 14;
3332 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3333 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003334 }
3335
Willy Tarreau19b14122017-02-28 09:48:11 +01003336 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003337 txn->status = rule->code;
3338 /* let's log the request time */
3339 s->logs.tv_request = now;
3340
Christopher Fauletbe821b92017-03-30 11:21:53 +02003341 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003342 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3343 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3344 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003345 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003346 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003347 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3348 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003349 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003350 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3351 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003352 }
3353 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003354 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3355 chunk->len += 4;
3356 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003357 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003358 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003359 bi_fast_delete(req->chn->buf, req->sov);
3360 req->next -= req->sov;
3361 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003362 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003363 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003364 req->msg_state = HTTP_MSG_CLOSED;
3365 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003366 /* Trim any possible response */
3367 res->chn->buf->i = 0;
3368 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003369 /* let the server side turn to SI_ST_CLO */
3370 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003371 } else {
3372 /* keep-alive not possible */
3373 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003374 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3375 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003376 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003377 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3378 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003379 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003380 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003381 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003382 }
3383
Willy Tarreaue7dff022015-04-03 01:14:29 +02003384 if (!(s->flags & SF_ERR_MASK))
3385 s->flags |= SF_ERR_LOCAL;
3386 if (!(s->flags & SF_FINST_MASK))
3387 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003388
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003389 ret = 1;
3390 leave:
3391 free_trash_chunk(chunk);
3392 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003393}
3394
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003395/* This stream analyser runs all HTTP request processing which is common to
3396 * frontends and backends, which means blocking ACLs, filters, connection-close,
3397 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003398 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003399 * either needs more data or wants to immediately abort the request (eg: deny,
3400 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003401 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003402int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003403{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003404 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003405 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003406 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003407 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003408 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003409 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003410 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003411 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003412
Willy Tarreau655dce92009-11-08 13:10:58 +01003413 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003414 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003415 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003416 }
3417
Willy Tarreau87b09662015-04-03 00:22:06 +02003418 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003419 now_ms, __FUNCTION__,
3420 s,
3421 req,
3422 req->rex, req->wex,
3423 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003424 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003425 req->analysers);
3426
Willy Tarreau65410832014-04-28 21:25:43 +02003427 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003428 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003429
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003430 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003431 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003432 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003433
Willy Tarreau0b748332014-04-29 00:13:29 +02003434 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003435 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3436 goto return_prx_yield;
3437
Willy Tarreau0b748332014-04-29 00:13:29 +02003438 case HTTP_RULE_RES_CONT:
3439 case HTTP_RULE_RES_STOP: /* nothing to do */
3440 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003441
Willy Tarreau0b748332014-04-29 00:13:29 +02003442 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3443 if (txn->flags & TX_CLTARPIT)
3444 goto tarpit;
3445 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003446
Willy Tarreau0b748332014-04-29 00:13:29 +02003447 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3448 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003449
Willy Tarreau0b748332014-04-29 00:13:29 +02003450 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003451 goto done;
3452
Willy Tarreau0b748332014-04-29 00:13:29 +02003453 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3454 goto return_bad_req;
3455 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003456 }
3457
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003458 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3459 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003460 struct hdr_ctx ctx;
3461
3462 ctx.idx = 0;
3463 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3464 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3465 if (unlikely(http_header_add_tail2(&txn->req,
3466 &txn->hdr_idx, "Early-Data: 1",
3467 strlen("Early-Data: 1"))) < 0) {
3468 goto return_bad_req;
3469 }
3470 }
3471
3472 }
3473
Willy Tarreau52542592014-04-28 18:33:26 +02003474 /* OK at this stage, we know that the request was accepted according to
3475 * the http-request rules, we can check for the stats. Note that the
3476 * URI is detected *before* the req* rules in order not to be affected
3477 * by a possible reqrep, while they are processed *after* so that a
3478 * reqdeny can still block them. This clearly needs to change in 1.6!
3479 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003480 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003481 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003482 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003483 txn->status = 500;
3484 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003485 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003486
Willy Tarreaue7dff022015-04-03 01:14:29 +02003487 if (!(s->flags & SF_ERR_MASK))
3488 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003489 goto return_prx_cond;
3490 }
3491
3492 /* parse the whole stats request and extract the relevant information */
3493 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003494 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003495 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003496
Willy Tarreau0b748332014-04-29 00:13:29 +02003497 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3498 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003499
Willy Tarreau0b748332014-04-29 00:13:29 +02003500 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3501 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003502 }
3503
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003504 /* evaluate the req* rules except reqadd */
3505 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003506 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003507 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003508
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003509 if (txn->flags & TX_CLDENY)
3510 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003511
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003512 if (txn->flags & TX_CLTARPIT) {
3513 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003514 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003515 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003516 }
Willy Tarreau06619262006-12-17 08:37:22 +01003517
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003518 /* add request headers from the rule sets in the same order */
3519 list_for_each_entry(wl, &px->req_add, list) {
3520 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003521 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003522 ret = acl_pass(ret);
3523 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3524 ret = !ret;
3525 if (!ret)
3526 continue;
3527 }
3528
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003529 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003530 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003531 }
3532
Willy Tarreau52542592014-04-28 18:33:26 +02003533
3534 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003535 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3536 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003537 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003538 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003539 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003540
Willy Tarreaue7dff022015-04-03 01:14:29 +02003541 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3542 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3543 if (!(s->flags & SF_FINST_MASK))
3544 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003545
Willy Tarreau70730dd2014-04-24 18:06:27 +02003546 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003547 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3548 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003549 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003550 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003551 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003552
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003553 /* check whether we have some ACLs set to redirect this request */
3554 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003555 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003556 int ret;
3557
Willy Tarreau192252e2015-04-04 01:47:55 +02003558 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003559 ret = acl_pass(ret);
3560 if (rule->cond->pol == ACL_COND_UNLESS)
3561 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003562 if (!ret)
3563 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003564 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003565 if (!http_apply_redirect_rule(rule, s, txn))
3566 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003567 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003568 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003569
Willy Tarreau2be39392010-01-03 17:24:51 +01003570 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3571 * If this happens, then the data will not come immediately, so we must
3572 * send all what we have without waiting. Note that due to the small gain
3573 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003574 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003575 * itself once used.
3576 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003577 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003578
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003579 done: /* done with this analyser, continue with next ones that the calling
3580 * points will have set, if any.
3581 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003582 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003583 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3584 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003585 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003586
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003587 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003588 /* Allow cookie logging
3589 */
3590 if (s->be->cookie_name || sess->fe->capture_name)
3591 manage_client_side_cookies(s, req);
3592
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003593 /* When a connection is tarpitted, we use the tarpit timeout,
3594 * which may be the same as the connect timeout if unspecified.
3595 * If unset, then set it to zero because we really want it to
3596 * eventually expire. We build the tarpit as an analyser.
3597 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003598 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003599
3600 /* wipe the request out so that we can drop the connection early
3601 * if the client closes first.
3602 */
3603 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003604
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003605 txn->status = http_err_codes[deny_status];
3606
Christopher Faulet0184ea72017-01-05 14:06:34 +01003607 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003608 req->analysers |= AN_REQ_HTTP_TARPIT;
3609 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3610 if (!req->analyse_exp)
3611 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003612 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003613 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003614 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003615 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003616 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003617 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003618 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003619
3620 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003621
3622 /* Allow cookie logging
3623 */
3624 if (s->be->cookie_name || sess->fe->capture_name)
3625 manage_client_side_cookies(s, req);
3626
Willy Tarreau0b748332014-04-29 00:13:29 +02003627 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003628 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003629 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003630 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003631 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003632 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003633 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003634 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003635 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003636 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003637 goto return_prx_cond;
3638
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 return_bad_req:
3640 /* We centralize bad requests processing here */
3641 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3642 /* we detected a parsing error. We want to archive this request
3643 * in the dedicated proxy area for later troubleshooting.
3644 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003645 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003646 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003647
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003648 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003649 txn->req.msg_state = HTTP_MSG_ERROR;
3650 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003651 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003652
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003653 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003654 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003655 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003656
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003657 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003658 if (!(s->flags & SF_ERR_MASK))
3659 s->flags |= SF_ERR_PRXCOND;
3660 if (!(s->flags & SF_FINST_MASK))
3661 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003662
Christopher Faulet0184ea72017-01-05 14:06:34 +01003663 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003664 req->analyse_exp = TICK_ETERNITY;
3665 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003666
3667 return_prx_yield:
3668 channel_dont_connect(req);
3669 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003671
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003672/* This function performs all the processing enabled for the current request.
3673 * It returns 1 if the processing can continue on next analysers, or zero if it
3674 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003675 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003676 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003677int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003678{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003679 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003680 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003681 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003682 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003683
Willy Tarreau655dce92009-11-08 13:10:58 +01003684 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003685 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003686 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003687 return 0;
3688 }
3689
Willy Tarreau87b09662015-04-03 00:22:06 +02003690 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003691 now_ms, __FUNCTION__,
3692 s,
3693 req,
3694 req->rex, req->wex,
3695 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003696 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003697 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003698
Willy Tarreau59234e92008-11-30 23:51:27 +01003699 /*
3700 * Right now, we know that we have processed the entire headers
3701 * and that unwanted requests have been filtered out. We can do
3702 * whatever we want with the remaining request. Also, now we
3703 * may have separate values for ->fe, ->be.
3704 */
Willy Tarreau06619262006-12-17 08:37:22 +01003705
Willy Tarreau59234e92008-11-30 23:51:27 +01003706 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003707 * If HTTP PROXY is set we simply get remote server address parsing
3708 * incoming request. Note that this requires that a connection is
3709 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003710 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003711 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003712 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003713 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003714
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003715 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003716 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003717 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003718 txn->req.msg_state = HTTP_MSG_ERROR;
3719 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003720 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003721 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003722
Willy Tarreaue7dff022015-04-03 01:14:29 +02003723 if (!(s->flags & SF_ERR_MASK))
3724 s->flags |= SF_ERR_RESOURCE;
3725 if (!(s->flags & SF_FINST_MASK))
3726 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003727
3728 return 0;
3729 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003730
3731 path = http_get_path(txn);
Christopher Faulet11ebb202018-04-13 15:53:12 +02003732 if (url2sa(req->buf->p + msg->sl.rq.u,
3733 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3734 &conn->addr.to, NULL) == -1)
3735 goto return_bad_req;
3736
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003737 /* if the path was found, we have to remove everything between
3738 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3739 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3740 * u_l characters by a single "/".
3741 */
3742 if (path) {
3743 char *cur_ptr = req->buf->p;
3744 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3745 int delta;
3746
3747 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3748 http_msg_move_end(&txn->req, delta);
3749 cur_end += delta;
3750 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3751 goto return_bad_req;
3752 }
3753 else {
3754 char *cur_ptr = req->buf->p;
3755 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3756 int delta;
3757
3758 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3759 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3760 http_msg_move_end(&txn->req, delta);
3761 cur_end += delta;
3762 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3763 goto return_bad_req;
3764 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003765 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003766
Willy Tarreau59234e92008-11-30 23:51:27 +01003767 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003768 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 * Note that doing so might move headers in the request, but
3770 * the fields will stay coherent and the URI will not move.
3771 * This should only be performed in the backend.
3772 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003773 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003774 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003775
William Lallemanda73203e2012-03-12 12:48:57 +01003776 /* add unique-id if "header-unique-id" is specified */
3777
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003778 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003779 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003780 goto return_bad_req;
3781 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003782 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003783 }
William Lallemanda73203e2012-03-12 12:48:57 +01003784
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003785 if (sess->fe->header_unique_id && s->unique_id) {
3786 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003787 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003788 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003789 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003790 goto return_bad_req;
3791 }
3792
Cyril Bontéb21570a2009-11-29 20:04:48 +01003793 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003794 * 9: add X-Forwarded-For if either the frontend or the backend
3795 * asks for it.
3796 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003797 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003798 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003799 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3800 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3801 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003802 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003803 /* The header is set to be added only if none is present
3804 * and we found it, so don't do anything.
3805 */
3806 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003807 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003808 /* Add an X-Forwarded-For header unless the source IP is
3809 * in the 'except' network range.
3810 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003811 if ((!sess->fe->except_mask.s_addr ||
3812 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3813 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003814 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003815 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003816 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003817 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003818 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003819 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003820
3821 /* Note: we rely on the backend to get the header name to be used for
3822 * x-forwarded-for, because the header is really meant for the backends.
3823 * However, if the backend did not specify any option, we have to rely
3824 * on the frontend's header name.
3825 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003826 if (s->be->fwdfor_hdr_len) {
3827 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003828 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003829 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003830 len = sess->fe->fwdfor_hdr_len;
3831 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003832 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003833 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003834
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003835 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003836 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003837 }
3838 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003839 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003840 /* FIXME: for the sake of completeness, we should also support
3841 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003842 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003843 int len;
3844 char pn[INET6_ADDRSTRLEN];
3845 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003846 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003847 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003848
Willy Tarreau59234e92008-11-30 23:51:27 +01003849 /* Note: we rely on the backend to get the header name to be used for
3850 * x-forwarded-for, because the header is really meant for the backends.
3851 * However, if the backend did not specify any option, we have to rely
3852 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003853 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003854 if (s->be->fwdfor_hdr_len) {
3855 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003856 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003857 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003858 len = sess->fe->fwdfor_hdr_len;
3859 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003860 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003861 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003862
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003863 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003864 goto return_bad_req;
3865 }
3866 }
3867
3868 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003869 * 10: add X-Original-To if either the frontend or the backend
3870 * asks for it.
3871 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003872 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003873
3874 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003875 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003876 /* Add an X-Original-To header unless the destination IP is
3877 * in the 'except' network range.
3878 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003879 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003880
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003881 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003882 ((!sess->fe->except_mask_to.s_addr ||
3883 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3884 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003885 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003886 (((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 +02003887 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003888 int len;
3889 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003890 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003891
3892 /* Note: we rely on the backend to get the header name to be used for
3893 * x-original-to, because the header is really meant for the backends.
3894 * However, if the backend did not specify any option, we have to rely
3895 * on the frontend's header name.
3896 */
3897 if (s->be->orgto_hdr_len) {
3898 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003899 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003900 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003901 len = sess->fe->orgto_hdr_len;
3902 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003903 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003904 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003905
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003906 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003907 goto return_bad_req;
3908 }
3909 }
3910 }
3911
Willy Tarreau50fc7772012-11-11 22:19:57 +01003912 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3913 * If an "Upgrade" token is found, the header is left untouched in order not to have
3914 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3915 * "Upgrade" is present in the Connection header.
3916 */
3917 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3918 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003919 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003920 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003921 unsigned int want_flags = 0;
3922
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003923 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003924 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003925 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003926 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003927 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003928 want_flags |= TX_CON_CLO_SET;
3929 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003930 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003931 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003932 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003933 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003934 want_flags |= TX_CON_KAL_SET;
3935 }
3936
3937 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003938 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003939 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003940
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003941
Willy Tarreau522d6c02009-12-06 18:49:18 +01003942 /* If we have no server assigned yet and we're balancing on url_param
3943 * with a POST request, we may be interested in checking the body for
3944 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003945 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003946 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003947 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003948 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003949 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003950 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003951 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003952
Christopher Fauletbe821b92017-03-30 11:21:53 +02003953 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3954 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003955#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003956 /* We expect some data from the client. Unless we know for sure
3957 * we already have a full request, we have to re-enable quick-ack
3958 * in case we previously disabled it, otherwise we might cause
3959 * the client to delay further data.
3960 */
3961 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3962 cli_conn && conn_ctrl_ready(cli_conn) &&
3963 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3964 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003965 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003966#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003967
Willy Tarreau59234e92008-11-30 23:51:27 +01003968 /*************************************************************
3969 * OK, that's finished for the headers. We have done what we *
3970 * could. Let's switch to the DATA state. *
3971 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003972 req->analyse_exp = TICK_ETERNITY;
3973 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003974
Willy Tarreau59234e92008-11-30 23:51:27 +01003975 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003976 /* OK let's go on with the BODY now */
3977 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003978
Willy Tarreau59234e92008-11-30 23:51:27 +01003979 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003980 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003981 /* we detected a parsing error. We want to archive this request
3982 * in the dedicated proxy area for later troubleshooting.
3983 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003984 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003985 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003986
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003987 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003988 txn->req.msg_state = HTTP_MSG_ERROR;
3989 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003990 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003991 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003992
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003993 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003994 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003995 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003996
Willy Tarreaue7dff022015-04-03 01:14:29 +02003997 if (!(s->flags & SF_ERR_MASK))
3998 s->flags |= SF_ERR_PRXCOND;
3999 if (!(s->flags & SF_FINST_MASK))
4000 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004001 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004002}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004003
Willy Tarreau60b85b02008-11-30 23:28:40 +01004004/* This function is an analyser which processes the HTTP tarpit. It always
4005 * returns zero, at the beginning because it prevents any other processing
4006 * from occurring, and at the end because it terminates the request.
4007 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004008int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004009{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004010 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004011
4012 /* This connection is being tarpitted. The CLIENT side has
4013 * already set the connect expiration date to the right
4014 * timeout. We just have to check that the client is still
4015 * there and that the timeout has not expired.
4016 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004017 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004018 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004019 !tick_is_expired(req->analyse_exp, now_ms))
4020 return 0;
4021
4022 /* We will set the queue timer to the time spent, just for
4023 * logging purposes. We fake a 500 server error, so that the
4024 * attacker will not suspect his connection has been tarpitted.
4025 * It will not cause trouble to the logs because we can exclude
4026 * the tarpitted connections by filtering on the 'PT' status flags.
4027 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004028 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4029
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004030 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004031 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004032
Christopher Faulet0184ea72017-01-05 14:06:34 +01004033 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004034 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004035
Willy Tarreaue7dff022015-04-03 01:14:29 +02004036 if (!(s->flags & SF_ERR_MASK))
4037 s->flags |= SF_ERR_PRXCOND;
4038 if (!(s->flags & SF_FINST_MASK))
4039 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004040 return 0;
4041}
4042
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004043/* This function is an analyser which waits for the HTTP request body. It waits
4044 * for either the buffer to be full, or the full advertised contents to have
4045 * reached the buffer. It must only be called after the standard HTTP request
4046 * processing has occurred, because it expects the request to be parsed and will
4047 * look for the Expect header. It may send a 100-Continue interim response. It
4048 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4049 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4050 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004051 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004052int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004053{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004054 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004055 struct http_txn *txn = s->txn;
4056 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004057
4058 /* We have to parse the HTTP request body to find any required data.
4059 * "balance url_param check_post" should have been the only way to get
4060 * into this. We were brought here after HTTP header analysis, so all
4061 * related structures are ready.
4062 */
4063
Willy Tarreau890988f2014-04-10 11:59:33 +02004064 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4065 /* This is the first call */
4066 if (msg->msg_state < HTTP_MSG_BODY)
4067 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004068
Willy Tarreau890988f2014-04-10 11:59:33 +02004069 if (msg->msg_state < HTTP_MSG_100_SENT) {
4070 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4071 * send an HTTP/1.1 100 Continue intermediate response.
4072 */
4073 if (msg->flags & HTTP_MSGF_VER_11) {
4074 struct hdr_ctx ctx;
4075 ctx.idx = 0;
4076 /* Expect is allowed in 1.1, look for it */
4077 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4078 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004079 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004080 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004081 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004082 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004083 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004084 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004085
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004086 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004087 * req->buf->p still points to the beginning of the message. We
4088 * must save the body in msg->next because it survives buffer
4089 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004090 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004091 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004092
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004093 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004094 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4095 else
4096 msg->msg_state = HTTP_MSG_DATA;
4097 }
4098
Willy Tarreau890988f2014-04-10 11:59:33 +02004099 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4100 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004101 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004102 goto missing_data;
4103
4104 /* OK we have everything we need now */
4105 goto http_end;
4106 }
4107
4108 /* OK here we're parsing a chunked-encoded message */
4109
Willy Tarreau522d6c02009-12-06 18:49:18 +01004110 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004111 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004112 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004113 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004114 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004115 unsigned int chunk;
4116 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004117
Willy Tarreau115acb92009-12-26 13:56:06 +01004118 if (!ret)
4119 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004120 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004121 msg->err_pos = req->buf->i + ret;
4122 if (msg->err_pos < 0)
4123 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004124 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004125 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004126 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004127
4128 msg->chunk_len = chunk;
4129 msg->body_len += chunk;
4130
4131 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004132 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004133 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004134 }
4135
Willy Tarreaud98cf932009-12-27 22:54:55 +01004136 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004137 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4138 * for at least a whole chunk or the whole content length bytes after
4139 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004140 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004141 if (msg->msg_state == HTTP_MSG_TRAILERS)
4142 goto http_end;
4143
Willy Tarreaue115b492015-05-01 23:05:14 +02004144 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004145 goto http_end;
4146
4147 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004148 /* we get here if we need to wait for more data. If the buffer is full,
4149 * we have the maximum we can expect.
4150 */
4151 if (buffer_full(req->buf, global.tune.maxrewrite))
4152 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004153
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004154 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004155 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004156 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004157
Willy Tarreaue7dff022015-04-03 01:14:29 +02004158 if (!(s->flags & SF_ERR_MASK))
4159 s->flags |= SF_ERR_CLITO;
4160 if (!(s->flags & SF_FINST_MASK))
4161 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004162 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004163 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004164
4165 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004166 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004167 /* Not enough data. We'll re-use the http-request
4168 * timeout here. Ideally, we should set the timeout
4169 * relative to the accept() date. We just set the
4170 * request timeout once at the beginning of the
4171 * request.
4172 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004173 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004174 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004175 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004176 return 0;
4177 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004178
4179 http_end:
4180 /* The situation will not evolve, so let's give up on the analysis. */
4181 s->logs.tv_request = now; /* update the request timer to reflect full request */
4182 req->analysers &= ~an_bit;
4183 req->analyse_exp = TICK_ETERNITY;
4184 return 1;
4185
4186 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004187 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004188 txn->req.msg_state = HTTP_MSG_ERROR;
4189 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004190 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004191
Willy Tarreaue7dff022015-04-03 01:14:29 +02004192 if (!(s->flags & SF_ERR_MASK))
4193 s->flags |= SF_ERR_PRXCOND;
4194 if (!(s->flags & SF_FINST_MASK))
4195 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004196
Willy Tarreau522d6c02009-12-06 18:49:18 +01004197 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004198 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004199 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004200 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004201 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004202 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004203}
4204
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004205/* send a server's name with an outgoing request over an established connection.
4206 * Note: this function is designed to be called once the request has been scheduled
4207 * for being forwarded. This is the reason why it rewinds the buffer before
4208 * proceeding.
4209 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004210int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004211
4212 struct hdr_ctx ctx;
4213
Mark Lamourinec2247f02012-01-04 13:02:01 -05004214 char *hdr_name = be->server_id_hdr_name;
4215 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004216 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004217 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004218 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004219
William Lallemandd9e90662012-01-30 17:27:17 +01004220 ctx.idx = 0;
4221
Willy Tarreau211cdec2014-04-17 20:18:08 +02004222 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004223 if (old_o) {
4224 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004225 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004226 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004227 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004228 }
4229
Willy Tarreau9b28e032012-10-12 23:49:43 +02004230 old_i = chn->buf->i;
4231 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004232 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004233 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004234 }
4235
4236 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004237 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004238 memcpy(hdr_val, hdr_name, hdr_name_len);
4239 hdr_val += hdr_name_len;
4240 *hdr_val++ = ':';
4241 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004242 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4243 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004244
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004245 if (old_o) {
4246 /* If this was a forwarded request, we must readjust the amount of
4247 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004248 * variations. Note that the current state is >= HTTP_MSG_BODY,
4249 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004250 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004251 old_o += chn->buf->i - old_i;
4252 b_adv(chn->buf, old_o);
4253 txn->req.next -= old_o;
4254 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004255 }
4256
Mark Lamourinec2247f02012-01-04 13:02:01 -05004257 return 0;
4258}
4259
Willy Tarreau610ecce2010-01-04 21:15:02 +01004260/* Terminate current transaction and prepare a new one. This is very tricky
4261 * right now but it works.
4262 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004263void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004264{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004265 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004266 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004267 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004268 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004269 struct connection *srv_conn;
4270 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004271 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004272
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273 /* FIXME: We need a more portable way of releasing a backend's and a
4274 * server's connections. We need a safer way to reinitialize buffer
4275 * flags. We also need a more accurate method for computing per-request
4276 * data.
4277 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004278 /*
4279 * XXX cognet: This is probably wrong, this is killing a whole
4280 * connection, in the new world order, we probably want to just kill
4281 * the stream, this is to be revisited the day we handle multiple
4282 * streams in one server connection.
4283 */
4284 cs = objt_cs(s->si[1].end);
4285 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286
Willy Tarreau4213a112013-12-15 10:25:42 +01004287 /* unless we're doing keep-alive, we want to quickly close the connection
4288 * to the server.
4289 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004290 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004291 !si_conn_ready(&s->si[1])) {
4292 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4293 si_shutr(&s->si[1]);
4294 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004295 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296
Willy Tarreaue7dff022015-04-03 01:14:29 +02004297 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004298 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004299 if (unlikely(s->srv_conn))
4300 sess_change_server(s, NULL);
4301 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302
4303 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004304 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004305
Willy Tarreaueee5b512015-04-03 23:46:31 +02004306 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307 int n;
4308
Willy Tarreaueee5b512015-04-03 23:46:31 +02004309 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004310 if (n < 1 || n > 5)
4311 n = 0;
4312
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004313 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004314 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004315 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004316 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004317 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004318 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4319 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004320 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004321 }
4322
4323 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004324 s->logs.bytes_in -= s->req.buf->i;
4325 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004326
4327 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004328 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004329 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004330 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 s->do_log(s);
4332 }
4333
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004334 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004335 stream_stop_content_counters(s);
4336 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004337
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338 s->logs.accept_date = date; /* user-visible date for logging */
4339 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004340 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4341 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 tv_zero(&s->logs.tv_request);
4343 s->logs.t_queue = -1;
4344 s->logs.t_connect = -1;
4345 s->logs.t_data = -1;
4346 s->logs.t_close = 0;
4347 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4348 s->logs.srv_queue_size = 0; /* we will get this number soon */
4349
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004350 s->logs.bytes_in = s->req.total = s->req.buf->i;
4351 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352
4353 if (s->pend_pos)
4354 pendconn_free(s->pend_pos);
4355
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004356 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004357 if (s->flags & SF_CURR_SESS) {
4358 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004359 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004361 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004362 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363 }
4364
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004365 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366
Willy Tarreau4213a112013-12-15 10:25:42 +01004367 /* only release our endpoint if we don't intend to reuse the
4368 * connection.
4369 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004370 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004371 !si_conn_ready(&s->si[1])) {
4372 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004373 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004374 }
4375
Willy Tarreau350f4872014-11-28 14:42:25 +01004376 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4377 s->si[1].err_type = SI_ET_NONE;
4378 s->si[1].conn_retries = 0; /* used for logging too */
4379 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004380 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 +01004381 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 +01004382 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 +02004383 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4384 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4385 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004386
Willy Tarreaueee5b512015-04-03 23:46:31 +02004387 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004388 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004389 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004390
4391 if (prev_status == 401 || prev_status == 407) {
4392 /* In HTTP keep-alive mode, if we receive a 401, we still have
4393 * a chance of being able to send the visitor again to the same
4394 * server over the same connection. This is required by some
4395 * broken protocols such as NTLM, and anyway whenever there is
4396 * an opportunity for sending the challenge to the proper place,
4397 * it's better to do it (at least it helps with debugging).
4398 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004399 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004400 if (srv_conn)
4401 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004402 }
4403
Willy Tarreau53f96852016-02-02 18:50:47 +01004404 /* Never ever allow to reuse a connection from a non-reuse backend */
4405 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4406 srv_conn->flags |= CO_FL_PRIVATE;
4407
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004408 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004409 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004411 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004412 s->req.flags |= CF_NEVER_WAIT;
4413 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004414 }
4415
Willy Tarreau714ea782015-11-25 20:11:11 +01004416 /* we're removing the analysers, we MUST re-enable events detection.
4417 * We don't enable close on the response channel since it's either
4418 * already closed, or in keep-alive with an idle connection handler.
4419 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004420 channel_auto_read(&s->req);
4421 channel_auto_close(&s->req);
4422 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004423
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004424 /* we're in keep-alive with an idle connection, monitor it if not already done */
4425 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004426 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004427 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004428 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004429 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004430 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004431 else if (prev_flags & TX_NOT_FIRST)
4432 /* note: we check the request, not the connection, but
4433 * this is valid for strategies SAFE and AGGR, and in
4434 * case of ALWS, we don't care anyway.
4435 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004436 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004437 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004438 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004439 }
Christopher Faulete6006242017-03-10 11:52:44 +01004440 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4441 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442}
4443
4444
4445/* This function updates the request state machine according to the response
4446 * state machine and buffer flags. It returns 1 if it changes anything (flag
4447 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4448 * it is only used to find when a request/response couple is complete. Both
4449 * this function and its equivalent should loop until both return zero. It
4450 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4451 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004452int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004453{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004454 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004455 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004456 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004457 unsigned int old_state = txn->req.msg_state;
4458
Christopher Faulet4be98032017-07-18 10:48:24 +02004459 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004460 return 0;
4461
4462 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004463 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004464 * We can shut the read side unless we want to abort_on_close,
4465 * or we have a POST request. The issue with POST requests is
4466 * that some browsers still send a CRLF after the request, and
4467 * this CRLF must be read so that it does not remain in the kernel
4468 * buffers, otherwise a close could cause an RST on some systems
4469 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004470 * Note that if we're using keep-alive on the client side, we'd
4471 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004472 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004473 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004474 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004475 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4476 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004477 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4478 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004479 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004480 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481
Willy Tarreau40f151a2012-12-20 12:10:09 +01004482 /* if the server closes the connection, we want to immediately react
4483 * and close the socket to save packets and syscalls.
4484 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004485 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004486
Willy Tarreau7f876a12015-11-18 11:59:55 +01004487 /* In any case we've finished parsing the request so we must
4488 * disable Nagle when sending data because 1) we're not going
4489 * to shut this side, and 2) the server is waiting for us to
4490 * send pending data.
4491 */
4492 chn->flags |= CF_NEVER_WAIT;
4493
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4495 goto wait_other_side;
4496
4497 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4498 /* The server has not finished to respond, so we
4499 * don't want to move in order not to upset it.
4500 */
4501 goto wait_other_side;
4502 }
4503
Willy Tarreau610ecce2010-01-04 21:15:02 +01004504 /* When we get here, it means that both the request and the
4505 * response have finished receiving. Depending on the connection
4506 * mode, we'll have to wait for the last bytes to leave in either
4507 * direction, and sometimes for a close to be effective.
4508 */
4509
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004510 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4511 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004512 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4513 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004514 }
4515 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4516 /* Option forceclose is set, or either side wants to close,
4517 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004518 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004519 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004520 *
4521 * However, there is an exception if the response
4522 * length is undefined. In this case, we need to wait
4523 * the close from the server. The response will be
4524 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004525 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004526 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4527 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4528 goto check_channel_flags;
4529
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004530 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4531 channel_shutr_now(chn);
4532 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004534 }
4535 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004536 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4537 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004538 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004539 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4540 channel_auto_read(chn);
4541 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004542 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004543 }
4544
Christopher Faulet4be98032017-07-18 10:48:24 +02004545 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004546 }
4547
4548 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4549 http_msg_closing:
4550 /* nothing else to forward, just waiting for the output buffer
4551 * to be empty and for the shutw_now to take effect.
4552 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004553 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554 txn->req.msg_state = HTTP_MSG_CLOSED;
4555 goto http_msg_closed;
4556 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004557 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004558 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004560 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004561 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562 }
4563
4564 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4565 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004566 /* if we don't know whether the server will close, we need to hard close */
4567 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4568 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4569
Willy Tarreau3988d932013-12-27 23:03:08 +01004570 /* see above in MSG_DONE why we only do this in these states */
4571 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4572 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004573 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4574 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004575 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 goto wait_other_side;
4577 }
4578
Christopher Faulet4be98032017-07-18 10:48:24 +02004579 check_channel_flags:
4580 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4581 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4582 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004583 txn->req.msg_state = HTTP_MSG_CLOSING;
4584 goto http_msg_closing;
4585 }
4586
4587
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004589 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590}
4591
4592
4593/* This function updates the response state machine according to the request
4594 * state machine and buffer flags. It returns 1 if it changes anything (flag
4595 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4596 * it is only used to find when a request/response couple is complete. Both
4597 * this function and its equivalent should loop until both return zero. It
4598 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4599 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004600int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004601{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004602 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004603 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004604 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605 unsigned int old_state = txn->rsp.msg_state;
4606
Christopher Faulet4be98032017-07-18 10:48:24 +02004607 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004608 return 0;
4609
4610 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4611 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004612 * still monitor the server connection for a possible close
4613 * while the request is being uploaded, so we don't disable
4614 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004616 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617
4618 if (txn->req.msg_state == HTTP_MSG_ERROR)
4619 goto wait_other_side;
4620
4621 if (txn->req.msg_state < HTTP_MSG_DONE) {
4622 /* The client seems to still be sending data, probably
4623 * because we got an error response during an upload.
4624 * We have the choice of either breaking the connection
4625 * or letting it pass through. Let's do the later.
4626 */
4627 goto wait_other_side;
4628 }
4629
Willy Tarreau610ecce2010-01-04 21:15:02 +01004630 /* When we get here, it means that both the request and the
4631 * response have finished receiving. Depending on the connection
4632 * mode, we'll have to wait for the last bytes to leave in either
4633 * direction, and sometimes for a close to be effective.
4634 */
4635
4636 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4637 /* Server-close mode : shut read and wait for the request
4638 * side to close its output buffer. The caller will detect
4639 * when we're in DONE and the other is in CLOSED and will
4640 * catch that for the final cleanup.
4641 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004642 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4643 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004644 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004645 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4646 /* Option forceclose is set, or either side wants to close,
4647 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004648 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004649 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004650 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004651 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004652 channel_shutr_now(chn);
4653 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004654 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004655 }
4656 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004657 /* The last possible modes are keep-alive and tunnel. Tunnel will
4658 * need to forward remaining data. Keep-alive will need to monitor
4659 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004661 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004662 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004663 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4664 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004665 }
4666
Christopher Faulet4be98032017-07-18 10:48:24 +02004667 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004668 }
4669
4670 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4671 http_msg_closing:
4672 /* nothing else to forward, just waiting for the output buffer
4673 * to be empty and for the shutw_now to take effect.
4674 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004675 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4677 goto http_msg_closed;
4678 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004679 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004680 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004682 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004683 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004684 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004686 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687 }
4688
4689 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4690 http_msg_closed:
4691 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004692 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004693 channel_auto_close(chn);
4694 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004695 goto wait_other_side;
4696 }
4697
Christopher Faulet4be98032017-07-18 10:48:24 +02004698 check_channel_flags:
4699 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4700 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4701 /* if we've just closed an output, let's switch */
4702 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4703 goto http_msg_closing;
4704 }
4705
Willy Tarreau610ecce2010-01-04 21:15:02 +01004706 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004707 /* We force the response to leave immediately if we're waiting for the
4708 * other side, since there is no pending shutdown to push it out.
4709 */
4710 if (!channel_is_empty(chn))
4711 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004712 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004713}
4714
4715
Christopher Faulet894da4c2017-07-18 11:29:07 +02004716/* Resync the request and response state machines. */
4717void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004718{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004719 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004720#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004721 int old_req_state = txn->req.msg_state;
4722 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004723#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004724
Willy Tarreau610ecce2010-01-04 21:15:02 +01004725 http_sync_req_state(s);
4726 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004727 if (!http_sync_res_state(s))
4728 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 if (!http_sync_req_state(s))
4730 break;
4731 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004732
Christopher Faulet894da4c2017-07-18 11:29:07 +02004733 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4734 "req->analysers=0x%08x res->analysers=0x%08x\n",
4735 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004736 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4737 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004738 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004739
4740
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741 /* OK, both state machines agree on a compatible state.
4742 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4744 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004745 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4746 * means we must abort the request.
4747 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4748 * corresponding channel.
4749 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4750 * on the response with server-close mode means we've completed one
4751 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004752 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004753 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4754 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004755 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004756 channel_auto_close(&s->req);
4757 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004758 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004759 channel_auto_close(&s->res);
4760 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004761 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004762 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4763 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004764 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004765 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004766 channel_auto_close(&s->res);
4767 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004768 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004769 channel_abort(&s->req);
4770 channel_auto_close(&s->req);
4771 channel_auto_read(&s->req);
4772 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004774 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4775 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4776 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4777 s->req.analysers &= AN_REQ_FLT_END;
4778 if (HAS_REQ_DATA_FILTERS(s))
4779 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4780 }
4781 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4782 s->res.analysers &= AN_RES_FLT_END;
4783 if (HAS_RSP_DATA_FILTERS(s))
4784 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4785 }
4786 channel_auto_close(&s->req);
4787 channel_auto_read(&s->req);
4788 channel_auto_close(&s->res);
4789 channel_auto_read(&s->res);
4790 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004791 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4792 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004794 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4795 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4796 /* server-close/keep-alive: terminate this transaction,
4797 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004798 * a fresh-new transaction, but only once we're sure there's
4799 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004800 * another request. They must not hold any pending output data
4801 * and the response buffer must realigned
4802 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004803 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004804 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004805 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004806 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004807 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004808 else {
4809 s->req.analysers = AN_REQ_FLT_END;
4810 s->res.analysers = AN_RES_FLT_END;
4811 txn->flags |= TX_WAIT_CLEANUP;
4812 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004814}
4815
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816/* This function is an analyser which forwards request body (including chunk
4817 * sizes if any). It is called as soon as we must forward, even if we forward
4818 * zero byte. The only situation where it must not be called is when we're in
4819 * tunnel mode and we want to forward till the close. It's used both to forward
4820 * remaining data and to resync after end of body. It expects the msg_state to
4821 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004822 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004823 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004824 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004825 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004826int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004827{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004828 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004829 struct http_txn *txn = s->txn;
4830 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004831 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004832
Christopher Faulet814d2702017-03-30 11:33:44 +02004833 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4834 now_ms, __FUNCTION__,
4835 s,
4836 req,
4837 req->rex, req->wex,
4838 req->flags,
4839 req->buf->i,
4840 req->analysers);
4841
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004842 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4843 return 0;
4844
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004845 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004846 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004847 /* Output closed while we were sending data. We must abort and
4848 * wake the other side up.
4849 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004850 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004851 msg->msg_state = HTTP_MSG_ERROR;
4852 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004853 return 1;
4854 }
4855
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856 /* Note that we don't have to send 100-continue back because we don't
4857 * need the data to complete our job, and it's up to the server to
4858 * decide whether to return 100, 417 or anything else in return of
4859 * an "Expect: 100-continue" header.
4860 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004861 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004862 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4863 ? HTTP_MSG_CHUNK_SIZE
4864 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004865
4866 /* TODO/filters: when http-buffer-request option is set or if a
4867 * rule on url_param exists, the first chunk size could be
4868 * already parsed. In that case, msg->next is after the chunk
4869 * size (including the CRLF after the size). So this case should
4870 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004871 }
4872
Willy Tarreau7ba23542014-04-17 21:50:00 +02004873 /* Some post-connect processing might want us to refrain from starting to
4874 * forward data. Currently, the only reason for this is "balance url_param"
4875 * whichs need to parse/process the request after we've enabled forwarding.
4876 */
4877 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004878 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004879 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004880 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004881 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004882 }
4883 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4884 }
4885
Willy Tarreau80a92c02014-03-12 10:41:13 +01004886 /* in most states, we should abort in case of early close */
4887 channel_auto_close(req);
4888
Willy Tarreauefdf0942014-04-24 20:08:57 +02004889 if (req->to_forward) {
4890 /* We can't process the buffer's contents yet */
4891 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004892 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004893 }
4894
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004895 if (msg->msg_state < HTTP_MSG_DONE) {
4896 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4897 ? http_msg_forward_chunked_body(s, msg)
4898 : http_msg_forward_body(s, msg));
4899 if (!ret)
4900 goto missing_data_or_waiting;
4901 if (ret < 0)
4902 goto return_bad_req;
4903 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004904
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004905 /* other states, DONE...TUNNEL */
4906 /* we don't want to forward closes on DONE except in tunnel mode. */
4907 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4908 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004909
Christopher Faulet894da4c2017-07-18 11:29:07 +02004910 http_resync_states(s);
4911 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004912 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4913 if (req->flags & CF_SHUTW) {
4914 /* request errors are most likely due to the
4915 * server aborting the transfer. */
4916 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004917 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004918 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004919 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004920 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004921 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004922 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004923 }
4924
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004925 /* If "option abortonclose" is set on the backend, we want to monitor
4926 * the client's connection and forward any shutdown notification to the
4927 * server, which will decide whether to close or to go on processing the
4928 * request. We only do that in tunnel mode, and not in other modes since
4929 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004930 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004931 channel_auto_read(req);
4932 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4933 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4934 s->si[1].flags |= SI_FL_NOLINGER;
4935 channel_auto_close(req);
4936 }
4937 else if (s->txn->meth == HTTP_METH_POST) {
4938 /* POST requests may require to read extra CRLF sent by broken
4939 * browsers and which could cause an RST to be sent upon close
4940 * on some systems (eg: Linux). */
4941 channel_auto_read(req);
4942 }
4943 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004944
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004945 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004946 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004947 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004948 if (!(s->flags & SF_ERR_MASK))
4949 s->flags |= SF_ERR_CLICL;
4950 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004951 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004952 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004953 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004954 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004955 }
4956
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004957 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4958 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004959 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004960 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004961
4962 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004963 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004964
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004965 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004966 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004967 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004968
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004969 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004970 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004971 * And when content-length is used, we never want to let the possible
4972 * shutdown be forwarded to the other side, as the state machine will
4973 * take care of it once the client responds. It's also important to
4974 * prevent TIME_WAITs from accumulating on the backend side, and for
4975 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004976 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004977 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004978 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004979
Willy Tarreau5c620922011-05-11 19:56:11 +02004980 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004981 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004982 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004983 * modes are already handled by the stream sock layer. We must not do
4984 * this in content-length mode because it could present the MSG_MORE
4985 * flag with the last block of forwarded data, which would cause an
4986 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004987 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004988 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004989 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004990
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004991 return 0;
4992
Willy Tarreaud98cf932009-12-27 22:54:55 +01004993 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004994 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004995 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004996 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004997
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004998 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004999 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005000 txn->req.msg_state = HTTP_MSG_ERROR;
5001 if (txn->status) {
5002 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005003 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005004 } else {
5005 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005006 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005007 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005008 req->analysers &= AN_REQ_FLT_END;
5009 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 +01005010
Willy Tarreaue7dff022015-04-03 01:14:29 +02005011 if (!(s->flags & SF_ERR_MASK))
5012 s->flags |= SF_ERR_PRXCOND;
5013 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005014 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005015 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005016 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005017 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005018 }
5019 return 0;
5020
5021 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005022 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005023 txn->req.msg_state = HTTP_MSG_ERROR;
5024 if (txn->status) {
5025 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005026 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005027 } else {
5028 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005029 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005030 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005031 req->analysers &= AN_REQ_FLT_END;
5032 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005033
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005034 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5035 HA_ATOMIC_ADD(&s->be->be_counters.srv_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.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005038
Willy Tarreaue7dff022015-04-03 01:14:29 +02005039 if (!(s->flags & SF_ERR_MASK))
5040 s->flags |= SF_ERR_SRVCL;
5041 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005042 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005043 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005044 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005045 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005046 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005047 return 0;
5048}
5049
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005050/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5051 * processing can continue on next analysers, or zero if it either needs more
5052 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005053 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 * when it has nothing left to do, and may remove any analyser when it wants to
5055 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005056 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005057int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005058{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005059 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005060 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005061 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005062 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005063 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005064 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005065 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005066
Willy Tarreau87b09662015-04-03 00:22:06 +02005067 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005068 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005069 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005070 rep,
5071 rep->rex, rep->wex,
5072 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005073 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005074 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005075
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005076 /*
5077 * Now parse the partial (or complete) lines.
5078 * We will check the response syntax, and also join multi-line
5079 * headers. An index of all the lines will be elaborated while
5080 * parsing.
5081 *
5082 * For the parsing, we use a 28 states FSM.
5083 *
5084 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005085 * rep->buf->p = beginning of response
5086 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5087 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005088 * msg->eol = end of current header or line (LF or CRLF)
5089 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005090 */
5091
Willy Tarreau628c40c2014-04-24 19:11:26 +02005092 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005093 /* There's a protected area at the end of the buffer for rewriting
5094 * purposes. We don't want to start to parse the request if the
5095 * protected area is affected, because we may have to move processed
5096 * data later, which is much more complicated.
5097 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005098 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005099 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005100 /* some data has still not left the buffer, wake us once that's done */
5101 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5102 goto abort_response;
5103 channel_dont_close(rep);
5104 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005105 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005106 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005107 }
5108
Willy Tarreau379357a2013-06-08 12:55:46 +02005109 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5110 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5111 buffer_slow_realign(rep->buf);
5112
Willy Tarreau9b28e032012-10-12 23:49:43 +02005113 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005114 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005115 }
5116
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005117 /* 1: we might have to print this header in debug mode */
5118 if (unlikely((global.mode & MODE_DEBUG) &&
5119 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005120 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005122
Willy Tarreau9b28e032012-10-12 23:49:43 +02005123 sol = rep->buf->p;
5124 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005125 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005126
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005127 sol += hdr_idx_first_pos(&txn->hdr_idx);
5128 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005129
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005130 while (cur_idx) {
5131 eol = sol + txn->hdr_idx.v[cur_idx].len;
5132 debug_hdr("srvhdr", s, sol, eol);
5133 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5134 cur_idx = txn->hdr_idx.v[cur_idx].next;
5135 }
5136 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005137
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138 /*
5139 * Now we quickly check if we have found a full valid response.
5140 * If not so, we check the FD and buffer states before leaving.
5141 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005142 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 * responses are checked first.
5144 *
5145 * Depending on whether the client is still there or not, we
5146 * may send an error response back or not. Note that normally
5147 * we should only check for HTTP status there, and check I/O
5148 * errors somewhere else.
5149 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150
Willy Tarreau655dce92009-11-08 13:10:58 +01005151 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005152 /* Invalid response */
5153 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5154 /* we detected a parsing error. We want to archive this response
5155 * in the dedicated proxy area for later troubleshooting.
5156 */
5157 hdr_response_bad:
5158 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005159 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005160
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005161 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005162 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005163 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005164 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005165 }
Willy Tarreau64648412010-03-05 10:41:54 +01005166 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005167 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005168 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005169 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005170 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005171 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005172 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005173
Willy Tarreaue7dff022015-04-03 01:14:29 +02005174 if (!(s->flags & SF_ERR_MASK))
5175 s->flags |= SF_ERR_PRXCOND;
5176 if (!(s->flags & SF_FINST_MASK))
5177 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178
5179 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005180 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005181
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005183 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005184 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005185 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005186 goto hdr_response_bad;
5187 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005189 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005190 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005192 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005193 else if (txn->flags & TX_NOT_FIRST)
5194 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005195
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005196 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005197 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005198 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005199 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005200 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005201
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005202 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005203 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005204 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005205
5206 /* Check to see if the server refused the early data.
5207 * If so, just send a 425
5208 */
5209 if (objt_cs(s->si[1].end)) {
5210 struct connection *conn = objt_cs(s->si[1].end)->conn;
5211
5212 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5213 txn->status = 425;
5214 }
5215
Willy Tarreau350f4872014-11-28 14:42:25 +01005216 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005217 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005218 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005219
Willy Tarreaue7dff022015-04-03 01:14:29 +02005220 if (!(s->flags & SF_ERR_MASK))
5221 s->flags |= SF_ERR_SRVCL;
5222 if (!(s->flags & SF_FINST_MASK))
5223 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005224 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005225 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005226
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005227 /* read timeout : return a 504 to the client. */
5228 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005229 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005230 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005231
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005232 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005233 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005234 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005235 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005236 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005237
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005238 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005239 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005240 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005241 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005242 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005243 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005244
Willy Tarreaue7dff022015-04-03 01:14:29 +02005245 if (!(s->flags & SF_ERR_MASK))
5246 s->flags |= SF_ERR_SRVTO;
5247 if (!(s->flags & SF_FINST_MASK))
5248 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 return 0;
5250 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005251
Willy Tarreauf003d372012-11-26 13:35:37 +01005252 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005253 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005254 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5255 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005256 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005257 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005258
Christopher Faulet0184ea72017-01-05 14:06:34 +01005259 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005260 channel_auto_close(rep);
5261
5262 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005263 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005264 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005265
Willy Tarreaue7dff022015-04-03 01:14:29 +02005266 if (!(s->flags & SF_ERR_MASK))
5267 s->flags |= SF_ERR_CLICL;
5268 if (!(s->flags & SF_FINST_MASK))
5269 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005270
Willy Tarreau87b09662015-04-03 00:22:06 +02005271 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005272 return 0;
5273 }
5274
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005275 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005276 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005277 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005278 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005279 else if (txn->flags & TX_NOT_FIRST)
5280 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005281
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005282 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005283 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005284 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005285 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005286 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005287
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005288 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005289 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005291 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005292 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005293 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005294
Willy Tarreaue7dff022015-04-03 01:14:29 +02005295 if (!(s->flags & SF_ERR_MASK))
5296 s->flags |= SF_ERR_SRVCL;
5297 if (!(s->flags & SF_FINST_MASK))
5298 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005299 return 0;
5300 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005301
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005302 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005303 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005304 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005305 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005306 else if (txn->flags & TX_NOT_FIRST)
5307 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005308
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005309 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005310 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005311 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005312
Willy Tarreaue7dff022015-04-03 01:14:29 +02005313 if (!(s->flags & SF_ERR_MASK))
5314 s->flags |= SF_ERR_CLICL;
5315 if (!(s->flags & SF_FINST_MASK))
5316 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005317
Willy Tarreau87b09662015-04-03 00:22:06 +02005318 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005319 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005320 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005321
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005322 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005323 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005324 return 0;
5325 }
5326
5327 /* More interesting part now : we know that we have a complete
5328 * response which at least looks like HTTP. We have an indicator
5329 * of each header's length, so we can parse them quickly.
5330 */
5331
5332 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005333 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005334
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335 /*
5336 * 1: get the status code
5337 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005338 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005339 if (n < 1 || n > 5)
5340 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005341 /* when the client triggers a 4xx from the server, it's most often due
5342 * to a missing object or permission. These events should be tracked
5343 * because if they happen often, it may indicate a brute force or a
5344 * vulnerability scan.
5345 */
5346 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005347 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005348
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005349 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005350 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005351
Willy Tarreau91852eb2015-05-01 13:26:00 +02005352 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5353 * exactly one digit "." one digit. This check may be disabled using
5354 * option accept-invalid-http-response.
5355 */
5356 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5357 if (msg->sl.st.v_l != 8) {
5358 msg->err_pos = 0;
5359 goto hdr_response_bad;
5360 }
5361
5362 if (rep->buf->p[4] != '/' ||
5363 !isdigit((unsigned char)rep->buf->p[5]) ||
5364 rep->buf->p[6] != '.' ||
5365 !isdigit((unsigned char)rep->buf->p[7])) {
5366 msg->err_pos = 4;
5367 goto hdr_response_bad;
5368 }
5369 }
5370
Willy Tarreau5b154472009-12-21 20:11:07 +01005371 /* check if the response is HTTP/1.1 or above */
5372 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005373 ((rep->buf->p[5] > '1') ||
5374 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005375 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005376
5377 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005378 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 +01005379
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005380 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005381 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005382
Willy Tarreau9b28e032012-10-12 23:49:43 +02005383 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005384
Willy Tarreau39650402010-03-15 19:44:39 +01005385 /* Adjust server's health based on status code. Note: status codes 501
5386 * and 505 are triggered on demand by client request, so we must not
5387 * count them as server failures.
5388 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005389 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005390 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005391 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005392 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005393 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005394 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005395
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005396 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005397 * We may be facing a 100-continue response, or any other informational
5398 * 1xx response which is non-final, in which case this is not the right
5399 * response, and we're waiting for the next one. Let's allow this response
5400 * to go to the client and wait for the next one. There's an exception for
5401 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005402 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005403 if (txn->status < 200 &&
5404 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005405 hdr_idx_init(&txn->hdr_idx);
5406 msg->next -= channel_forward(rep, msg->next);
5407 msg->msg_state = HTTP_MSG_RPBEFORE;
5408 txn->status = 0;
5409 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005410 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005411 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005412 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005413
Willy Tarreaua14ad722017-07-07 11:36:32 +02005414 /*
5415 * 2: check for cacheability.
5416 */
5417
5418 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419 case 200:
5420 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005421 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005422 case 206:
5423 case 300:
5424 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005425 case 404:
5426 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005427 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005428 case 414:
5429 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005430 break;
5431 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005432 /* RFC7231#6.1:
5433 * Responses with status codes that are defined as
5434 * cacheable by default (e.g., 200, 203, 204, 206,
5435 * 300, 301, 404, 405, 410, 414, and 501 in this
5436 * specification) can be reused by a cache with
5437 * heuristic expiration unless otherwise indicated
5438 * by the method definition or explicit cache
5439 * controls [RFC7234]; all other status codes are
5440 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005441 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005442 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005443 break;
5444 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005445
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005446 /*
5447 * 3: we may need to capture headers
5448 */
5449 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005450 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005451 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005452 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005453
Willy Tarreau557f1992015-05-01 10:05:17 +02005454 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5455 * by RFC7230#3.3.3 :
5456 *
5457 * The length of a message body is determined by one of the following
5458 * (in order of precedence):
5459 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005460 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5461 * the connection will become a tunnel immediately after the empty
5462 * line that concludes the header fields. A client MUST ignore
5463 * any Content-Length or Transfer-Encoding header fields received
5464 * in such a message. Any 101 response (Switching Protocols) is
5465 * managed in the same manner.
5466 *
5467 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005468 * (Informational), 204 (No Content), or 304 (Not Modified) status
5469 * code is always terminated by the first empty line after the
5470 * header fields, regardless of the header fields present in the
5471 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005472 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005473 * 3. If a Transfer-Encoding header field is present and the chunked
5474 * transfer coding (Section 4.1) is the final encoding, the message
5475 * body length is determined by reading and decoding the chunked
5476 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005477 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005478 * If a Transfer-Encoding header field is present in a response and
5479 * the chunked transfer coding is not the final encoding, the
5480 * message body length is determined by reading the connection until
5481 * it is closed by the server. If a Transfer-Encoding header field
5482 * is present in a request and the chunked transfer coding is not
5483 * the final encoding, the message body length cannot be determined
5484 * reliably; the server MUST respond with the 400 (Bad Request)
5485 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005486 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005487 * If a message is received with both a Transfer-Encoding and a
5488 * Content-Length header field, the Transfer-Encoding overrides the
5489 * Content-Length. Such a message might indicate an attempt to
5490 * perform request smuggling (Section 9.5) or response splitting
5491 * (Section 9.4) and ought to be handled as an error. A sender MUST
5492 * remove the received Content-Length field prior to forwarding such
5493 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005494 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005495 * 4. If a message is received without Transfer-Encoding and with
5496 * either multiple Content-Length header fields having differing
5497 * field-values or a single Content-Length header field having an
5498 * invalid value, then the message framing is invalid and the
5499 * recipient MUST treat it as an unrecoverable error. If this is a
5500 * request message, the server MUST respond with a 400 (Bad Request)
5501 * status code and then close the connection. If this is a response
5502 * message received by a proxy, the proxy MUST close the connection
5503 * to the server, discard the received response, and send a 502 (Bad
5504 * Gateway) response to the client. If this is a response message
5505 * received by a user agent, the user agent MUST close the
5506 * connection to the server and discard the received response.
5507 *
5508 * 5. If a valid Content-Length header field is present without
5509 * Transfer-Encoding, its decimal value defines the expected message
5510 * body length in octets. If the sender closes the connection or
5511 * the recipient times out before the indicated number of octets are
5512 * received, the recipient MUST consider the message to be
5513 * incomplete and close the connection.
5514 *
5515 * 6. If this is a request message and none of the above are true, then
5516 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005517 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005518 * 7. Otherwise, this is a response message without a declared message
5519 * body length, so the message body length is determined by the
5520 * number of octets received prior to the server closing the
5521 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005522 */
5523
5524 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005525 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005526 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005527 * FIXME: should we parse anyway and return an error on chunked encoding ?
5528 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005529 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5530 txn->status == 101)) {
5531 /* Either we've established an explicit tunnel, or we're
5532 * switching the protocol. In both cases, we're very unlikely
5533 * to understand the next protocols. We have to switch to tunnel
5534 * mode, so that we transfer the request and responses then let
5535 * this protocol pass unmodified. When we later implement specific
5536 * parsers for such protocols, we'll want to check the Upgrade
5537 * header which contains information about that protocol for
5538 * responses with status 101 (eg: see RFC2817 about TLS).
5539 */
5540 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5541 msg->flags |= HTTP_MSGF_XFER_LEN;
5542 goto end;
5543 }
5544
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005545 if (txn->meth == HTTP_METH_HEAD ||
5546 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005547 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005548 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005549 goto skip_content_length;
5550 }
5551
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005552 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005553 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005554 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005555 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005556 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5557 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005558 /* bad transfer-encoding (chunked followed by something else) */
5559 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005560 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005561 break;
5562 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005563 }
5564
Willy Tarreau1c913912015-04-30 10:57:51 +02005565 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005566 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005567 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005568 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5569 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5570 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005571 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572 signed long long cl;
5573
Willy Tarreauad14f752011-09-02 20:33:27 +02005574 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005575 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005576 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005577 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005578
Willy Tarreauad14f752011-09-02 20:33:27 +02005579 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005580 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005581 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005582 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005583
Willy Tarreauad14f752011-09-02 20:33:27 +02005584 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005585 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005586 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005587 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005588
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005589 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005590 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005591 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005592 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005593
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005594 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005595 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005596 }
5597
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005598 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005599 /* Now we have to check if we need to modify the Connection header.
5600 * This is more difficult on the response than it is on the request,
5601 * because we can have two different HTTP versions and we don't know
5602 * how the client will interprete a response. For instance, let's say
5603 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5604 * HTTP/1.1 response without any header. Maybe it will bound itself to
5605 * HTTP/1.0 because it only knows about it, and will consider the lack
5606 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5607 * the lack of header as a keep-alive. Thus we will use two flags
5608 * indicating how a request MAY be understood by the client. In case
5609 * of multiple possibilities, we'll fix the header to be explicit. If
5610 * ambiguous cases such as both close and keepalive are seen, then we
5611 * will fall back to explicit close. Note that we won't take risks with
5612 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005613 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005614 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005615 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5616 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5617 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5618 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005619 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005620
Willy Tarreau70dffda2014-01-30 03:07:23 +01005621 /* this situation happens when combining pretend-keepalive with httpclose. */
5622 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005623 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005624 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005625 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5626
Willy Tarreau60466522010-01-18 19:08:45 +01005627 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005628 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005629 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5630 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005631
Willy Tarreau60466522010-01-18 19:08:45 +01005632 /* now adjust header transformations depending on current state */
5633 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5634 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5635 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005636 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005637 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005638 }
Willy Tarreau60466522010-01-18 19:08:45 +01005639 else { /* SCL / KAL */
5640 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005641 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005642 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005643 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005644
Willy Tarreau60466522010-01-18 19:08:45 +01005645 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005646 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005647
Willy Tarreau60466522010-01-18 19:08:45 +01005648 /* Some keep-alive responses are converted to Server-close if
5649 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005650 */
Willy Tarreau60466522010-01-18 19:08:45 +01005651 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5652 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005653 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005654 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005655 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005656 }
5657
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005658 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005659 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005660 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005661
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005662 /* end of job, return OK */
5663 rep->analysers &= ~an_bit;
5664 rep->analyse_exp = TICK_ETERNITY;
5665 channel_auto_close(rep);
5666 return 1;
5667
5668 abort_keep_alive:
5669 /* A keep-alive request to the server failed on a network error.
5670 * The client is required to retry. We need to close without returning
5671 * any other information so that the client retries.
5672 */
5673 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005674 rep->analysers &= AN_RES_FLT_END;
5675 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005676 channel_auto_close(rep);
5677 s->logs.logwait = 0;
5678 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005679 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005680 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005681 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005682 return 0;
5683}
5684
5685/* This function performs all the processing enabled for the current response.
5686 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005687 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005688 * other functions. It works like process_request (see indications above).
5689 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005690int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005691{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005692 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005693 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005694 struct http_msg *msg = &txn->rsp;
5695 struct proxy *cur_proxy;
5696 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005697 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005698
Willy Tarreau87b09662015-04-03 00:22:06 +02005699 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005700 now_ms, __FUNCTION__,
5701 s,
5702 rep,
5703 rep->rex, rep->wex,
5704 rep->flags,
5705 rep->buf->i,
5706 rep->analysers);
5707
5708 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5709 return 0;
5710
Willy Tarreau70730dd2014-04-24 18:06:27 +02005711 /* The stats applet needs to adjust the Connection header but we don't
5712 * apply any filter there.
5713 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005714 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5715 rep->analysers &= ~an_bit;
5716 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005717 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005718 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005719
Willy Tarreau58975672014-04-24 21:13:57 +02005720 /*
5721 * We will have to evaluate the filters.
5722 * As opposed to version 1.2, now they will be evaluated in the
5723 * filters order and not in the header order. This means that
5724 * each filter has to be validated among all headers.
5725 *
5726 * Filters are tried with ->be first, then with ->fe if it is
5727 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005728 *
5729 * Maybe we are in resume condiion. In this case I choose the
5730 * "struct proxy" which contains the rule list matching the resume
5731 * pointer. If none of theses "struct proxy" match, I initialise
5732 * the process with the first one.
5733 *
5734 * In fact, I check only correspondance betwwen the current list
5735 * pointer and the ->fe rule list. If it doesn't match, I initialize
5736 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005737 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005738 if (s->current_rule_list == &sess->fe->http_res_rules)
5739 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005740 else
5741 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005742 while (1) {
5743 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005744
Willy Tarreau58975672014-04-24 21:13:57 +02005745 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005746 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005747 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005748
Willy Tarreau51d861a2015-05-22 17:30:48 +02005749 if (ret == HTTP_RULE_RES_BADREQ)
5750 goto return_srv_prx_502;
5751
5752 if (ret == HTTP_RULE_RES_DONE) {
5753 rep->analysers &= ~an_bit;
5754 rep->analyse_exp = TICK_ETERNITY;
5755 return 1;
5756 }
5757 }
5758
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005759 /* we need to be called again. */
5760 if (ret == HTTP_RULE_RES_YIELD) {
5761 channel_dont_close(rep);
5762 return 0;
5763 }
5764
Willy Tarreau58975672014-04-24 21:13:57 +02005765 /* try headers filters */
5766 if (rule_set->rsp_exp != NULL) {
5767 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5768 return_bad_resp:
5769 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005770 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005771 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005772 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005773 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005774 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005775 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005776 txn->status = 502;
5777 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005778 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005779 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005780 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005781 if (!(s->flags & SF_ERR_MASK))
5782 s->flags |= SF_ERR_PRXCOND;
5783 if (!(s->flags & SF_FINST_MASK))
5784 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005785 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005786 }
Willy Tarreau58975672014-04-24 21:13:57 +02005787 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005788
Willy Tarreau58975672014-04-24 21:13:57 +02005789 /* has the response been denied ? */
5790 if (txn->flags & TX_SVDENY) {
5791 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005792 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005793
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005794 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5795 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005796 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005797 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005798
Willy Tarreau58975672014-04-24 21:13:57 +02005799 goto return_srv_prx_502;
5800 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005801
Willy Tarreau58975672014-04-24 21:13:57 +02005802 /* add response headers from the rule sets in the same order */
5803 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005804 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005805 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005806 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005807 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005808 ret = acl_pass(ret);
5809 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5810 ret = !ret;
5811 if (!ret)
5812 continue;
5813 }
5814 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5815 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005817
Willy Tarreau58975672014-04-24 21:13:57 +02005818 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005819 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005820 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005821 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005822 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005823
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005824 /* After this point, this anayzer can't return yield, so we can
5825 * remove the bit corresponding to this analyzer from the list.
5826 *
5827 * Note that the intermediate returns and goto found previously
5828 * reset the analyzers.
5829 */
5830 rep->analysers &= ~an_bit;
5831 rep->analyse_exp = TICK_ETERNITY;
5832
Willy Tarreau58975672014-04-24 21:13:57 +02005833 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005834 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005835 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005836
Willy Tarreau58975672014-04-24 21:13:57 +02005837 /*
5838 * Now check for a server cookie.
5839 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005840 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005841 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005842
Willy Tarreau58975672014-04-24 21:13:57 +02005843 /*
5844 * Check for cache-control or pragma headers if required.
5845 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005846 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005847 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005848
Willy Tarreau58975672014-04-24 21:13:57 +02005849 /*
5850 * Add server cookie in the response if needed
5851 */
5852 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5853 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005854 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005855 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5856 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5857 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5858 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5859 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005860 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005861 /* the server is known, it's not the one the client requested, or the
5862 * cookie's last seen date needs to be refreshed. We have to
5863 * insert a set-cookie here, except if we want to insert only on POST
5864 * requests and this one isn't. Note that servers which don't have cookies
5865 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005866 */
Willy Tarreau58975672014-04-24 21:13:57 +02005867 if (!objt_server(s->target)->cookie) {
5868 chunk_printf(&trash,
5869 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5870 s->be->cookie_name);
5871 }
5872 else {
5873 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005874
Willy Tarreau58975672014-04-24 21:13:57 +02005875 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5876 /* emit last_date, which is mandatory */
5877 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5878 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5879 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005880
Willy Tarreau58975672014-04-24 21:13:57 +02005881 if (s->be->cookie_maxlife) {
5882 /* emit first_date, which is either the original one or
5883 * the current date.
5884 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005885 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005886 s30tob64(txn->cookie_first_date ?
5887 txn->cookie_first_date >> 2 :
5888 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005889 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005890 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005891 }
Willy Tarreau58975672014-04-24 21:13:57 +02005892 chunk_appendf(&trash, "; path=/");
5893 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005894
Willy Tarreau58975672014-04-24 21:13:57 +02005895 if (s->be->cookie_domain)
5896 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005897
Willy Tarreau58975672014-04-24 21:13:57 +02005898 if (s->be->ck_opts & PR_CK_HTTPONLY)
5899 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005900
Willy Tarreau58975672014-04-24 21:13:57 +02005901 if (s->be->ck_opts & PR_CK_SECURE)
5902 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005903
Willy Tarreau58975672014-04-24 21:13:57 +02005904 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5905 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005906
Willy Tarreau58975672014-04-24 21:13:57 +02005907 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005908 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005909 /* the server did not change, only the date was updated */
5910 txn->flags |= TX_SCK_UPDATED;
5911 else
5912 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005913
Willy Tarreau58975672014-04-24 21:13:57 +02005914 /* Here, we will tell an eventual cache on the client side that we don't
5915 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5916 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5917 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005918 */
Willy Tarreau58975672014-04-24 21:13:57 +02005919 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005920
Willy Tarreau58975672014-04-24 21:13:57 +02005921 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005922
Willy Tarreau58975672014-04-24 21:13:57 +02005923 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5924 "Cache-control: private", 22) < 0))
5925 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005926 }
Willy Tarreau58975672014-04-24 21:13:57 +02005927 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005928
Willy Tarreau58975672014-04-24 21:13:57 +02005929 /*
5930 * Check if result will be cacheable with a cookie.
5931 * We'll block the response if security checks have caught
5932 * nasty things such as a cacheable cookie.
5933 */
5934 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5935 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5936 (s->be->options & PR_O_CHK_CACHE)) {
5937 /* we're in presence of a cacheable response containing
5938 * a set-cookie header. We'll block it as requested by
5939 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005940 */
Willy Tarreau58975672014-04-24 21:13:57 +02005941 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005942 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005943
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005944 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5945 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005946 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005947 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948
Christopher Faulet767a84b2017-11-24 16:50:31 +01005949 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5950 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005951 send_log(s->be, LOG_ALERT,
5952 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5953 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5954 goto return_srv_prx_502;
5955 }
Willy Tarreau03945942009-12-22 16:50:27 +01005956
Willy Tarreau70730dd2014-04-24 18:06:27 +02005957 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005958 /*
5959 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5960 * If an "Upgrade" token is found, the header is left untouched in order
5961 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005962 * if anything but "Upgrade" is present in the Connection header. We don't
5963 * want to touch any 101 response either since it's switching to another
5964 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005965 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005966 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005967 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005968 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005969 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5970 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005971
Willy Tarreau58975672014-04-24 21:13:57 +02005972 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5973 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5974 /* we want a keep-alive response here. Keep-alive header
5975 * required if either side is not 1.1.
5976 */
5977 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5978 want_flags |= TX_CON_KAL_SET;
5979 }
5980 else {
5981 /* we want a close response here. Close header required if
5982 * the server is 1.1, regardless of the client.
5983 */
5984 if (msg->flags & HTTP_MSGF_VER_11)
5985 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005986 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005987
Willy Tarreau58975672014-04-24 21:13:57 +02005988 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5989 http_change_connection_header(txn, msg, want_flags);
5990 }
5991
5992 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005993 /* Always enter in the body analyzer */
5994 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5995 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005996
Willy Tarreau58975672014-04-24 21:13:57 +02005997 /* if the user wants to log as soon as possible, without counting
5998 * bytes from the server, then this is the right moment. We have
5999 * to temporarily assign bytes_out to log what we currently have.
6000 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006001 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006002 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6003 s->logs.bytes_out = txn->rsp.eoh;
6004 s->do_log(s);
6005 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006006 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006007 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006008}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006009
Willy Tarreaud98cf932009-12-27 22:54:55 +01006010/* This function is an analyser which forwards response body (including chunk
6011 * sizes if any). It is called as soon as we must forward, even if we forward
6012 * zero byte. The only situation where it must not be called is when we're in
6013 * tunnel mode and we want to forward till the close. It's used both to forward
6014 * remaining data and to resync after end of body. It expects the msg_state to
6015 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006016 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006017 *
6018 * It is capable of compressing response data both in content-length mode and
6019 * in chunked mode. The state machines follows different flows depending on
6020 * whether content-length and chunked modes are used, since there are no
6021 * trailers in content-length :
6022 *
6023 * chk-mode cl-mode
6024 * ,----- BODY -----.
6025 * / \
6026 * V size > 0 V chk-mode
6027 * .--> SIZE -------------> DATA -------------> CRLF
6028 * | | size == 0 | last byte |
6029 * | v final crlf v inspected |
6030 * | TRAILERS -----------> DONE |
6031 * | |
6032 * `----------------------------------------------'
6033 *
6034 * Compression only happens in the DATA state, and must be flushed in final
6035 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6036 * is performed at once on final states for all bytes parsed, or when leaving
6037 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006038 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006039int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006040{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006041 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006042 struct http_txn *txn = s->txn;
6043 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006044 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006045
Christopher Faulet814d2702017-03-30 11:33:44 +02006046 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6047 now_ms, __FUNCTION__,
6048 s,
6049 res,
6050 res->rex, res->wex,
6051 res->flags,
6052 res->buf->i,
6053 res->analysers);
6054
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006055 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6056 return 0;
6057
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006058 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006059 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006060 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006061 /* Output closed while we were sending data. We must abort and
6062 * wake the other side up.
6063 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006064 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006065 msg->msg_state = HTTP_MSG_ERROR;
6066 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006067 return 1;
6068 }
6069
Willy Tarreau4fe41902010-06-07 22:27:41 +02006070 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006071 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006072
Christopher Fauletd7c91962015-04-30 11:48:27 +02006073 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006074 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6075 ? HTTP_MSG_CHUNK_SIZE
6076 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006077 }
6078
Willy Tarreauefdf0942014-04-24 20:08:57 +02006079 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006080 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006081 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006082 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006083 }
6084
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006085 if (msg->msg_state < HTTP_MSG_DONE) {
6086 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6087 ? http_msg_forward_chunked_body(s, msg)
6088 : http_msg_forward_body(s, msg));
6089 if (!ret)
6090 goto missing_data_or_waiting;
6091 if (ret < 0)
6092 goto return_bad_res;
6093 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006094
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006095 /* other states, DONE...TUNNEL */
6096 /* for keep-alive we don't want to forward closes on DONE */
6097 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6098 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6099 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006100
Christopher Faulet894da4c2017-07-18 11:29:07 +02006101 http_resync_states(s);
6102 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006103 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6104 if (res->flags & CF_SHUTW) {
6105 /* response errors are most likely due to the
6106 * client aborting the transfer. */
6107 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006108 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006109 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006110 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 +01006111 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006112 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006113 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006114 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006115 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006116
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006117 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006118 if (res->flags & CF_SHUTW)
6119 goto aborted_xfer;
6120
6121 /* stop waiting for data if the input is closed before the end. If the
6122 * client side was already closed, it means that the client has aborted,
6123 * so we don't want to count this as a server abort. Otherwise it's a
6124 * server abort.
6125 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006126 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006127 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006128 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006129 /* If we have some pending data, we continue the processing */
6130 if (!buffer_pending(res->buf)) {
6131 if (!(s->flags & SF_ERR_MASK))
6132 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006133 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006134 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006135 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006136 goto return_bad_res_stats_ok;
6137 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006138 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006139
Willy Tarreau40dba092010-03-04 18:14:51 +01006140 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006141 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006142 goto return_bad_res;
6143
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006144 /* When TE: chunked is used, we need to get there again to parse
6145 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006146 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6147 * or if there are filters registered on the stream, we don't want to
6148 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006149 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006150 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006151 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006152 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6153 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006154 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006155
Willy Tarreau5c620922011-05-11 19:56:11 +02006156 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006157 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006158 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006159 * modes are already handled by the stream sock layer. We must not do
6160 * this in content-length mode because it could present the MSG_MORE
6161 * flag with the last block of forwarded data, which would cause an
6162 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006163 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006164 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006165 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006166
Willy Tarreau87b09662015-04-03 00:22:06 +02006167 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006168 return 0;
6169
Willy Tarreau40dba092010-03-04 18:14:51 +01006170 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006171 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006172 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006173 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006174
6175 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006176 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006177 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006178 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006179 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006180 res->analysers &= AN_RES_FLT_END;
6181 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 +01006182 if (objt_server(s->target))
6183 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006184
Willy Tarreaue7dff022015-04-03 01:14:29 +02006185 if (!(s->flags & SF_ERR_MASK))
6186 s->flags |= SF_ERR_PRXCOND;
6187 if (!(s->flags & SF_FINST_MASK))
6188 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006189 return 0;
6190
6191 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006192 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006193 txn->rsp.msg_state = HTTP_MSG_ERROR;
6194 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006195 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006196 res->analysers &= AN_RES_FLT_END;
6197 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 +01006198
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006199 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6200 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006201 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006202 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006203
Willy Tarreaue7dff022015-04-03 01:14:29 +02006204 if (!(s->flags & SF_ERR_MASK))
6205 s->flags |= SF_ERR_CLICL;
6206 if (!(s->flags & SF_FINST_MASK))
6207 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006208 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006209}
6210
6211
6212static inline int
6213http_msg_forward_body(struct stream *s, struct http_msg *msg)
6214{
6215 struct channel *chn = msg->chn;
6216 int ret;
6217
6218 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6219
6220 if (msg->msg_state == HTTP_MSG_ENDING)
6221 goto ending;
6222
6223 /* Neither content-length, nor transfer-encoding was found, so we must
6224 * read the body until the server connection is closed. In that case, we
6225 * eat data as they come. Of course, this happens for response only. */
6226 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6227 unsigned long long len = (chn->buf->i - msg->next);
6228 msg->chunk_len += len;
6229 msg->body_len += len;
6230 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006231 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6232 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6233 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006234 msg->next += ret;
6235 msg->chunk_len -= ret;
6236 if (msg->chunk_len) {
6237 /* input empty or output full */
6238 if (chn->buf->i > msg->next)
6239 chn->flags |= CF_WAKE_WRITE;
6240 goto missing_data_or_waiting;
6241 }
6242
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006243 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6244 * always set for a request. */
6245 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6246 /* The server still sending data that should be filtered */
6247 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6248 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006249 msg->msg_state = HTTP_MSG_TUNNEL;
6250 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006251 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006252
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006253 msg->msg_state = HTTP_MSG_ENDING;
6254
6255 ending:
6256 /* we may have some pending data starting at res->buf->p such as a last
6257 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006258 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6259 /* default_ret */ msg->next,
6260 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006261 b_adv(chn->buf, ret);
6262 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006263 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6264 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006265 if (msg->next)
6266 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006267
Christopher Fauletda02e172015-12-04 09:25:05 +01006268 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6269 /* default_ret */ 1,
6270 /* on_error */ goto error,
6271 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006272 if (msg->msg_state == HTTP_MSG_ENDING)
6273 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006274 return 1;
6275
6276 missing_data_or_waiting:
6277 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006278 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6279 /* default_ret */ msg->next,
6280 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006281 b_adv(chn->buf, ret);
6282 msg->next -= ret;
6283 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6284 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006285 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006286 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006287 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006288 return 0;
6289 error:
6290 return -1;
6291}
6292
6293static inline int
6294http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6295{
6296 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006297 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006298 int ret;
6299
6300 /* Here we have the guarantee to be in one of the following state:
6301 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6302 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6303
6304 switch_states:
6305 switch (msg->msg_state) {
6306 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006307 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6308 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6309 /* on_error */ goto error);
6310 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006311 msg->chunk_len -= ret;
6312 if (msg->chunk_len) {
6313 /* input empty or output full */
6314 if (chn->buf->i > msg->next)
6315 chn->flags |= CF_WAKE_WRITE;
6316 goto missing_data_or_waiting;
6317 }
6318
6319 /* nothing left to forward for this chunk*/
6320 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6321 /* fall through for HTTP_MSG_CHUNK_CRLF */
6322
6323 case HTTP_MSG_CHUNK_CRLF:
6324 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006325 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006326 if (ret == 0)
6327 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006328 if (ret < 0) {
6329 msg->err_pos = chn->buf->i + ret;
6330 if (msg->err_pos < 0)
6331 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006332 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006333 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006334 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006335 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6336 /* fall through for HTTP_MSG_CHUNK_SIZE */
6337
6338 case HTTP_MSG_CHUNK_SIZE:
6339 /* read the chunk size and assign it to ->chunk_len,
6340 * then set ->next to point to the body and switch to
6341 * DATA or TRAILERS state.
6342 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006343 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006344 if (ret == 0)
6345 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006346 if (ret < 0) {
6347 msg->err_pos = chn->buf->i + ret;
6348 if (msg->err_pos < 0)
6349 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006350 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006351 }
6352
6353 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006354 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006355 msg->chunk_len = chunk;
6356 msg->body_len += chunk;
6357
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006358 if (msg->chunk_len) {
6359 msg->msg_state = HTTP_MSG_DATA;
6360 goto switch_states;
6361 }
6362 msg->msg_state = HTTP_MSG_TRAILERS;
6363 /* fall through for HTTP_MSG_TRAILERS */
6364
6365 case HTTP_MSG_TRAILERS:
6366 ret = http_forward_trailers(msg);
6367 if (ret < 0)
6368 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006369 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6370 /* default_ret */ 1,
6371 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006372 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006373 if (!ret)
6374 goto missing_data_or_waiting;
6375 break;
6376
6377 case HTTP_MSG_ENDING:
6378 goto ending;
6379
6380 default:
6381 /* This should no happen in this function */
6382 goto error;
6383 }
6384
6385 msg->msg_state = HTTP_MSG_ENDING;
6386 ending:
6387 /* we may have some pending data starting at res->buf->p such as a last
6388 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006389 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006390 /* default_ret */ msg->next,
6391 /* on_error */ goto error);
6392 b_adv(chn->buf, ret);
6393 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006394 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6395 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006396 if (msg->next)
6397 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006398
Christopher Fauletda02e172015-12-04 09:25:05 +01006399 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006400 /* default_ret */ 1,
6401 /* on_error */ goto error,
6402 /* on_wait */ goto waiting);
6403 msg->msg_state = HTTP_MSG_DONE;
6404 return 1;
6405
6406 missing_data_or_waiting:
6407 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006408 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006409 /* default_ret */ msg->next,
6410 /* on_error */ goto error);
6411 b_adv(chn->buf, ret);
6412 msg->next -= ret;
6413 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6414 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006415 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006416 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006417 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006418 return 0;
6419
6420 chunk_parsing_error:
6421 if (msg->err_pos >= 0) {
6422 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006423 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006424 msg->msg_state, strm_fe(s));
6425 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006426 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006427 msg, msg->msg_state, s->be);
6428 }
6429 error:
6430 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006431}
6432
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006434/* Iterate the same filter through all request headers.
6435 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006436 * Since it can manage the switch to another backend, it updates the per-proxy
6437 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006438 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006439int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006440{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 char *cur_ptr, *cur_end, *cur_next;
6442 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006443 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006444 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006445 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 last_hdr = 0;
6448
Willy Tarreau9b28e032012-10-12 23:49:43 +02006449 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006450 old_idx = 0;
6451
6452 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006453 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006455 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 (exp->action == ACT_ALLOW ||
6457 exp->action == ACT_DENY ||
6458 exp->action == ACT_TARPIT))
6459 return 0;
6460
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006461 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006462 if (!cur_idx)
6463 break;
6464
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006465 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 cur_ptr = cur_next;
6467 cur_end = cur_ptr + cur_hdr->len;
6468 cur_next = cur_end + cur_hdr->cr + 1;
6469
6470 /* Now we have one header between cur_ptr and cur_end,
6471 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006472 */
6473
Willy Tarreau15a53a42015-01-21 13:39:42 +01006474 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006477 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 last_hdr = 1;
6479 break;
6480
6481 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006482 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006484 break;
6485
6486 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006487 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006489 break;
6490
6491 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006492 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6493 if (trash.len < 0)
6494 return -1;
6495
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006496 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 /* FIXME: if the user adds a newline in the replacement, the
6498 * index will not be recalculated for now, and the new line
6499 * will not be counted as a new header.
6500 */
6501
6502 cur_end += delta;
6503 cur_next += delta;
6504 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006505 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 break;
6507
6508 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006509 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006510 cur_next += delta;
6511
Willy Tarreaufa355d42009-11-29 18:12:29 +01006512 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006513 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6514 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006515 cur_hdr->len = 0;
6516 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006517 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006518 break;
6519
6520 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006521 }
6522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006523 /* keep the link from this header to next one in case of later
6524 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 old_idx = cur_idx;
6527 }
6528 return 0;
6529}
6530
6531
6532/* Apply the filter to the request line.
6533 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6534 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006535 * Since it can manage the switch to another backend, it updates the per-proxy
6536 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006537 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006538int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 char *cur_ptr, *cur_end;
6541 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006542 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006543 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006544
Willy Tarreau3d300592007-03-18 18:34:41 +01006545 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006547 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 (exp->action == ACT_ALLOW ||
6549 exp->action == ACT_DENY ||
6550 exp->action == ACT_TARPIT))
6551 return 0;
6552 else if (exp->action == ACT_REMOVE)
6553 return 0;
6554
6555 done = 0;
6556
Willy Tarreau9b28e032012-10-12 23:49:43 +02006557 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006558 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006559
6560 /* Now we have the request line between cur_ptr and cur_end */
6561
Willy Tarreau15a53a42015-01-21 13:39:42 +01006562 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006565 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566 done = 1;
6567 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006570 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 done = 1;
6572 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006574 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006575 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006576 done = 1;
6577 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006580 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6581 if (trash.len < 0)
6582 return -1;
6583
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006584 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006585 /* FIXME: if the user adds a newline in the replacement, the
6586 * index will not be recalculated for now, and the new line
6587 * will not be counted as a new header.
6588 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006589
Willy Tarreaufa355d42009-11-29 18:12:29 +01006590 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006591 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006592 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006593 HTTP_MSG_RQMETH,
6594 cur_ptr, cur_end + 1,
6595 NULL, NULL);
6596 if (unlikely(!cur_end))
6597 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006599 /* we have a full request and we know that we have either a CR
6600 * or an LF at <ptr>.
6601 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006602 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6603 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006604 /* there is no point trying this regex on headers */
6605 return 1;
6606 }
6607 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006608 return done;
6609}
Willy Tarreau97de6242006-12-27 17:18:38 +01006610
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006613/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006614 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006615 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616 * unparsable request. Since it can manage the switch to another backend, it
6617 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006618 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006619int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006620{
Willy Tarreau192252e2015-04-04 01:47:55 +02006621 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006622 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006623 struct hdr_exp *exp;
6624
6625 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006626 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006628 /*
6629 * The interleaving of transformations and verdicts
6630 * makes it difficult to decide to continue or stop
6631 * the evaluation.
6632 */
6633
Willy Tarreau6c123b12010-01-28 20:22:06 +01006634 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6635 break;
6636
Willy Tarreau3d300592007-03-18 18:34:41 +01006637 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006638 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006639 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006640 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006641
6642 /* if this filter had a condition, evaluate it now and skip to
6643 * next filter if the condition does not match.
6644 */
6645 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006646 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006647 ret = acl_pass(ret);
6648 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6649 ret = !ret;
6650
6651 if (!ret)
6652 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006653 }
6654
6655 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006656 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006657 if (unlikely(ret < 0))
6658 return -1;
6659
6660 if (likely(ret == 0)) {
6661 /* The filter did not match the request, it can be
6662 * iterated through all headers.
6663 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006664 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6665 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006667 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006668 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006669}
6670
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006671
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006672/* Find the end of a cookie value contained between <s> and <e>. It works the
6673 * same way as with headers above except that the semi-colon also ends a token.
6674 * See RFC2965 for more information. Note that it requires a valid header to
6675 * return a valid result.
6676 */
6677char *find_cookie_value_end(char *s, const char *e)
6678{
6679 int quoted, qdpair;
6680
6681 quoted = qdpair = 0;
6682 for (; s < e; s++) {
6683 if (qdpair) qdpair = 0;
6684 else if (quoted) {
6685 if (*s == '\\') qdpair = 1;
6686 else if (*s == '"') quoted = 0;
6687 }
6688 else if (*s == '"') quoted = 1;
6689 else if (*s == ',' || *s == ';') return s;
6690 }
6691 return s;
6692}
6693
6694/* Delete a value in a header between delimiters <from> and <next> in buffer
6695 * <buf>. The number of characters displaced is returned, and the pointer to
6696 * the first delimiter is updated if required. The function tries as much as
6697 * possible to respect the following principles :
6698 * - replace <from> delimiter by the <next> one unless <from> points to a
6699 * colon, in which case <next> is simply removed
6700 * - set exactly one space character after the new first delimiter, unless
6701 * there are not enough characters in the block being moved to do so.
6702 * - remove unneeded spaces before the previous delimiter and after the new
6703 * one.
6704 *
6705 * It is the caller's responsibility to ensure that :
6706 * - <from> points to a valid delimiter or the colon ;
6707 * - <next> points to a valid delimiter or the final CR/LF ;
6708 * - there are non-space chars before <from> ;
6709 * - there is a CR/LF at or after <next>.
6710 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006711int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006712{
6713 char *prev = *from;
6714
6715 if (*prev == ':') {
6716 /* We're removing the first value, preserve the colon and add a
6717 * space if possible.
6718 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006719 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006720 next++;
6721 prev++;
6722 if (prev < next)
6723 *prev++ = ' ';
6724
Willy Tarreau2235b262016-11-05 15:50:20 +01006725 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006726 next++;
6727 } else {
6728 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006729 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006730 prev--;
6731 *from = prev;
6732
6733 /* copy the delimiter and if possible a space if we're
6734 * not at the end of the line.
6735 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006736 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737 *prev++ = *next++;
6738 if (prev + 1 < next)
6739 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006740 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006741 next++;
6742 }
6743 }
6744 return buffer_replace2(buf, prev, next, NULL, 0);
6745}
6746
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006747/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006748 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006749 * desirable to call it only when needed. This code is quite complex because
6750 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6751 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006753void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006755 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006756 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006758 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006759 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6760 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006761
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006762 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006763 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006764 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006765
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006766 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006768 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006769
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006770 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 hdr_beg = hdr_next;
6772 hdr_end = hdr_beg + cur_hdr->len;
6773 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006774
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006775 /* We have one full header between hdr_beg and hdr_end, and the
6776 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006777 * "Cookie:" headers.
6778 */
6779
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006781 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006782 old_idx = cur_idx;
6783 continue;
6784 }
6785
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006786 del_from = NULL; /* nothing to be deleted */
6787 preserve_hdr = 0; /* assume we may kill the whole header */
6788
Willy Tarreau58f10d72006-12-04 02:26:12 +01006789 /* Now look for cookies. Conforming to RFC2109, we have to support
6790 * attributes whose name begin with a '$', and associate them with
6791 * the right cookie, if we want to delete this cookie.
6792 * So there are 3 cases for each cookie read :
6793 * 1) it's a special attribute, beginning with a '$' : ignore it.
6794 * 2) it's a server id cookie that we *MAY* want to delete : save
6795 * some pointers on it (last semi-colon, beginning of cookie...)
6796 * 3) it's an application cookie : we *MAY* have to delete a previous
6797 * "special" cookie.
6798 * At the end of loop, if a "special" cookie remains, we may have to
6799 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006800 * *MUST* delete it.
6801 *
6802 * Note: RFC2965 is unclear about the processing of spaces around
6803 * the equal sign in the ATTR=VALUE form. A careful inspection of
6804 * the RFC explicitly allows spaces before it, and not within the
6805 * tokens (attrs or values). An inspection of RFC2109 allows that
6806 * too but section 10.1.3 lets one think that spaces may be allowed
6807 * after the equal sign too, resulting in some (rare) buggy
6808 * implementations trying to do that. So let's do what servers do.
6809 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6810 * allowed quoted strings in values, with any possible character
6811 * after a backslash, including control chars and delimitors, which
6812 * causes parsing to become ambiguous. Browsers also allow spaces
6813 * within values even without quotes.
6814 *
6815 * We have to keep multiple pointers in order to support cookie
6816 * removal at the beginning, middle or end of header without
6817 * corrupting the header. All of these headers are valid :
6818 *
6819 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6820 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6821 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6822 * | | | | | | | | |
6823 * | | | | | | | | hdr_end <--+
6824 * | | | | | | | +--> next
6825 * | | | | | | +----> val_end
6826 * | | | | | +-----------> val_beg
6827 * | | | | +--------------> equal
6828 * | | | +----------------> att_end
6829 * | | +---------------------> att_beg
6830 * | +--------------------------> prev
6831 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006832 */
6833
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6835 /* Iterate through all cookies on this line */
6836
6837 /* find att_beg */
6838 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006839 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 att_beg++;
6841
6842 /* find att_end : this is the first character after the last non
6843 * space before the equal. It may be equal to hdr_end.
6844 */
6845 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006846
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006847 while (equal < hdr_end) {
6848 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006849 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006850 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006851 continue;
6852 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006853 }
6854
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006855 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6856 * is between <att_beg> and <equal>, both may be identical.
6857 */
6858
6859 /* look for end of cookie if there is an equal sign */
6860 if (equal < hdr_end && *equal == '=') {
6861 /* look for the beginning of the value */
6862 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006863 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006864 val_beg++;
6865
6866 /* find the end of the value, respecting quotes */
6867 next = find_cookie_value_end(val_beg, hdr_end);
6868
6869 /* make val_end point to the first white space or delimitor after the value */
6870 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006871 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 val_end--;
6873 } else {
6874 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006875 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006876
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 /* We have nothing to do with attributes beginning with '$'. However,
6878 * they will automatically be removed if a header before them is removed,
6879 * since they're supposed to be linked together.
6880 */
6881 if (*att_beg == '$')
6882 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006883
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006884 /* Ignore cookies with no equal sign */
6885 if (equal == next) {
6886 /* This is not our cookie, so we must preserve it. But if we already
6887 * scheduled another cookie for removal, we cannot remove the
6888 * complete header, but we can remove the previous block itself.
6889 */
6890 preserve_hdr = 1;
6891 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006892 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006893 val_end += delta;
6894 next += delta;
6895 hdr_end += delta;
6896 hdr_next += delta;
6897 cur_hdr->len += delta;
6898 http_msg_move_end(&txn->req, delta);
6899 prev = del_from;
6900 del_from = NULL;
6901 }
6902 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006903 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006904
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 /* if there are spaces around the equal sign, we need to
6906 * strip them otherwise we'll get trouble for cookie captures,
6907 * or even for rewrites. Since this happens extremely rarely,
6908 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006909 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006910 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6911 int stripped_before = 0;
6912 int stripped_after = 0;
6913
6914 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006915 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006916 equal += stripped_before;
6917 val_beg += stripped_before;
6918 }
6919
6920 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006921 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006922 val_beg += stripped_after;
6923 stripped_before += stripped_after;
6924 }
6925
6926 val_end += stripped_before;
6927 next += stripped_before;
6928 hdr_end += stripped_before;
6929 hdr_next += stripped_before;
6930 cur_hdr->len += stripped_before;
6931 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006932 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006933 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006935 /* First, let's see if we want to capture this cookie. We check
6936 * that we don't already have a client side cookie, because we
6937 * can only capture one. Also as an optimisation, we ignore
6938 * cookies shorter than the declared name.
6939 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006940 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6941 (val_end - att_beg >= sess->fe->capture_namelen) &&
6942 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006943 int log_len = val_end - att_beg;
6944
Willy Tarreaubafbe012017-11-24 17:34:44 +01006945 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006946 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006947 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006948 if (log_len > sess->fe->capture_len)
6949 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 memcpy(txn->cli_cookie, att_beg, log_len);
6951 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006952 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006954
Willy Tarreaubca99692010-10-06 19:25:55 +02006955 /* Persistence cookies in passive, rewrite or insert mode have the
6956 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006957 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006958 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006959 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006960 * For cookies in prefix mode, the form is :
6961 *
6962 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006963 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006964 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6965 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6966 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006967 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6970 * have the server ID between val_beg and delim, and the original cookie between
6971 * delim+1 and val_end. Otherwise, delim==val_end :
6972 *
6973 * Cookie: NAME=SRV; # in all but prefix modes
6974 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6975 * | || || | |+-> next
6976 * | || || | +--> val_end
6977 * | || || +---------> delim
6978 * | || |+------------> val_beg
6979 * | || +-------------> att_end = equal
6980 * | |+-----------------> att_beg
6981 * | +------------------> prev
6982 * +-------------------------> hdr_beg
6983 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006984
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006985 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006986 for (delim = val_beg; delim < val_end; delim++)
6987 if (*delim == COOKIE_DELIM)
6988 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006989 } else {
6990 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006991 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006992 /* Now check if the cookie contains a date field, which would
6993 * appear after a vertical bar ('|') just after the server name
6994 * and before the delimiter.
6995 */
6996 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6997 if (vbar1) {
6998 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006999 * right is the last seen date. It is a base64 encoded
7000 * 30-bit value representing the UNIX date since the
7001 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007002 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007003 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007004 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007005 if (val_end - vbar1 >= 5) {
7006 val = b64tos30(vbar1);
7007 if (val > 0)
7008 txn->cookie_last_date = val << 2;
7009 }
7010 /* look for a second vertical bar */
7011 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7012 if (vbar1 && (val_end - vbar1 > 5)) {
7013 val = b64tos30(vbar1 + 1);
7014 if (val > 0)
7015 txn->cookie_first_date = val << 2;
7016 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007017 }
7018 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019
Willy Tarreauf64d1412010-10-07 20:06:11 +02007020 /* if the cookie has an expiration date and the proxy wants to check
7021 * it, then we do that now. We first check if the cookie is too old,
7022 * then only if it has expired. We detect strict overflow because the
7023 * time resolution here is not great (4 seconds). Cookies with dates
7024 * in the future are ignored if their offset is beyond one day. This
7025 * allows an admin to fix timezone issues without expiring everyone
7026 * and at the same time avoids keeping unwanted side effects for too
7027 * long.
7028 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007029 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7030 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007031 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007032 txn->flags &= ~TX_CK_MASK;
7033 txn->flags |= TX_CK_OLD;
7034 delim = val_beg; // let's pretend we have not found the cookie
7035 txn->cookie_first_date = 0;
7036 txn->cookie_last_date = 0;
7037 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007038 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7039 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007040 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007041 txn->flags &= ~TX_CK_MASK;
7042 txn->flags |= TX_CK_EXPIRED;
7043 delim = val_beg; // let's pretend we have not found the cookie
7044 txn->cookie_first_date = 0;
7045 txn->cookie_last_date = 0;
7046 }
7047
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 /* Here, we'll look for the first running server which supports the cookie.
7049 * This allows to share a same cookie between several servers, for example
7050 * to dedicate backup servers to specific servers only.
7051 * However, to prevent clients from sticking to cookie-less backup server
7052 * when they have incidentely learned an empty cookie, we simply ignore
7053 * empty cookies and mark them as invalid.
7054 * The same behaviour is applied when persistence must be ignored.
7055 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007056 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007057 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007058
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007059 while (srv) {
7060 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7061 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007062 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007063 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007064 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007065 /* we found the server and we can use it */
7066 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007067 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007068 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007069 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007070 break;
7071 } else {
7072 /* we found a server, but it's down,
7073 * mark it as such and go on in case
7074 * another one is available.
7075 */
7076 txn->flags &= ~TX_CK_MASK;
7077 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007078 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007080 srv = srv->next;
7081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007082
Willy Tarreauf64d1412010-10-07 20:06:11 +02007083 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007084 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007086 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007087 txn->flags |= TX_CK_UNUSED;
7088 else
7089 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007090 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 /* depending on the cookie mode, we may have to either :
7093 * - delete the complete cookie if we're in insert+indirect mode, so that
7094 * the server never sees it ;
7095 * - remove the server id from the cookie value, and tag the cookie as an
7096 * application cookie so that it does not get accidentely removed later,
7097 * if we're in cookie prefix mode
7098 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007099 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007100 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101
Willy Tarreau9b28e032012-10-12 23:49:43 +02007102 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 val_end += delta;
7104 next += delta;
7105 hdr_end += delta;
7106 hdr_next += delta;
7107 cur_hdr->len += delta;
7108 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007110 del_from = NULL;
7111 preserve_hdr = 1; /* we want to keep this cookie */
7112 }
7113 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007114 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007115 del_from = prev;
7116 }
7117 } else {
7118 /* This is not our cookie, so we must preserve it. But if we already
7119 * scheduled another cookie for removal, we cannot remove the
7120 * complete header, but we can remove the previous block itself.
7121 */
7122 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007125 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007126 if (att_beg >= del_from)
7127 att_beg += delta;
7128 if (att_end >= del_from)
7129 att_end += delta;
7130 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007131 val_end += delta;
7132 next += delta;
7133 hdr_end += delta;
7134 hdr_next += delta;
7135 cur_hdr->len += delta;
7136 http_msg_move_end(&txn->req, delta);
7137 prev = del_from;
7138 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007139 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007140 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007141
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 /* continue with next cookie on this header line */
7143 att_beg = next;
7144 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 /* There are no more cookies on this line.
7147 * We may still have one (or several) marked for deletion at the
7148 * end of the line. We must do this now in two ways :
7149 * - if some cookies must be preserved, we only delete from the
7150 * mark to the end of line ;
7151 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007153 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007155 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007156 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007157 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007158 cur_hdr->len += delta;
7159 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007160 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161
7162 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007163 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7164 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007165 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007166 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007167 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007168 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007169 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170 }
7171
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007172 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007173 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007174 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007175}
7176
7177
Willy Tarreaua15645d2007-03-18 16:22:39 +01007178/* Iterate the same filter through all response headers contained in <rtr>.
7179 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7180 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007181int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 char *cur_ptr, *cur_end, *cur_next;
7184 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007185 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007186 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007187 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188
7189 last_hdr = 0;
7190
Willy Tarreau9b28e032012-10-12 23:49:43 +02007191 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192 old_idx = 0;
7193
7194 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007195 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007197 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198 (exp->action == ACT_ALLOW ||
7199 exp->action == ACT_DENY))
7200 return 0;
7201
7202 cur_idx = txn->hdr_idx.v[old_idx].next;
7203 if (!cur_idx)
7204 break;
7205
7206 cur_hdr = &txn->hdr_idx.v[cur_idx];
7207 cur_ptr = cur_next;
7208 cur_end = cur_ptr + cur_hdr->len;
7209 cur_next = cur_end + cur_hdr->cr + 1;
7210
7211 /* Now we have one header between cur_ptr and cur_end,
7212 * and the next header starts at cur_next.
7213 */
7214
Willy Tarreau15a53a42015-01-21 13:39:42 +01007215 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007216 switch (exp->action) {
7217 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007218 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219 last_hdr = 1;
7220 break;
7221
7222 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007223 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224 last_hdr = 1;
7225 break;
7226
7227 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007228 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7229 if (trash.len < 0)
7230 return -1;
7231
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007232 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 /* FIXME: if the user adds a newline in the replacement, the
7234 * index will not be recalculated for now, and the new line
7235 * will not be counted as a new header.
7236 */
7237
7238 cur_end += delta;
7239 cur_next += delta;
7240 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007241 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 break;
7243
7244 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007245 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 cur_next += delta;
7247
Willy Tarreaufa355d42009-11-29 18:12:29 +01007248 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7250 txn->hdr_idx.used--;
7251 cur_hdr->len = 0;
7252 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007253 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 break;
7255
7256 }
7257 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258
7259 /* keep the link from this header to next one in case of later
7260 * removal of next header.
7261 */
7262 old_idx = cur_idx;
7263 }
7264 return 0;
7265}
7266
7267
7268/* Apply the filter to the status line in the response buffer <rtr>.
7269 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7270 * or -1 if a replacement resulted in an invalid status line.
7271 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007272int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 char *cur_ptr, *cur_end;
7275 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007276 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007277 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007278
7279
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 else if (exp->action == ACT_REMOVE)
7287 return 0;
7288
7289 done = 0;
7290
Willy Tarreau9b28e032012-10-12 23:49:43 +02007291 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007292 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293
7294 /* Now we have the status line between cur_ptr and cur_end */
7295
Willy Tarreau15a53a42015-01-21 13:39:42 +01007296 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297 switch (exp->action) {
7298 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007299 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007300 done = 1;
7301 break;
7302
7303 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007304 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305 done = 1;
7306 break;
7307
7308 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007309 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7310 if (trash.len < 0)
7311 return -1;
7312
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007313 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 /* FIXME: if the user adds a newline in the replacement, the
7315 * index will not be recalculated for now, and the new line
7316 * will not be counted as a new header.
7317 */
7318
Willy Tarreaufa355d42009-11-29 18:12:29 +01007319 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007321 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007322 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007323 cur_ptr, cur_end + 1,
7324 NULL, NULL);
7325 if (unlikely(!cur_end))
7326 return -1;
7327
7328 /* we have a full respnse and we know that we have either a CR
7329 * or an LF at <ptr>.
7330 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007331 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007332 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333 /* there is no point trying this regex on headers */
7334 return 1;
7335 }
7336 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 return done;
7338}
7339
7340
7341
7342/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007343 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7345 * unparsable response.
7346 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007347int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348{
Willy Tarreau192252e2015-04-04 01:47:55 +02007349 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007350 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007351 struct hdr_exp *exp;
7352
7353 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354 int ret;
7355
7356 /*
7357 * The interleaving of transformations and verdicts
7358 * makes it difficult to decide to continue or stop
7359 * the evaluation.
7360 */
7361
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007362 if (txn->flags & TX_SVDENY)
7363 break;
7364
Willy Tarreau3d300592007-03-18 18:34:41 +01007365 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7367 exp->action == ACT_PASS)) {
7368 exp = exp->next;
7369 continue;
7370 }
7371
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007372 /* if this filter had a condition, evaluate it now and skip to
7373 * next filter if the condition does not match.
7374 */
7375 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007376 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007377 ret = acl_pass(ret);
7378 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7379 ret = !ret;
7380 if (!ret)
7381 continue;
7382 }
7383
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007385 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007386 if (unlikely(ret < 0))
7387 return -1;
7388
7389 if (likely(ret == 0)) {
7390 /* The filter did not match the response, it can be
7391 * iterated through all headers.
7392 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007393 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7394 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007395 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 }
7397 return 0;
7398}
7399
7400
Willy Tarreaua15645d2007-03-18 16:22:39 +01007401/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007402 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007403 * desirable to call it only when needed. This function is also used when we
7404 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007405 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007406void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007408 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007409 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007410 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007411 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007413 char *hdr_beg, *hdr_end, *hdr_next;
7414 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 /* Iterate through the headers.
7417 * we start with the start line.
7418 */
7419 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007420 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421
7422 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7423 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007424 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007425
7426 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007427 hdr_beg = hdr_next;
7428 hdr_end = hdr_beg + cur_hdr->len;
7429 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 /* We have one full header between hdr_beg and hdr_end, and the
7432 * next header starts at hdr_next. We're only interested in
7433 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007434 */
7435
Willy Tarreau24581ba2010-08-31 22:39:35 +02007436 is_cookie2 = 0;
7437 prev = hdr_beg + 10;
7438 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007439 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007440 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7441 if (!val) {
7442 old_idx = cur_idx;
7443 continue;
7444 }
7445 is_cookie2 = 1;
7446 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 }
7448
Willy Tarreau24581ba2010-08-31 22:39:35 +02007449 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7450 * <prev> points to the colon.
7451 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007452 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7455 * check-cache is enabled) and we are not interested in checking
7456 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007457 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007458 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007459 return;
7460
Willy Tarreau24581ba2010-08-31 22:39:35 +02007461 /* OK so now we know we have to process this response cookie.
7462 * The format of the Set-Cookie header is slightly different
7463 * from the format of the Cookie header in that it does not
7464 * support the comma as a cookie delimiter (thus the header
7465 * cannot be folded) because the Expires attribute described in
7466 * the original Netscape's spec may contain an unquoted date
7467 * with a comma inside. We have to live with this because
7468 * many browsers don't support Max-Age and some browsers don't
7469 * support quoted strings. However the Set-Cookie2 header is
7470 * clean.
7471 *
7472 * We have to keep multiple pointers in order to support cookie
7473 * removal at the beginning, middle or end of header without
7474 * corrupting the header (in case of set-cookie2). A special
7475 * pointer, <scav> points to the beginning of the set-cookie-av
7476 * fields after the first semi-colon. The <next> pointer points
7477 * either to the end of line (set-cookie) or next unquoted comma
7478 * (set-cookie2). All of these headers are valid :
7479 *
7480 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7481 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7482 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7483 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7484 * | | | | | | | | | |
7485 * | | | | | | | | +-> next hdr_end <--+
7486 * | | | | | | | +------------> scav
7487 * | | | | | | +--------------> val_end
7488 * | | | | | +--------------------> val_beg
7489 * | | | | +----------------------> equal
7490 * | | | +------------------------> att_end
7491 * | | +----------------------------> att_beg
7492 * | +------------------------------> prev
7493 * +-----------------------------------------> hdr_beg
7494 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007495
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 for (; prev < hdr_end; prev = next) {
7497 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007498
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 /* find att_beg */
7500 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007501 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007503
Willy Tarreau24581ba2010-08-31 22:39:35 +02007504 /* find att_end : this is the first character after the last non
7505 * space before the equal. It may be equal to hdr_end.
7506 */
7507 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007508
Willy Tarreau24581ba2010-08-31 22:39:35 +02007509 while (equal < hdr_end) {
7510 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7511 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007512 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007513 continue;
7514 att_end = equal;
7515 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007516
Willy Tarreau24581ba2010-08-31 22:39:35 +02007517 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7518 * is between <att_beg> and <equal>, both may be identical.
7519 */
7520
7521 /* look for end of cookie if there is an equal sign */
7522 if (equal < hdr_end && *equal == '=') {
7523 /* look for the beginning of the value */
7524 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007525 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007526 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007527
Willy Tarreau24581ba2010-08-31 22:39:35 +02007528 /* find the end of the value, respecting quotes */
7529 next = find_cookie_value_end(val_beg, hdr_end);
7530
7531 /* make val_end point to the first white space or delimitor after the value */
7532 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007533 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007534 val_end--;
7535 } else {
7536 /* <equal> points to next comma, semi-colon or EOL */
7537 val_beg = val_end = next = equal;
7538 }
7539
7540 if (next < hdr_end) {
7541 /* Set-Cookie2 supports multiple cookies, and <next> points to
7542 * a colon or semi-colon before the end. So skip all attr-value
7543 * pairs and look for the next comma. For Set-Cookie, since
7544 * commas are permitted in values, skip to the end.
7545 */
7546 if (is_cookie2)
7547 next = find_hdr_value_end(next, hdr_end);
7548 else
7549 next = hdr_end;
7550 }
7551
7552 /* Now everything is as on the diagram above */
7553
7554 /* Ignore cookies with no equal sign */
7555 if (equal == val_end)
7556 continue;
7557
7558 /* If there are spaces around the equal sign, we need to
7559 * strip them otherwise we'll get trouble for cookie captures,
7560 * or even for rewrites. Since this happens extremely rarely,
7561 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007562 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007563 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7564 int stripped_before = 0;
7565 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566
Willy Tarreau24581ba2010-08-31 22:39:35 +02007567 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007568 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007569 equal += stripped_before;
7570 val_beg += stripped_before;
7571 }
7572
7573 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007574 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007575 val_beg += stripped_after;
7576 stripped_before += stripped_after;
7577 }
7578
7579 val_end += stripped_before;
7580 next += stripped_before;
7581 hdr_end += stripped_before;
7582 hdr_next += stripped_before;
7583 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007584 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 }
7586
7587 /* First, let's see if we want to capture this cookie. We check
7588 * that we don't already have a server side cookie, because we
7589 * can only capture one. Also as an optimisation, we ignore
7590 * cookies shorter than the declared name.
7591 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007592 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007593 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007594 (val_end - att_beg >= sess->fe->capture_namelen) &&
7595 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007596 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007597 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007598 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007599 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007600 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007601 if (log_len > sess->fe->capture_len)
7602 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007603 memcpy(txn->srv_cookie, att_beg, log_len);
7604 txn->srv_cookie[log_len] = 0;
7605 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007606 }
7607
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007608 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007610 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007611 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7612 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007613 /* assume passive cookie by default */
7614 txn->flags &= ~TX_SCK_MASK;
7615 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616
7617 /* If the cookie is in insert mode on a known server, we'll delete
7618 * this occurrence because we'll insert another one later.
7619 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007620 * a direct access.
7621 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007622 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007623 /* The "preserve" flag was set, we don't want to touch the
7624 * server's cookie.
7625 */
7626 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007627 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007628 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007629 /* this cookie must be deleted */
7630 if (*prev == ':' && next == hdr_end) {
7631 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007632 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007633 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7634 txn->hdr_idx.used--;
7635 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007636 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007637 hdr_next += delta;
7638 http_msg_move_end(&txn->rsp, delta);
7639 /* note: while both invalid now, <next> and <hdr_end>
7640 * are still equal, so the for() will stop as expected.
7641 */
7642 } else {
7643 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007644 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007645 next = prev;
7646 hdr_end += delta;
7647 hdr_next += delta;
7648 cur_hdr->len += delta;
7649 http_msg_move_end(&txn->rsp, delta);
7650 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007651 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007652 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007653 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007655 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007656 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657 * with this server since we know it.
7658 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007659 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007660 next += delta;
7661 hdr_end += delta;
7662 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007663 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007664 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665
Willy Tarreauf1348312010-10-07 15:54:11 +02007666 txn->flags &= ~TX_SCK_MASK;
7667 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007668 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007669 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007671 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007672 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007673 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007674 next += delta;
7675 hdr_end += delta;
7676 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007678 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679
Willy Tarreau827aee92011-03-10 16:55:02 +01007680 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007681 txn->flags &= ~TX_SCK_MASK;
7682 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683 }
7684 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007685 /* that's done for this cookie, check the next one on the same
7686 * line when next != hdr_end (only if is_cookie2).
7687 */
7688 }
7689 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007691 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692}
7693
7694
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007696 * Parses the Cache-Control and Pragma request header fields to determine if
7697 * the request may be served from the cache and/or if it is cacheable. Updates
7698 * s->txn->flags.
7699 */
7700void check_request_for_cacheability(struct stream *s, struct channel *chn)
7701{
7702 struct http_txn *txn = s->txn;
7703 char *p1, *p2;
7704 char *cur_ptr, *cur_end, *cur_next;
7705 int pragma_found;
7706 int cc_found;
7707 int cur_idx;
7708
7709 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7710 return; /* nothing more to do here */
7711
7712 cur_idx = 0;
7713 pragma_found = cc_found = 0;
7714 cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
7715
7716 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7717 struct hdr_idx_elem *cur_hdr;
7718 int val;
7719
7720 cur_hdr = &txn->hdr_idx.v[cur_idx];
7721 cur_ptr = cur_next;
7722 cur_end = cur_ptr + cur_hdr->len;
7723 cur_next = cur_end + cur_hdr->cr + 1;
7724
7725 /* We have one full header between cur_ptr and cur_end, and the
7726 * next header starts at cur_next.
7727 */
7728
7729 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7730 if (val) {
7731 if ((cur_end - (cur_ptr + val) >= 8) &&
7732 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7733 pragma_found = 1;
7734 continue;
7735 }
7736 }
7737
7738 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7739 if (!val)
7740 continue;
7741
7742 /* OK, right now we know we have a cache-control header at cur_ptr */
7743 cc_found = 1;
7744 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7745
7746 if (p1 >= cur_end) /* no more info */
7747 continue;
7748
7749 /* p1 is at the beginning of the value */
7750 p2 = p1;
7751 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7752 p2++;
7753
7754 /* we have a complete value between p1 and p2. We don't check the
7755 * values after max-age, max-stale nor min-fresh, we simply don't
7756 * use the cache when they're specified.
7757 */
7758 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7759 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7760 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7761 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7762 txn->flags |= TX_CACHE_IGNORE;
7763 continue;
7764 }
7765
7766 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7767 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7768 continue;
7769 }
7770 }
7771
7772 /* RFC7234#5.4:
7773 * When the Cache-Control header field is also present and
7774 * understood in a request, Pragma is ignored.
7775 * When the Cache-Control header field is not present in a
7776 * request, caches MUST consider the no-cache request
7777 * pragma-directive as having the same effect as if
7778 * "Cache-Control: no-cache" were present.
7779 */
7780 if (!cc_found && pragma_found)
7781 txn->flags |= TX_CACHE_IGNORE;
7782}
7783
7784/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007785 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007786 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007787void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007788{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007789 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790 char *p1, *p2;
7791
7792 char *cur_ptr, *cur_end, *cur_next;
7793 int cur_idx;
7794
Willy Tarreau12b32f22017-12-21 16:08:09 +01007795 if (txn->status < 200) {
7796 /* do not try to cache interim responses! */
7797 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007798 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007799 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007800
7801 /* Iterate through the headers.
7802 * we start with the start line.
7803 */
7804 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007805 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007806
7807 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7808 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007809 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810
7811 cur_hdr = &txn->hdr_idx.v[cur_idx];
7812 cur_ptr = cur_next;
7813 cur_end = cur_ptr + cur_hdr->len;
7814 cur_next = cur_end + cur_hdr->cr + 1;
7815
7816 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007817 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818 */
7819
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007820 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7821 if (val) {
7822 if ((cur_end - (cur_ptr + val) >= 8) &&
7823 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7824 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7825 return;
7826 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007827 }
7828
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007829 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7830 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831 continue;
7832
7833 /* OK, right now we know we have a cache-control header at cur_ptr */
7834
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007835 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007836
7837 if (p1 >= cur_end) /* no more info */
7838 continue;
7839
7840 /* p1 is at the beginning of the value */
7841 p2 = p1;
7842
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007843 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844 p2++;
7845
7846 /* we have a complete value between p1 and p2 */
7847 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007848 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7849 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7850 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7851 continue;
7852 }
7853
Willy Tarreaua15645d2007-03-18 16:22:39 +01007854 /* we have something of the form no-cache="set-cookie" */
7855 if ((cur_end - p1 >= 21) &&
7856 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7857 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007858 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007859 continue;
7860 }
7861
7862 /* OK, so we know that either p2 points to the end of string or to a comma */
7863 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007864 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007865 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007866 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867 return;
7868 }
7869
7870 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007871 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872 continue;
7873 }
7874 }
7875}
7876
Willy Tarreau58f10d72006-12-04 02:26:12 +01007877
Willy Tarreaub2513902006-12-17 14:52:38 +01007878/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007879 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007880 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007881 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007882 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007883 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007884 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007885 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007886 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007887int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007888{
7889 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007890 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007891 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007892
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007893 if (!uri_auth)
7894 return 0;
7895
Cyril Bonté70be45d2010-10-12 00:14:35 +02007896 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007897 return 0;
7898
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007899 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007900 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007901 return 0;
7902
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007903 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007904 return 0;
7905
Willy Tarreaub2513902006-12-17 14:52:38 +01007906 return 1;
7907}
7908
Willy Tarreau4076a152009-04-02 15:18:36 +02007909/*
7910 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007911 * By default it tries to report the error position as msg->err_pos. However if
7912 * this one is not set, it will then report msg->next, which is the last known
7913 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007914 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007915 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007916void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007917 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007918 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007919{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007920 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007921 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007922 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007923
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007924 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007925 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007926 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007927 len1 = MIN(len1, es->len);
7928 len2 = es->len - len1; /* remaining data if buffer wraps */
7929
Willy Tarreauf3764b72016-03-31 13:45:10 +02007930 if (!es->buf)
7931 es->buf = malloc(global.tune.bufsize);
7932
7933 if (es->buf) {
7934 memcpy(es->buf, chn->buf->p, len1);
7935 if (len2)
7936 memcpy(es->buf + len1, chn->buf->data, len2);
7937 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007938
Willy Tarreau4076a152009-04-02 15:18:36 +02007939 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007940 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007941 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007942 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007943
Willy Tarreau4076a152009-04-02 15:18:36 +02007944 es->when = date; // user-visible date
7945 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007946 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007947 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007948 if (objt_conn(sess->origin))
7949 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007950 else
7951 memset(&es->src, 0, sizeof(es->src));
7952
Willy Tarreau078272e2010-12-12 12:46:33 +01007953 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007954 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007955 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007956 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007957 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007958 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007959 es->b_out = chn->buf->o;
7960 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007961 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007962 es->m_clen = msg->chunk_len;
7963 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007964 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007965}
Willy Tarreaub2513902006-12-17 14:52:38 +01007966
Willy Tarreau294c4732011-12-16 21:35:50 +01007967/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7968 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7969 * performed over the whole headers. Otherwise it must contain a valid header
7970 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7971 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7972 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7973 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007974 * -1. The value fetch stops at commas, so this function is suited for use with
7975 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007976 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007977 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007978unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007979 struct hdr_idx *idx, int occ,
7980 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007981{
Willy Tarreau294c4732011-12-16 21:35:50 +01007982 struct hdr_ctx local_ctx;
7983 char *ptr_hist[MAX_HDR_HISTORY];
7984 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007985 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007986 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007987
Willy Tarreau294c4732011-12-16 21:35:50 +01007988 if (!ctx) {
7989 local_ctx.idx = 0;
7990 ctx = &local_ctx;
7991 }
7992
Willy Tarreaubce70882009-09-07 11:51:47 +02007993 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007994 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007995 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007996 occ--;
7997 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007998 *vptr = ctx->line + ctx->val;
7999 *vlen = ctx->vlen;
8000 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008001 }
8002 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008003 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008004 }
8005
8006 /* negative occurrence, we scan all the list then walk back */
8007 if (-occ > MAX_HDR_HISTORY)
8008 return 0;
8009
Willy Tarreau294c4732011-12-16 21:35:50 +01008010 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008011 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008012 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8013 len_hist[hist_ptr] = ctx->vlen;
8014 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008015 hist_ptr = 0;
8016 found++;
8017 }
8018 if (-occ > found)
8019 return 0;
8020 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008021 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8022 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8023 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008024 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008025 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008026 if (hist_ptr >= MAX_HDR_HISTORY)
8027 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008028 *vptr = ptr_hist[hist_ptr];
8029 *vlen = len_hist[hist_ptr];
8030 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008031}
8032
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008033/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8034 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8035 * performed over the whole headers. Otherwise it must contain a valid header
8036 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8037 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8038 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8039 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8040 * -1. This function differs from http_get_hdr() in that it only returns full
8041 * line header values and does not stop at commas.
8042 * The return value is 0 if nothing was found, or non-zero otherwise.
8043 */
8044unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8045 struct hdr_idx *idx, int occ,
8046 struct hdr_ctx *ctx, char **vptr, int *vlen)
8047{
8048 struct hdr_ctx local_ctx;
8049 char *ptr_hist[MAX_HDR_HISTORY];
8050 int len_hist[MAX_HDR_HISTORY];
8051 unsigned int hist_ptr;
8052 int found;
8053
8054 if (!ctx) {
8055 local_ctx.idx = 0;
8056 ctx = &local_ctx;
8057 }
8058
8059 if (occ >= 0) {
8060 /* search from the beginning */
8061 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8062 occ--;
8063 if (occ <= 0) {
8064 *vptr = ctx->line + ctx->val;
8065 *vlen = ctx->vlen;
8066 return 1;
8067 }
8068 }
8069 return 0;
8070 }
8071
8072 /* negative occurrence, we scan all the list then walk back */
8073 if (-occ > MAX_HDR_HISTORY)
8074 return 0;
8075
8076 found = hist_ptr = 0;
8077 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8078 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8079 len_hist[hist_ptr] = ctx->vlen;
8080 if (++hist_ptr >= MAX_HDR_HISTORY)
8081 hist_ptr = 0;
8082 found++;
8083 }
8084 if (-occ > found)
8085 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008086
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008087 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008088 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8089 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8090 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008091 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008092 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008093 if (hist_ptr >= MAX_HDR_HISTORY)
8094 hist_ptr -= MAX_HDR_HISTORY;
8095 *vptr = ptr_hist[hist_ptr];
8096 *vlen = len_hist[hist_ptr];
8097 return 1;
8098}
8099
Willy Tarreaubaaee002006-06-26 02:48:02 +02008100/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008101 * Print a debug line with a header. Always stop at the first CR or LF char,
8102 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8103 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008104 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008105void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008106{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008107 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008108 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008109
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008110 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008111 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008112 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008113 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 +02008114
8115 for (max = 0; start + max < end; max++)
8116 if (start[max] == '\r' || start[max] == '\n')
8117 break;
8118
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008119 UBOUND(max, trash.size - trash.len - 3);
8120 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8121 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008122 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008123}
8124
Willy Tarreaueee5b512015-04-03 23:46:31 +02008125
8126/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8127 * The hdr_idx is allocated as well. In case of allocation failure, everything
8128 * allocated is freed and NULL is returned. Otherwise the new transaction is
8129 * assigned to the stream and returned.
8130 */
8131struct http_txn *http_alloc_txn(struct stream *s)
8132{
8133 struct http_txn *txn = s->txn;
8134
8135 if (txn)
8136 return txn;
8137
Willy Tarreaubafbe012017-11-24 17:34:44 +01008138 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008139 if (!txn)
8140 return txn;
8141
8142 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008143 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008144 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008145 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008146 return NULL;
8147 }
8148
8149 s->txn = txn;
8150 return txn;
8151}
8152
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008153void http_txn_reset_req(struct http_txn *txn)
8154{
8155 txn->req.flags = 0;
8156 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8157 txn->req.next = 0;
8158 txn->req.chunk_len = 0LL;
8159 txn->req.body_len = 0LL;
8160 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8161}
8162
8163void http_txn_reset_res(struct http_txn *txn)
8164{
8165 txn->rsp.flags = 0;
8166 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8167 txn->rsp.next = 0;
8168 txn->rsp.chunk_len = 0LL;
8169 txn->rsp.body_len = 0LL;
8170 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8171}
8172
Willy Tarreau0937bc42009-12-22 15:03:09 +01008173/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008174 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008175 * the required fields are properly allocated and that we only need to (re)init
8176 * them. This should be used before processing any new request.
8177 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008178void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008179{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008180 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008181 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008182
8183 txn->flags = 0;
8184 txn->status = -1;
8185
Willy Tarreauf64d1412010-10-07 20:06:11 +02008186 txn->cookie_first_date = 0;
8187 txn->cookie_last_date = 0;
8188
Willy Tarreaueee5b512015-04-03 23:46:31 +02008189 txn->srv_cookie = NULL;
8190 txn->cli_cookie = NULL;
8191 txn->uri = NULL;
8192
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008193 http_txn_reset_req(txn);
8194 http_txn_reset_res(txn);
8195
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008196 txn->req.chn = &s->req;
8197 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008198
8199 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008200
8201 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8202 if (fe->options2 & PR_O2_REQBUG_OK)
8203 txn->req.err_pos = -1; /* let buggy requests pass */
8204
Willy Tarreau0937bc42009-12-22 15:03:09 +01008205 if (txn->hdr_idx.v)
8206 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008207
8208 vars_init(&s->vars_txn, SCOPE_TXN);
8209 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008210}
8211
8212/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008213void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008214{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008215 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008216 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008217
8218 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008219 pool_free(pool_head_requri, txn->uri);
8220 pool_free(pool_head_capture, txn->cli_cookie);
8221 pool_free(pool_head_capture, txn->srv_cookie);
8222 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008223
William Lallemanda73203e2012-03-12 12:48:57 +01008224 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008225 txn->uri = NULL;
8226 txn->srv_cookie = NULL;
8227 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008228
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008229 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008230 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008231 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008232 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008233 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008234 }
8235
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008236 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008237 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008238 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008239 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008240 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008241 }
8242
Willy Tarreau6204cd92016-03-10 16:33:04 +01008243 vars_prune(&s->vars_txn, s->sess, s);
8244 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008245}
8246
8247/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008248void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008249{
8250 http_end_txn(s);
8251 http_init_txn(s);
8252
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008253 /* reinitialise the current rule list pointer to NULL. We are sure that
8254 * any rulelist match the NULL pointer.
8255 */
8256 s->current_rule_list = NULL;
8257
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008258 s->be = strm_fe(s);
8259 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008260 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008261 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008262 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008263 /* re-init store persistence */
8264 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008265 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008266
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008267 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008268
Willy Tarreau739cfba2010-01-25 23:11:14 +01008269 /* We must trim any excess data from the response buffer, because we
8270 * may have blocked an invalid response from a server that we don't
8271 * want to accidentely forward once we disable the analysers, nor do
8272 * we want those data to come along with next response. A typical
8273 * example of such data would be from a buggy server responding to
8274 * a HEAD with some data, or sending more than the advertised
8275 * content-length.
8276 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008277 if (unlikely(s->res.buf->i))
8278 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008279
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008280 /* Now we can realign the response buffer */
8281 buffer_realign(s->res.buf);
8282
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008283 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008284 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008285
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008286 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008287 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008288
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008289 s->req.rex = TICK_ETERNITY;
8290 s->req.wex = TICK_ETERNITY;
8291 s->req.analyse_exp = TICK_ETERNITY;
8292 s->res.rex = TICK_ETERNITY;
8293 s->res.wex = TICK_ETERNITY;
8294 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008295 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008296}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008297
Sasha Pachev218f0642014-06-16 12:05:59 -06008298void free_http_res_rules(struct list *r)
8299{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008300 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008301
8302 list_for_each_entry_safe(pr, tr, r, list) {
8303 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008304 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008305 free(pr);
8306 }
8307}
8308
8309void free_http_req_rules(struct list *r)
8310{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008311 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008312
8313 list_for_each_entry_safe(pr, tr, r, list) {
8314 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008315 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008316 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008317
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008318 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008319 free(pr);
8320 }
8321}
8322
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008323/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008324struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008325{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008326 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008327 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008328 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008329 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008330
Vincent Bernat02779b62016-04-03 13:48:43 +02008331 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008332 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008333 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008334 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008335 }
8336
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008337 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008338 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008339 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008340 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008341 int code;
8342 int hc;
8343
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008344 if (!strcmp(args[0], "tarpit")) {
8345 rule->action = ACT_HTTP_REQ_TARPIT;
8346 rule->deny_status = HTTP_ERR_500;
8347 }
8348 else {
8349 rule->action = ACT_ACTION_DENY;
8350 rule->deny_status = HTTP_ERR_403;
8351 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008352 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008353 if (strcmp(args[cur_arg], "deny_status") == 0) {
8354 cur_arg++;
8355 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008356 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8357 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008358 goto out_err;
8359 }
8360
8361 code = atol(args[cur_arg]);
8362 cur_arg++;
8363 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8364 if (http_err_codes[hc] == code) {
8365 rule->deny_status = hc;
8366 break;
8367 }
8368 }
8369
8370 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008371 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8372 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008373 }
8374 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008375 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008376 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008377 cur_arg = 1;
8378
8379 while(*args[cur_arg]) {
8380 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008381 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008382 cur_arg+=2;
8383 continue;
8384 } else
8385 break;
8386 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008387 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008388 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008389 cur_arg = 1;
8390
8391 if (!*args[cur_arg] ||
8392 (*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 +01008393 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8394 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008395 goto out_err;
8396 }
8397 rule->arg.nice = atoi(args[cur_arg]);
8398 if (rule->arg.nice < -1024)
8399 rule->arg.nice = -1024;
8400 else if (rule->arg.nice > 1024)
8401 rule->arg.nice = 1024;
8402 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008403 } else if (!strcmp(args[0], "set-tos")) {
8404#ifdef IP_TOS
8405 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008406 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008407 cur_arg = 1;
8408
8409 if (!*args[cur_arg] ||
8410 (*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 +01008411 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8412 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008413 goto out_err;
8414 }
8415
8416 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8417 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008418 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8419 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008420 goto out_err;
8421 }
8422 cur_arg++;
8423#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008424 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 +02008425 goto out_err;
8426#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008427 } else if (!strcmp(args[0], "set-mark")) {
8428#ifdef SO_MARK
8429 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008430 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008431 cur_arg = 1;
8432
8433 if (!*args[cur_arg] ||
8434 (*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 +01008435 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8436 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008437 goto out_err;
8438 }
8439
8440 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8441 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008442 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8443 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008444 goto out_err;
8445 }
8446 cur_arg++;
8447 global.last_checks |= LSTCHK_NETADM;
8448#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008449 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 +02008450 goto out_err;
8451#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008452 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008453 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008454 cur_arg = 1;
8455
8456 if (!*args[cur_arg] ||
8457 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8458 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008459 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8460 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008461 goto out_err;
8462 }
8463 if (strcmp(args[cur_arg], "silent") == 0)
8464 rule->arg.loglevel = -1;
8465 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8466 goto bad_log_level;
8467 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008468 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008469 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008470 cur_arg = 1;
8471
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008472 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8473 (*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 +01008474 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8475 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008476 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008477 }
8478
8479 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8480 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8481 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008482
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008483 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008484 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008485 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 +01008486 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008487 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8488 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008489 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008490 goto out_err;
8491 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008492 free(proxy->conf.lfs_file);
8493 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8494 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008495 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008496 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008497 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008498 cur_arg = 1;
8499
8500 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008501 (*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 +01008502 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8503 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008504 goto out_err;
8505 }
8506
8507 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8508 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8509 LIST_INIT(&rule->arg.hdr_add.fmt);
8510
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008511 error = NULL;
8512 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008513 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8514 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008515 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008516 goto out_err;
8517 }
8518
8519 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008520 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008521 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 +01008522 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008523 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8524 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008525 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008526 goto out_err;
8527 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008528
8529 free(proxy->conf.lfs_file);
8530 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8531 proxy->conf.lfs_line = proxy->conf.args.line;
8532 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008533 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008534 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008535 cur_arg = 1;
8536
8537 if (!*args[cur_arg] ||
8538 (*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 +01008539 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8540 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008541 goto out_err;
8542 }
8543
8544 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8545 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8546
8547 proxy->conf.args.ctx = ARGC_HRQ;
8548 free(proxy->conf.lfs_file);
8549 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8550 proxy->conf.lfs_line = proxy->conf.args.line;
8551 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008552 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008553 struct sample_expr *expr;
8554 unsigned int where;
8555 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008556 unsigned int tsc_num;
8557 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008558
8559 cur_arg = 1;
8560 proxy->conf.args.ctx = ARGC_TRK;
8561
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008562 tsc_num_str = &args[0][8];
8563 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8564 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8565 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8566 free(err);
8567 goto out_err;
8568 }
8569
Willy Tarreau09448f72014-06-25 18:12:15 +02008570 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8571 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008572 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8573 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008574 free(err);
8575 goto out_err;
8576 }
8577
8578 where = 0;
8579 if (proxy->cap & PR_CAP_FE)
8580 where |= SMP_VAL_FE_HRQ_HDR;
8581 if (proxy->cap & PR_CAP_BE)
8582 where |= SMP_VAL_BE_HRQ_HDR;
8583
8584 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008585 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8586 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8587 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8588 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008589 free(expr);
8590 goto out_err;
8591 }
8592
8593 if (strcmp(args[cur_arg], "table") == 0) {
8594 cur_arg++;
8595 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008596 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8597 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008598 free(expr);
8599 goto out_err;
8600 }
8601 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008602 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008603 cur_arg++;
8604 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008605 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008606 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008607 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008608 } else if (strcmp(args[0], "redirect") == 0) {
8609 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008610 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008611
Willy Tarreaube4653b2015-05-28 15:26:58 +02008612 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008613 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8614 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008615 goto out_err;
8616 }
8617
8618 /* this redirect rule might already contain a parsed condition which
8619 * we'll pass to the http-request rule.
8620 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008621 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008622 rule->arg.redir = redir;
8623 rule->cond = redir->cond;
8624 redir->cond = NULL;
8625 cur_arg = 2;
8626 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008627 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8628 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008629 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008630 /*
8631 * '+ 8' for 'add-acl('
8632 * '- 9' for 'add-acl(' + trailing ')'
8633 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008634 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008635
8636 cur_arg = 1;
8637
8638 if (!*args[cur_arg] ||
8639 (*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 +01008640 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8641 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008642 goto out_err;
8643 }
8644
8645 LIST_INIT(&rule->arg.map.key);
8646 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008647 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008648 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008649 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008650 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8651 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008652 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008653 goto out_err;
8654 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008655 free(proxy->conf.lfs_file);
8656 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8657 proxy->conf.lfs_line = proxy->conf.args.line;
8658 cur_arg += 1;
8659 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8660 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008661 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008662 /*
8663 * '+ 8' for 'del-acl('
8664 * '- 9' for 'del-acl(' + trailing ')'
8665 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008666 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008667
8668 cur_arg = 1;
8669
8670 if (!*args[cur_arg] ||
8671 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008672 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8673 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008674 goto out_err;
8675 }
8676
8677 LIST_INIT(&rule->arg.map.key);
8678 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008679 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008680 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008681 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008682 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8683 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008684 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008685 goto out_err;
8686 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008687 free(proxy->conf.lfs_file);
8688 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8689 proxy->conf.lfs_line = proxy->conf.args.line;
8690 cur_arg += 1;
8691 } else if (strncmp(args[0], "del-map", 7) == 0) {
8692 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008693 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008694 /*
8695 * '+ 8' for 'del-map('
8696 * '- 9' for 'del-map(' + trailing ')'
8697 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008698 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008699
8700 cur_arg = 1;
8701
8702 if (!*args[cur_arg] ||
8703 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008704 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8705 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008706 goto out_err;
8707 }
8708
8709 LIST_INIT(&rule->arg.map.key);
8710 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008711 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008712 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008713 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008714 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8715 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008716 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008717 goto out_err;
8718 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008719 free(proxy->conf.lfs_file);
8720 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8721 proxy->conf.lfs_line = proxy->conf.args.line;
8722 cur_arg += 1;
8723 } else if (strncmp(args[0], "set-map", 7) == 0) {
8724 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008725 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008726 /*
8727 * '+ 8' for 'set-map('
8728 * '- 9' for 'set-map(' + trailing ')'
8729 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008730 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008731
8732 cur_arg = 1;
8733
8734 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8735 (*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 +01008736 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8737 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008738 goto out_err;
8739 }
8740
8741 LIST_INIT(&rule->arg.map.key);
8742 LIST_INIT(&rule->arg.map.value);
8743 proxy->conf.args.ctx = ARGC_HRQ;
8744
8745 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008746 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008747 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008748 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008749 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8750 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008751 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008752 goto out_err;
8753 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008754
8755 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008756 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008757 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 +01008758 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008759 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8760 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008761 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008762 goto out_err;
8763 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008764 free(proxy->conf.lfs_file);
8765 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8766 proxy->conf.lfs_line = proxy->conf.args.line;
8767
8768 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008769 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8770 char *errmsg = NULL;
8771 cur_arg = 1;
8772 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008773 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008774 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008775 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008776 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8777 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008778 free(errmsg);
8779 goto out_err;
8780 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008781 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008782 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008783 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8784 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8785 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8786 "%s%s, but got '%s'%s.\n",
8787 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008788 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008789 }
8790
8791 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8792 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008793 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008794
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008795 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008796 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8797 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008798 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008799 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008800 }
8801 rule->cond = cond;
8802 }
8803 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008804 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8805 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8806 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008807 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008808 }
8809
8810 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008811 out_err:
8812 free(rule);
8813 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008814}
8815
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008816/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008817struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008818{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008819 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008820 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008821 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008822 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008823
8824 rule = calloc(1, sizeof(*rule));
8825 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008826 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008827 goto out_err;
8828 }
8829
8830 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008831 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008832 cur_arg = 1;
8833 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008834 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008835 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008836 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008837 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008838 cur_arg = 1;
8839
8840 if (!*args[cur_arg] ||
8841 (*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 +01008842 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8843 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008844 goto out_err;
8845 }
8846 rule->arg.nice = atoi(args[cur_arg]);
8847 if (rule->arg.nice < -1024)
8848 rule->arg.nice = -1024;
8849 else if (rule->arg.nice > 1024)
8850 rule->arg.nice = 1024;
8851 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008852 } else if (!strcmp(args[0], "set-tos")) {
8853#ifdef IP_TOS
8854 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008855 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008856 cur_arg = 1;
8857
8858 if (!*args[cur_arg] ||
8859 (*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 +01008860 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8861 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008862 goto out_err;
8863 }
8864
8865 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8866 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008867 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8868 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008869 goto out_err;
8870 }
8871 cur_arg++;
8872#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008873 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 +02008874 goto out_err;
8875#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008876 } else if (!strcmp(args[0], "set-mark")) {
8877#ifdef SO_MARK
8878 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008879 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008880 cur_arg = 1;
8881
8882 if (!*args[cur_arg] ||
8883 (*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 +01008884 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8885 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008886 goto out_err;
8887 }
8888
8889 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8890 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008891 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8892 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008893 goto out_err;
8894 }
8895 cur_arg++;
8896 global.last_checks |= LSTCHK_NETADM;
8897#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008898 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 +02008899 goto out_err;
8900#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008901 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008902 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008903 cur_arg = 1;
8904
8905 if (!*args[cur_arg] ||
8906 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8907 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008908 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8909 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008910 goto out_err;
8911 }
8912 if (strcmp(args[cur_arg], "silent") == 0)
8913 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008914 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008915 goto bad_log_level;
8916 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008917 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008918 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008919 cur_arg = 1;
8920
8921 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8922 (*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 +01008923 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8924 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008925 goto out_err;
8926 }
8927
8928 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8929 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8930 LIST_INIT(&rule->arg.hdr_add.fmt);
8931
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008932 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008933 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008934 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 +01008935 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008936 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8937 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008938 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008939 goto out_err;
8940 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008941 free(proxy->conf.lfs_file);
8942 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8943 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008944 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008945 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008946 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008947 cur_arg = 1;
8948
8949 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008950 (*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 +01008951 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8952 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008953 goto out_err;
8954 }
8955
8956 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8957 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8958 LIST_INIT(&rule->arg.hdr_add.fmt);
8959
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008960 error = NULL;
8961 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008962 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8963 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008964 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008965 goto out_err;
8966 }
8967
8968 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008969 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008970 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 +01008971 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008972 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8973 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008974 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008975 goto out_err;
8976 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008977
8978 free(proxy->conf.lfs_file);
8979 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8980 proxy->conf.lfs_line = proxy->conf.args.line;
8981 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008982 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008983 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008984 cur_arg = 1;
8985
8986 if (!*args[cur_arg] ||
8987 (*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 +01008988 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8989 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008990 goto out_err;
8991 }
8992
8993 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8994 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8995
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008996 proxy->conf.args.ctx = ARGC_HRS;
8997 free(proxy->conf.lfs_file);
8998 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8999 proxy->conf.lfs_line = proxy->conf.args.line;
9000 cur_arg += 1;
9001 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9002 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009003 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009004 /*
9005 * '+ 8' for 'add-acl('
9006 * '- 9' for 'add-acl(' + trailing ')'
9007 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009008 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009009
9010 cur_arg = 1;
9011
9012 if (!*args[cur_arg] ||
9013 (*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 +01009014 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9015 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009016 goto out_err;
9017 }
9018
9019 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009020 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009021 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009022 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009023 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009024 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9025 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009026 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009027 goto out_err;
9028 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009029 free(proxy->conf.lfs_file);
9030 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9031 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009032
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009033 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009034 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9035 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009036 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009037 /*
9038 * '+ 8' for 'del-acl('
9039 * '- 9' for 'del-acl(' + trailing ')'
9040 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009041 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009042
9043 cur_arg = 1;
9044
9045 if (!*args[cur_arg] ||
9046 (*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 +01009047 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9048 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009049 goto out_err;
9050 }
9051
9052 LIST_INIT(&rule->arg.map.key);
9053 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009054 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009055 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009056 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009057 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9058 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009059 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009060 goto out_err;
9061 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009062 free(proxy->conf.lfs_file);
9063 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9064 proxy->conf.lfs_line = proxy->conf.args.line;
9065 cur_arg += 1;
9066 } else if (strncmp(args[0], "del-map", 7) == 0) {
9067 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009068 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009069 /*
9070 * '+ 8' for 'del-map('
9071 * '- 9' for 'del-map(' + trailing ')'
9072 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009073 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009074
9075 cur_arg = 1;
9076
9077 if (!*args[cur_arg] ||
9078 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009079 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9080 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009081 goto out_err;
9082 }
9083
9084 LIST_INIT(&rule->arg.map.key);
9085 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009086 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009087 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009088 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009089 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9090 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009091 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009092 goto out_err;
9093 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009094 free(proxy->conf.lfs_file);
9095 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9096 proxy->conf.lfs_line = proxy->conf.args.line;
9097 cur_arg += 1;
9098 } else if (strncmp(args[0], "set-map", 7) == 0) {
9099 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009100 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009101 /*
9102 * '+ 8' for 'set-map('
9103 * '- 9' for 'set-map(' + trailing ')'
9104 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009105 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009106
9107 cur_arg = 1;
9108
9109 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9110 (*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 +01009111 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9112 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009113 goto out_err;
9114 }
9115
9116 LIST_INIT(&rule->arg.map.key);
9117 LIST_INIT(&rule->arg.map.value);
9118
9119 proxy->conf.args.ctx = ARGC_HRS;
9120
9121 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009122 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009123 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009124 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009125 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9126 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009127 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009128 goto out_err;
9129 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009130
9131 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009132 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009133 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 +01009134 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009135 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9136 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009137 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009138 goto out_err;
9139 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009140
9141 free(proxy->conf.lfs_file);
9142 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9143 proxy->conf.lfs_line = proxy->conf.args.line;
9144
9145 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009146 } else if (strcmp(args[0], "redirect") == 0) {
9147 struct redirect_rule *redir;
9148 char *errmsg = NULL;
9149
9150 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009151 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9152 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009153 goto out_err;
9154 }
9155
9156 /* this redirect rule might already contain a parsed condition which
9157 * we'll pass to the http-request rule.
9158 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009159 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009160 rule->arg.redir = redir;
9161 rule->cond = redir->cond;
9162 redir->cond = NULL;
9163 cur_arg = 2;
9164 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009165 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009166 struct sample_expr *expr;
9167 unsigned int where;
9168 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009169 unsigned int tsc_num;
9170 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009171
9172 cur_arg = 1;
9173 proxy->conf.args.ctx = ARGC_TRK;
9174
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009175 tsc_num_str = &args[0][8];
9176 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9177 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9178 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9179 free(err);
9180 goto out_err;
9181 }
9182
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009183 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9184 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009185 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9186 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009187 free(err);
9188 goto out_err;
9189 }
9190
9191 where = 0;
9192 if (proxy->cap & PR_CAP_FE)
9193 where |= SMP_VAL_FE_HRS_HDR;
9194 if (proxy->cap & PR_CAP_BE)
9195 where |= SMP_VAL_BE_HRS_HDR;
9196
9197 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009198 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9199 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9200 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9201 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009202 free(expr);
9203 goto out_err;
9204 }
9205
9206 if (strcmp(args[cur_arg], "table") == 0) {
9207 cur_arg++;
9208 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009209 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9210 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009211 free(expr);
9212 goto out_err;
9213 }
9214 /* we copy the table name for now, it will be resolved later */
9215 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9216 cur_arg++;
9217 }
9218 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009219 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009220 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009221 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9222 char *errmsg = NULL;
9223 cur_arg = 1;
9224 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009225 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009226 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009227 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009228 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9229 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009230 free(errmsg);
9231 goto out_err;
9232 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009233 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009234 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009235 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9236 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9237 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9238 "%s%s, but got '%s'%s.\n",
9239 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009240 goto out_err;
9241 }
9242
9243 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9244 struct acl_cond *cond;
9245 char *errmsg = NULL;
9246
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009247 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009248 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9249 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009250 free(errmsg);
9251 goto out_err;
9252 }
9253 rule->cond = cond;
9254 }
9255 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009256 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9257 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9258 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009259 goto out_err;
9260 }
9261
9262 return rule;
9263 out_err:
9264 free(rule);
9265 return NULL;
9266}
9267
Willy Tarreau4baae242012-12-27 12:00:31 +01009268/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009269 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009270 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9271 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009272 */
9273struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009274 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009275{
9276 struct redirect_rule *rule;
9277 int cur_arg;
9278 int type = REDIRECT_TYPE_NONE;
9279 int code = 302;
9280 const char *destination = NULL;
9281 const char *cookie = NULL;
9282 int cookie_set = 0;
9283 unsigned int flags = REDIRECT_FLAG_NONE;
9284 struct acl_cond *cond = NULL;
9285
9286 cur_arg = 0;
9287 while (*(args[cur_arg])) {
9288 if (strcmp(args[cur_arg], "location") == 0) {
9289 if (!*args[cur_arg + 1])
9290 goto missing_arg;
9291
9292 type = REDIRECT_TYPE_LOCATION;
9293 cur_arg++;
9294 destination = args[cur_arg];
9295 }
9296 else if (strcmp(args[cur_arg], "prefix") == 0) {
9297 if (!*args[cur_arg + 1])
9298 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009299 type = REDIRECT_TYPE_PREFIX;
9300 cur_arg++;
9301 destination = args[cur_arg];
9302 }
9303 else if (strcmp(args[cur_arg], "scheme") == 0) {
9304 if (!*args[cur_arg + 1])
9305 goto missing_arg;
9306
9307 type = REDIRECT_TYPE_SCHEME;
9308 cur_arg++;
9309 destination = args[cur_arg];
9310 }
9311 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9312 if (!*args[cur_arg + 1])
9313 goto missing_arg;
9314
9315 cur_arg++;
9316 cookie = args[cur_arg];
9317 cookie_set = 1;
9318 }
9319 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9320 if (!*args[cur_arg + 1])
9321 goto missing_arg;
9322
9323 cur_arg++;
9324 cookie = args[cur_arg];
9325 cookie_set = 0;
9326 }
9327 else if (strcmp(args[cur_arg], "code") == 0) {
9328 if (!*args[cur_arg + 1])
9329 goto missing_arg;
9330
9331 cur_arg++;
9332 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009333 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009334 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009335 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009336 args[cur_arg - 1], args[cur_arg]);
9337 return NULL;
9338 }
9339 }
9340 else if (!strcmp(args[cur_arg],"drop-query")) {
9341 flags |= REDIRECT_FLAG_DROP_QS;
9342 }
9343 else if (!strcmp(args[cur_arg],"append-slash")) {
9344 flags |= REDIRECT_FLAG_APPEND_SLASH;
9345 }
9346 else if (strcmp(args[cur_arg], "if") == 0 ||
9347 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009348 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009349 if (!cond) {
9350 memprintf(errmsg, "error in condition: %s", *errmsg);
9351 return NULL;
9352 }
9353 break;
9354 }
9355 else {
9356 memprintf(errmsg,
9357 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9358 args[cur_arg]);
9359 return NULL;
9360 }
9361 cur_arg++;
9362 }
9363
9364 if (type == REDIRECT_TYPE_NONE) {
9365 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9366 return NULL;
9367 }
9368
Willy Tarreaube4653b2015-05-28 15:26:58 +02009369 if (dir && type != REDIRECT_TYPE_LOCATION) {
9370 memprintf(errmsg, "response only supports redirect type 'location'");
9371 return NULL;
9372 }
9373
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009374 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009375 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009376 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009377
9378 if (!use_fmt) {
9379 /* old-style static redirect rule */
9380 rule->rdr_str = strdup(destination);
9381 rule->rdr_len = strlen(destination);
9382 }
9383 else {
9384 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009385
9386 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9387 * if prefix == "/", we don't want to add anything, otherwise it
9388 * makes it hard for the user to configure a self-redirection.
9389 */
Godbachd9722032014-12-18 15:44:58 +08009390 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009391 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009392 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9393 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 +01009394 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9395 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009396 return NULL;
9397 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009398 free(curproxy->conf.lfs_file);
9399 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9400 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009401 }
9402 }
9403
Willy Tarreau4baae242012-12-27 12:00:31 +01009404 if (cookie) {
9405 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9406 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9407 */
9408 rule->cookie_len = strlen(cookie);
9409 if (cookie_set) {
9410 rule->cookie_str = malloc(rule->cookie_len + 10);
9411 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9412 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9413 rule->cookie_len += 9;
9414 } else {
9415 rule->cookie_str = malloc(rule->cookie_len + 21);
9416 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9417 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9418 rule->cookie_len += 20;
9419 }
9420 }
9421 rule->type = type;
9422 rule->code = code;
9423 rule->flags = flags;
9424 LIST_INIT(&rule->list);
9425 return rule;
9426
9427 missing_arg:
9428 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9429 return NULL;
9430}
9431
Willy Tarreau8797c062007-05-07 00:55:35 +02009432/************************************************************************/
9433/* The code below is dedicated to ACL parsing and matching */
9434/************************************************************************/
9435
9436
Willy Tarreau14174bc2012-04-16 14:34:04 +02009437/* This function ensures that the prerequisites for an L7 fetch are ready,
9438 * which means that a request or response is ready. If some data is missing,
9439 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009440 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9441 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009442 *
9443 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009444 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9445 * decide whether or not an HTTP message is present ;
9446 * 0 if the requested data cannot be fetched or if it is certain that
9447 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009448 * 1 if an HTTP message is ready
9449 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009450int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009451 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009452{
Willy Tarreau192252e2015-04-04 01:47:55 +02009453 struct http_txn *txn;
9454 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009455
Willy Tarreaube508f12016-03-10 11:47:01 +01009456 /* Note: it is possible that <s> is NULL when called before stream
9457 * initialization (eg: tcp-request connection), so this function is the
9458 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009459 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009460 if (!s)
9461 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009462
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009463 if (!s->txn) {
9464 if (unlikely(!http_alloc_txn(s)))
9465 return 0; /* not enough memory */
9466 http_init_txn(s);
9467 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009468 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009469 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009470
9471 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009472 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009473
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009474 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009475 /* If the buffer does not leave enough free space at the end,
9476 * we must first realign it.
9477 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009478 if (s->req.buf->p > s->req.buf->data &&
9479 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9480 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009481
Willy Tarreau14174bc2012-04-16 14:34:04 +02009482 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009483 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009484 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009485
9486 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009487 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009488 http_msg_analyzer(msg, &txn->hdr_idx);
9489
9490 /* Still no valid request ? */
9491 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009492 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009493 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009494 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009495 }
9496 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009497 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009498 return 0;
9499 }
9500
9501 /* OK we just got a valid HTTP request. We have some minor
9502 * preparation to perform so that further checks can rely
9503 * on HTTP tests.
9504 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009505
9506 /* If the request was parsed but was too large, we must absolutely
9507 * return an error so that it is not processed. At the moment this
9508 * cannot happen, but if the parsers are to change in the future,
9509 * we want this check to be maintained.
9510 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009511 if (unlikely(s->req.buf->i + s->req.buf->p >
9512 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009513 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009514 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009515 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009516 return 1;
9517 }
9518
Willy Tarreau9b28e032012-10-12 23:49:43 +02009519 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009520 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009521 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009522
Willy Tarreau506d0502013-07-06 13:29:24 +02009523 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9524 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009525 }
9526
Willy Tarreau506d0502013-07-06 13:29:24 +02009527 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009528 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009529 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009530
9531 /* otherwise everything's ready for the request */
9532 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009533 else {
9534 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009535 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9536 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009537 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009538 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009539 }
9540
9541 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009542 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009543 return 1;
9544}
Willy Tarreau8797c062007-05-07 00:55:35 +02009545
Willy Tarreau8797c062007-05-07 00:55:35 +02009546/* 1. Check on METHOD
9547 * We use the pre-parsed method if it is known, and store its number as an
9548 * integer. If it is unknown, we use the pointer and the length.
9549 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009550static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009551{
9552 int len, meth;
9553
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009554 len = strlen(text);
9555 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009556
9557 pattern->val.i = meth;
9558 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009559 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009560 pattern->len = len;
9561 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009562 else {
9563 pattern->ptr.str = NULL;
9564 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009565 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009566 return 1;
9567}
9568
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009569/* This function fetches the method of current HTTP request and stores
9570 * it in the global pattern struct as a chunk. There are two possibilities :
9571 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9572 * in <len> and <ptr> is NULL ;
9573 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9574 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009575 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009576 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009577static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009578smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009579{
9580 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009581 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009582
Willy Tarreau24e32d82012-04-23 23:55:44 +02009583 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009584
Willy Tarreaube508f12016-03-10 11:47:01 +01009585 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009586 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009587 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009588 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009589 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009590 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9591 /* ensure the indexes are not affected */
9592 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009593 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009594 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9595 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009596 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009597 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009598 return 1;
9599}
9600
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009601/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009602static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009603{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009604 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009605 struct pattern_list *lst;
9606 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009607
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009608 list_for_each_entry(lst, &expr->patterns, list) {
9609 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009610
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009611 /* well-known method */
9612 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009613 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009614 return pattern;
9615 else
9616 continue;
9617 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009618
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009619 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009620 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009621 continue;
9622
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009623 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009624 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9625 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009626 return pattern;
9627 }
9628 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009629}
9630
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009631static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009632smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009633{
Willy Tarreaube508f12016-03-10 11:47:01 +01009634 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009635 char *ptr;
9636 int len;
9637
Willy Tarreauc0239e02012-04-16 14:42:55 +02009638 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009639
Willy Tarreaube508f12016-03-10 11:47:01 +01009640 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009641 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009642 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009643
9644 while ((len-- > 0) && (*ptr++ != '/'));
9645 if (len <= 0)
9646 return 0;
9647
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009648 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009649 smp->data.u.str.str = ptr;
9650 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009651
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009652 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009653 return 1;
9654}
9655
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009656static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009657smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009658{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009659 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009660 char *ptr;
9661 int len;
9662
Willy Tarreauc0239e02012-04-16 14:42:55 +02009663 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009664
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009665 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009666 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9667 return 0;
9668
Willy Tarreau8797c062007-05-07 00:55:35 +02009669 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009670 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009671
9672 while ((len-- > 0) && (*ptr++ != '/'));
9673 if (len <= 0)
9674 return 0;
9675
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009676 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009677 smp->data.u.str.str = ptr;
9678 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009679
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009680 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009681 return 1;
9682}
9683
9684/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009685static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009686smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009687{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009688 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009689 char *ptr;
9690 int len;
9691
Willy Tarreauc0239e02012-04-16 14:42:55 +02009692 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009693
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009694 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009695 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9696 return 0;
9697
Willy Tarreau8797c062007-05-07 00:55:35 +02009698 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009699 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009700
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009701 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009702 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009703 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009704 return 1;
9705}
9706
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009707static int
9708smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9709{
9710 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9711 return 0;
9712
9713 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009714 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009715 return 0;
9716 smp->strm->unique_id[0] = '\0';
9717 }
9718 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9719 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9720
9721 smp->data.type = SMP_T_STR;
9722 smp->data.u.str.str = smp->strm->unique_id;
9723 smp->flags = SMP_F_CONST;
9724 return 1;
9725}
9726
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009727/* Returns a string block containing all headers including the
9728 * empty line wich separes headers from the body. This is useful
9729 * form some headers analysis.
9730 */
9731static int
9732smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9733{
9734 struct http_msg *msg;
9735 struct hdr_idx *idx;
9736 struct http_txn *txn;
9737
9738 CHECK_HTTP_MESSAGE_FIRST();
9739
9740 txn = smp->strm->txn;
9741 idx = &txn->hdr_idx;
9742 msg = &txn->req;
9743
9744 smp->data.type = SMP_T_STR;
9745 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9746 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9747 (msg->chn->buf->p[msg->eoh] == '\r');
9748
9749 return 1;
9750}
9751
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009752/* Returns the header request in a length/value encoded format.
9753 * This is useful for exchanges with the SPOE.
9754 *
9755 * A "length value" is a multibyte code encoding numbers. It uses the
9756 * SPOE format. The encoding is the following:
9757 *
9758 * Each couple "header name" / "header value" is composed
9759 * like this:
9760 * "length value" "header name bytes"
9761 * "length value" "header value bytes"
9762 * When the last header is reached, the header name and the header
9763 * value are empty. Their length are 0
9764 */
9765static int
9766smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9767{
9768 struct http_msg *msg;
9769 struct chunk *temp;
9770 struct hdr_idx *idx;
9771 const char *cur_ptr, *cur_next, *p;
9772 int old_idx, cur_idx;
9773 struct hdr_idx_elem *cur_hdr;
9774 const char *hn, *hv;
9775 int hnl, hvl;
9776 int ret;
9777 struct http_txn *txn;
9778 char *buf;
9779 char *end;
9780
9781 CHECK_HTTP_MESSAGE_FIRST();
9782
9783 temp = get_trash_chunk();
9784 buf = temp->str;
9785 end = temp->str + temp->size;
9786
9787 txn = smp->strm->txn;
9788 idx = &txn->hdr_idx;
9789 msg = &txn->req;
9790
9791 /* Build array of headers. */
9792 old_idx = 0;
9793 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9794 while (1) {
9795 cur_idx = idx->v[old_idx].next;
9796 if (!cur_idx)
9797 break;
9798 old_idx = cur_idx;
9799
9800 cur_hdr = &idx->v[cur_idx];
9801 cur_ptr = cur_next;
9802 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9803
9804 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9805 * and the next header starts at cur_next. We'll check
9806 * this header in the list as well as against the default
9807 * rule.
9808 */
9809
9810 /* look for ': *'. */
9811 hn = cur_ptr;
9812 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9813 if (p >= cur_ptr+cur_hdr->len)
9814 continue;
9815 hnl = p - hn;
9816 p++;
9817 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9818 p++;
9819 if (p >= cur_ptr + cur_hdr->len)
9820 continue;
9821 hv = p;
9822 hvl = cur_ptr + cur_hdr->len-p;
9823
9824 /* encode the header name. */
9825 ret = encode_varint(hnl, &buf, end);
9826 if (ret == -1)
9827 return 0;
9828 if (buf + hnl > end)
9829 return 0;
9830 memcpy(buf, hn, hnl);
9831 buf += hnl;
9832
9833 /* encode and copy the value. */
9834 ret = encode_varint(hvl, &buf, end);
9835 if (ret == -1)
9836 return 0;
9837 if (buf + hvl > end)
9838 return 0;
9839 memcpy(buf, hv, hvl);
9840 buf += hvl;
9841 }
9842
9843 /* encode the end of the header list with empty
9844 * header name and header value.
9845 */
9846 ret = encode_varint(0, &buf, end);
9847 if (ret == -1)
9848 return 0;
9849 ret = encode_varint(0, &buf, end);
9850 if (ret == -1)
9851 return 0;
9852
9853 /* Initialise sample data which will be filled. */
9854 smp->data.type = SMP_T_BIN;
9855 smp->data.u.str.str = temp->str;
9856 smp->data.u.str.len = buf - temp->str;
9857 smp->data.u.str.size = temp->size;
9858
9859 return 1;
9860}
9861
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009862/* returns the longest available part of the body. This requires that the body
9863 * has been waited for using http-buffer-request.
9864 */
9865static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009866smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009867{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009868 struct http_msg *msg;
9869 unsigned long len;
9870 unsigned long block1;
9871 char *body;
9872 struct chunk *temp;
9873
9874 CHECK_HTTP_MESSAGE_FIRST();
9875
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009876 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009877 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009878 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009879 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009880
9881 len = http_body_bytes(msg);
9882 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9883
9884 block1 = len;
9885 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9886 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9887
9888 if (block1 == len) {
9889 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009890 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009891 smp->data.u.str.str = body;
9892 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009893 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9894 }
9895 else {
9896 /* buffer is wrapped, we need to defragment it */
9897 temp = get_trash_chunk();
9898 memcpy(temp->str, body, block1);
9899 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009900 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009901 smp->data.u.str.str = temp->str;
9902 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009903 smp->flags = SMP_F_VOL_TEST;
9904 }
9905 return 1;
9906}
9907
9908
9909/* returns the available length of the body. This requires that the body
9910 * has been waited for using http-buffer-request.
9911 */
9912static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009913smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009914{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009915 struct http_msg *msg;
9916
9917 CHECK_HTTP_MESSAGE_FIRST();
9918
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009919 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009920 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009921 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009922 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009923
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009924 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009925 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009926
9927 smp->flags = SMP_F_VOL_TEST;
9928 return 1;
9929}
9930
9931
9932/* returns the advertised length of the body, or the advertised size of the
9933 * chunks available in the buffer. This requires that the body has been waited
9934 * for using http-buffer-request.
9935 */
9936static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009937smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009938{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009939 struct http_msg *msg;
9940
9941 CHECK_HTTP_MESSAGE_FIRST();
9942
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009943 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009944 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009945 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009946 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009947
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009948 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009949 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009950
9951 smp->flags = SMP_F_VOL_TEST;
9952 return 1;
9953}
9954
9955
Willy Tarreau8797c062007-05-07 00:55:35 +02009956/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009957static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009958smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009959{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009960 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009961
Willy Tarreauc0239e02012-04-16 14:42:55 +02009962 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009963
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009964 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009965 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009966 smp->data.u.str.len = txn->req.sl.rq.u_l;
9967 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009968 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009969 return 1;
9970}
9971
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009972static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009973smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009974{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009975 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009976 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009977
Willy Tarreauc0239e02012-04-16 14:42:55 +02009978 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009979
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009980 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009981 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009982 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009983 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009984
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009985 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009986 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009987 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009988 return 1;
9989}
9990
9991static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009992smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009993{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009994 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009995 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009996
Willy Tarreauc0239e02012-04-16 14:42:55 +02009997 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009998
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009999 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010000 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010001 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10002 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010003
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010004 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010005 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010006 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010007 return 1;
10008}
10009
Willy Tarreau185b5c42012-04-26 15:11:51 +020010010/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10011 * Accepts an optional argument of type string containing the header field name,
10012 * and an optional argument of type signed or unsigned integer to request an
10013 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010014 * headers are considered from the first one. It does not stop on commas and
10015 * returns full lines instead (useful for User-Agent or Date for example).
10016 */
10017static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010018smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010019{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010020 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010021 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010022 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010023 int occ = 0;
10024 const char *name_str = NULL;
10025 int name_len = 0;
10026
10027 if (!ctx) {
10028 /* first call */
10029 ctx = &static_hdr_ctx;
10030 ctx->idx = 0;
10031 smp->ctx.a[0] = ctx;
10032 }
10033
10034 if (args) {
10035 if (args[0].type != ARGT_STR)
10036 return 0;
10037 name_str = args[0].data.str.str;
10038 name_len = args[0].data.str.len;
10039
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010040 if (args[1].type == ARGT_SINT)
10041 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010042 }
10043
10044 CHECK_HTTP_MESSAGE_FIRST();
10045
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010046 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010047 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 +020010048
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010049 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10050 /* search for header from the beginning */
10051 ctx->idx = 0;
10052
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010053 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010054 /* no explicit occurrence and single fetch => last header by default */
10055 occ = -1;
10056
10057 if (!occ)
10058 /* prepare to report multiple occurrences for ACL fetches */
10059 smp->flags |= SMP_F_NOT_LAST;
10060
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010061 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010062 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010063 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010064 return 1;
10065
10066 smp->flags &= ~SMP_F_NOT_LAST;
10067 return 0;
10068}
10069
10070/* 6. Check on HTTP header count. The number of occurrences is returned.
10071 * Accepts exactly 1 argument of type string. It does not stop on commas and
10072 * returns full lines instead (useful for User-Agent or Date for example).
10073 */
10074static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010075smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010076{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010077 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010078 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010079 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010080 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010081 const char *name = NULL;
10082 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010083
Willy Tarreau601a4d12015-04-01 19:16:09 +020010084 if (args && args->type == ARGT_STR) {
10085 name = args->data.str.str;
10086 len = args->data.str.len;
10087 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010088
10089 CHECK_HTTP_MESSAGE_FIRST();
10090
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010091 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010092 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 +020010093
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010094 ctx.idx = 0;
10095 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010096 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010097 cnt++;
10098
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010099 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010100 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010101 smp->flags = SMP_F_VOL_HDR;
10102 return 1;
10103}
10104
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010105static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010106smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010107{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010108 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010109 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010110 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010111 struct chunk *temp;
10112 char del = ',';
10113
10114 if (args && args->type == ARGT_STR)
10115 del = *args[0].data.str.str;
10116
10117 CHECK_HTTP_MESSAGE_FIRST();
10118
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010119 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010120 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 +020010121
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010122 temp = get_trash_chunk();
10123
10124 ctx.idx = 0;
10125 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10126 if (temp->len)
10127 temp->str[temp->len++] = del;
10128 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10129 temp->len += ctx.del;
10130 }
10131
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010132 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010133 smp->data.u.str.str = temp->str;
10134 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010135 smp->flags = SMP_F_VOL_HDR;
10136 return 1;
10137}
10138
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010139/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10140 * Accepts an optional argument of type string containing the header field name,
10141 * and an optional argument of type signed or unsigned integer to request an
10142 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010143 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010144 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010145static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010146smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010147{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010148 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010149 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010150 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010151 int occ = 0;
10152 const char *name_str = NULL;
10153 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010154
Willy Tarreaua890d072013-04-02 12:01:06 +020010155 if (!ctx) {
10156 /* first call */
10157 ctx = &static_hdr_ctx;
10158 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010159 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010160 }
10161
Willy Tarreau185b5c42012-04-26 15:11:51 +020010162 if (args) {
10163 if (args[0].type != ARGT_STR)
10164 return 0;
10165 name_str = args[0].data.str.str;
10166 name_len = args[0].data.str.len;
10167
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010168 if (args[1].type == ARGT_SINT)
10169 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010170 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010171
Willy Tarreaue333ec92012-04-16 16:26:40 +020010172 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010173
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010174 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010175 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 +020010176
Willy Tarreau185b5c42012-04-26 15:11:51 +020010177 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010178 /* search for header from the beginning */
10179 ctx->idx = 0;
10180
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010181 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010182 /* no explicit occurrence and single fetch => last header by default */
10183 occ = -1;
10184
10185 if (!occ)
10186 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010187 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010188
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010189 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010190 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010191 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010192 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010193
Willy Tarreau37406352012-04-23 16:16:37 +020010194 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010195 return 0;
10196}
10197
Willy Tarreauc11416f2007-06-17 16:58:38 +020010198/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010199 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010200 */
10201static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010202smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010203{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010204 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010205 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010206 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010207 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010208 const char *name = NULL;
10209 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010210
Willy Tarreau601a4d12015-04-01 19:16:09 +020010211 if (args && args->type == ARGT_STR) {
10212 name = args->data.str.str;
10213 len = args->data.str.len;
10214 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010215
Willy Tarreaue333ec92012-04-16 16:26:40 +020010216 CHECK_HTTP_MESSAGE_FIRST();
10217
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010218 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010219 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 +020010220
Willy Tarreau33a7e692007-06-10 19:45:56 +020010221 ctx.idx = 0;
10222 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010223 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010224 cnt++;
10225
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010226 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010227 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010228 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010229 return 1;
10230}
10231
Willy Tarreau185b5c42012-04-26 15:11:51 +020010232/* Fetch an HTTP header's integer value. The integer value is returned. It
10233 * takes a mandatory argument of type string and an optional one of type int
10234 * to designate a specific occurrence. It returns an unsigned integer, which
10235 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010236 */
10237static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010238smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010239{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010240 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010241
Willy Tarreauf853c462012-04-23 18:53:56 +020010242 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010243 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010244 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010245 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010246
Willy Tarreaud53e2422012-04-16 17:21:11 +020010247 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010248}
10249
Cyril Bonté69fa9922012-10-25 00:01:06 +020010250/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10251 * and an optional one of type int to designate a specific occurrence.
10252 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010253 */
10254static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010255smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010256{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010257 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010258
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010259 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010260 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010261 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010262 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010263 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010264 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010265 if (smp->data.u.str.len < temp->size - 1) {
10266 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10267 temp->str[smp->data.u.str.len] = '\0';
10268 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010269 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010270 break;
10271 }
10272 }
10273 }
10274
Willy Tarreaud53e2422012-04-16 17:21:11 +020010275 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010276 if (!(smp->flags & SMP_F_NOT_LAST))
10277 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010278 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010279 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010280}
10281
Willy Tarreau737b0c12007-06-10 21:28:46 +020010282/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10283 * the first '/' after the possible hostname, and ends before the possible '?'.
10284 */
10285static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010286smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010287{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010288 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010289 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010290
Willy Tarreauc0239e02012-04-16 14:42:55 +020010291 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010292
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010293 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010294 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010295 ptr = http_get_path(txn);
10296 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010297 return 0;
10298
10299 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010300 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010301 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010302
10303 while (ptr < end && *ptr != '?')
10304 ptr++;
10305
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010306 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010307 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010308 return 1;
10309}
10310
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010311/* This produces a concatenation of the first occurrence of the Host header
10312 * followed by the path component if it begins with a slash ('/'). This means
10313 * that '*' will not be added, resulting in exactly the first Host entry.
10314 * If no Host header is found, then the path is returned as-is. The returned
10315 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010316 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010317 */
10318static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010319smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010320{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010321 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010322 char *ptr, *end, *beg;
10323 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010324 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010325
10326 CHECK_HTTP_MESSAGE_FIRST();
10327
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010328 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010329 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010330 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010331 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010332
10333 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010334 temp = get_trash_chunk();
10335 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010336 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010337 smp->data.u.str.str = temp->str;
10338 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010339
10340 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010341 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010342 beg = http_get_path(txn);
10343 if (!beg)
10344 beg = end;
10345
10346 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10347
10348 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010349 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10350 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010351 }
10352
10353 smp->flags = SMP_F_VOL_1ST;
10354 return 1;
10355}
10356
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010357/* This produces a 32-bit hash of the concatenation of the first occurrence of
10358 * the Host header followed by the path component if it begins with a slash ('/').
10359 * This means that '*' will not be added, resulting in exactly the first Host
10360 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010361 * is hashed using the path hash followed by a full avalanche hash and provides a
10362 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010363 * high-traffic sites without having to store whole paths.
10364 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010365int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010366smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010367{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010368 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010369 struct hdr_ctx ctx;
10370 unsigned int hash = 0;
10371 char *ptr, *beg, *end;
10372 int len;
10373
10374 CHECK_HTTP_MESSAGE_FIRST();
10375
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010376 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010377 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010378 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010379 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10380 ptr = ctx.line + ctx.val;
10381 len = ctx.vlen;
10382 while (len--)
10383 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10384 }
10385
10386 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010387 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010388 beg = http_get_path(txn);
10389 if (!beg)
10390 beg = end;
10391
10392 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10393
10394 if (beg < ptr && *beg == '/') {
10395 while (beg < ptr)
10396 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10397 }
10398 hash = full_hash(hash);
10399
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010400 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010401 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010402 smp->flags = SMP_F_VOL_1ST;
10403 return 1;
10404}
10405
Willy Tarreau4a550602012-12-09 14:53:32 +010010406/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010407 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10408 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10409 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010410 * that in environments where IPv6 is insignificant, truncating the output to
10411 * 8 bytes would still work.
10412 */
10413static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010414smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010415{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010416 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010417 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010418
10419 if (!cli_conn)
10420 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010421
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010422 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010423 return 0;
10424
Willy Tarreau47ca5452012-12-23 20:22:19 +010010425 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010426 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010427 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010428
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010429 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010430 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010431 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010432 temp->len += 4;
10433 break;
10434 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010435 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010436 temp->len += 16;
10437 break;
10438 default:
10439 return 0;
10440 }
10441
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010442 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010443 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010444 return 1;
10445}
10446
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010447/* Extracts the query string, which comes after the question mark '?'. If no
10448 * question mark is found, nothing is returned. Otherwise it returns a sample
10449 * of type string carrying the whole query string.
10450 */
10451static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010452smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010453{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010454 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010455 char *ptr, *end;
10456
10457 CHECK_HTTP_MESSAGE_FIRST();
10458
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010459 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010460 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10461 end = ptr + txn->req.sl.rq.u_l;
10462
10463 /* look up the '?' */
10464 do {
10465 if (ptr == end)
10466 return 0;
10467 } while (*ptr++ != '?');
10468
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010469 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010470 smp->data.u.str.str = ptr;
10471 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010472 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10473 return 1;
10474}
10475
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010476static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010477smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010478{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010479 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10480 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10481 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010482
Willy Tarreau24e32d82012-04-23 23:55:44 +020010483 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010484
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010485 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010486 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010487 return 1;
10488}
10489
Willy Tarreau7f18e522010-10-22 20:04:13 +020010490/* return a valid test if the current request is the first one on the connection */
10491static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010492smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010493{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010494 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010495 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010496 return 1;
10497}
10498
Willy Tarreau34db1082012-04-19 17:16:54 +020010499/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010501smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010502{
10503
Willy Tarreau24e32d82012-04-23 23:55:44 +020010504 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010505 return 0;
10506
Willy Tarreauc0239e02012-04-16 14:42:55 +020010507 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010508
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010509 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010510 return 0;
10511
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010512 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010513 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010514 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010515 return 1;
10516}
Willy Tarreau8797c062007-05-07 00:55:35 +020010517
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010518/* Accepts exactly 1 argument of type userlist */
10519static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010520smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010521{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010522 if (!args || args->type != ARGT_USR)
10523 return 0;
10524
10525 CHECK_HTTP_MESSAGE_FIRST();
10526
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010527 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010528 return 0;
10529
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010530 /* if the user does not belong to the userlist or has a wrong password,
10531 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010532 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010533 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010534 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10535 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010536 return 0;
10537
10538 /* pat_match_auth() will need the user list */
10539 smp->ctx.a[0] = args->data.usr;
10540
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010541 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010542 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010543 smp->data.u.str.str = smp->strm->txn->auth.user;
10544 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010545
10546 return 1;
10547}
10548
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010549/* Try to find the next occurrence of a cookie name in a cookie header value.
10550 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10551 * the cookie value is returned into *value and *value_l, and the function
10552 * returns a pointer to the next pointer to search from if the value was found.
10553 * Otherwise if the cookie was not found, NULL is returned and neither value
10554 * nor value_l are touched. The input <hdr> string should first point to the
10555 * header's value, and the <hdr_end> pointer must point to the first character
10556 * not part of the value. <list> must be non-zero if value may represent a list
10557 * of values (cookie headers). This makes it faster to abort parsing when no
10558 * list is expected.
10559 */
David Carlier4686f792015-09-25 14:10:50 +010010560char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010561extract_cookie_value(char *hdr, const char *hdr_end,
10562 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010563 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010564{
10565 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10566 char *next;
10567
10568 /* we search at least a cookie name followed by an equal, and more
10569 * generally something like this :
10570 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10571 */
10572 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10573 /* Iterate through all cookies on this line */
10574
Willy Tarreau2235b262016-11-05 15:50:20 +010010575 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010576 att_beg++;
10577
10578 /* find att_end : this is the first character after the last non
10579 * space before the equal. It may be equal to hdr_end.
10580 */
10581 equal = att_end = att_beg;
10582
10583 while (equal < hdr_end) {
10584 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10585 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010586 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010587 continue;
10588 att_end = equal;
10589 }
10590
10591 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10592 * is between <att_beg> and <equal>, both may be identical.
10593 */
10594
10595 /* look for end of cookie if there is an equal sign */
10596 if (equal < hdr_end && *equal == '=') {
10597 /* look for the beginning of the value */
10598 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010599 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010600 val_beg++;
10601
10602 /* find the end of the value, respecting quotes */
10603 next = find_cookie_value_end(val_beg, hdr_end);
10604
10605 /* make val_end point to the first white space or delimitor after the value */
10606 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010607 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010608 val_end--;
10609 } else {
10610 val_beg = val_end = next = equal;
10611 }
10612
10613 /* We have nothing to do with attributes beginning with '$'. However,
10614 * they will automatically be removed if a header before them is removed,
10615 * since they're supposed to be linked together.
10616 */
10617 if (*att_beg == '$')
10618 continue;
10619
10620 /* Ignore cookies with no equal sign */
10621 if (equal == next)
10622 continue;
10623
10624 /* Now we have the cookie name between att_beg and att_end, and
10625 * its value between val_beg and val_end.
10626 */
10627
10628 if (att_end - att_beg == cookie_name_l &&
10629 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10630 /* let's return this value and indicate where to go on from */
10631 *value = val_beg;
10632 *value_l = val_end - val_beg;
10633 return next + 1;
10634 }
10635
10636 /* Set-Cookie headers only have the name in the first attr=value part */
10637 if (!list)
10638 break;
10639 }
10640
10641 return NULL;
10642}
10643
William Lallemanda43ba4e2014-01-28 18:14:25 +010010644/* Fetch a captured HTTP request header. The index is the position of
10645 * the "capture" option in the configuration file
10646 */
10647static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010648smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010649{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010650 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010651 int idx;
10652
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010653 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010654 return 0;
10655
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010656 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010657
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010658 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 +010010659 return 0;
10660
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010661 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010662 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010663 smp->data.u.str.str = smp->strm->req_cap[idx];
10664 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010665
10666 return 1;
10667}
10668
10669/* Fetch a captured HTTP response header. The index is the position of
10670 * the "capture" option in the configuration file
10671 */
10672static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010673smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010674{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010675 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010676 int idx;
10677
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010678 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010679 return 0;
10680
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010681 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010682
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010683 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 +010010684 return 0;
10685
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010686 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010687 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010688 smp->data.u.str.str = smp->strm->res_cap[idx];
10689 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010690
10691 return 1;
10692}
10693
William Lallemand65ad6e12014-01-31 15:08:02 +010010694/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10695static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010696smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010697{
10698 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010699 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010700 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010701
Willy Tarreau15e91e12015-04-04 00:52:09 +020010702 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010703 return 0;
10704
William Lallemand96a77852014-02-05 00:30:02 +010010705 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010706
William Lallemand96a77852014-02-05 00:30:02 +010010707 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10708 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010709
William Lallemand96a77852014-02-05 00:30:02 +010010710 temp = get_trash_chunk();
10711 temp->str = txn->uri;
10712 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010713 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010714 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010715 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010716
10717 return 1;
10718
10719}
10720
10721/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10722static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010723smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010724{
10725 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010726 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010727 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010728
Willy Tarreau15e91e12015-04-04 00:52:09 +020010729 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010730 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010731
William Lallemand65ad6e12014-01-31 15:08:02 +010010732 ptr = txn->uri;
10733
10734 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10735 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010736
William Lallemand65ad6e12014-01-31 15:08:02 +010010737 if (!*ptr)
10738 return 0;
10739
10740 ptr++; /* skip the space */
10741
10742 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010743 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010744 if (!ptr)
10745 return 0;
10746 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10747 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010748
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010749 smp->data.u.str = *temp;
10750 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010751 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010752 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010753
10754 return 1;
10755}
10756
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010757/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10758 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10759 */
10760static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010761smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010762{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010763 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010764
Willy Tarreau15e91e12015-04-04 00:52:09 +020010765 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010766 return 0;
10767
10768 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010769 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010770 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010771 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010772
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010773 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010774 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010775 smp->flags = SMP_F_CONST;
10776 return 1;
10777
10778}
10779
10780/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10781 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10782 */
10783static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010784smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010785{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010786 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010787
Willy Tarreau15e91e12015-04-04 00:52:09 +020010788 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010789 return 0;
10790
10791 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010792 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010793 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010794 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010795
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010796 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010797 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010798 smp->flags = SMP_F_CONST;
10799 return 1;
10800
10801}
10802
William Lallemand65ad6e12014-01-31 15:08:02 +010010803
Willy Tarreaue333ec92012-04-16 16:26:40 +020010804/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010805 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010806 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010807 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010808 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010809 * Accepts exactly 1 argument of type string. If the input options indicate
10810 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010811 * The returned sample is of type CSTR. Can be used to parse cookies in other
10812 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010813 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010814int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010815{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010816 struct http_txn *txn;
10817 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010818 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010819 const struct http_msg *msg;
10820 const char *hdr_name;
10821 int hdr_name_len;
10822 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010823 int occ = 0;
10824 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010825
Willy Tarreau24e32d82012-04-23 23:55:44 +020010826 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010827 return 0;
10828
Willy Tarreaua890d072013-04-02 12:01:06 +020010829 if (!ctx) {
10830 /* first call */
10831 ctx = &static_hdr_ctx;
10832 ctx->idx = 0;
10833 smp->ctx.a[2] = ctx;
10834 }
10835
Willy Tarreaue333ec92012-04-16 16:26:40 +020010836 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010837
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010838 txn = smp->strm->txn;
10839 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010840
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010841 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010842 msg = &txn->req;
10843 hdr_name = "Cookie";
10844 hdr_name_len = 6;
10845 } else {
10846 msg = &txn->rsp;
10847 hdr_name = "Set-Cookie";
10848 hdr_name_len = 10;
10849 }
10850
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010851 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010852 /* no explicit occurrence and single fetch => last cookie by default */
10853 occ = -1;
10854
10855 /* OK so basically here, either we want only one value and it's the
10856 * last one, or we want to iterate over all of them and we fetch the
10857 * next one.
10858 */
10859
Willy Tarreau9b28e032012-10-12 23:49:43 +020010860 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010861 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010862 /* search for the header from the beginning, we must first initialize
10863 * the search parameters.
10864 */
Willy Tarreau37406352012-04-23 16:16:37 +020010865 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010866 ctx->idx = 0;
10867 }
10868
Willy Tarreau28376d62012-04-26 21:26:10 +020010869 smp->flags |= SMP_F_VOL_HDR;
10870
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010871 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010872 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10873 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010874 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10875 goto out;
10876
Willy Tarreau24e32d82012-04-23 23:55:44 +020010877 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010878 continue;
10879
Willy Tarreau37406352012-04-23 16:16:37 +020010880 smp->ctx.a[0] = ctx->line + ctx->val;
10881 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010882 }
10883
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010884 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010885 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010886 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010887 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010888 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010889 &smp->data.u.str.str,
10890 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010891 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010892 found = 1;
10893 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010894 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010895 smp->flags |= SMP_F_NOT_LAST;
10896 return 1;
10897 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010899 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010900 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010901 /* all cookie headers and values were scanned. If we're looking for the
10902 * last occurrence, we may return it now.
10903 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010904 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010905 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010906 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010907}
10908
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010909/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010910 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010911 * multiple cookies may be parsed on the same line. The returned sample is of
10912 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010913 */
10914static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010915smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010916{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010917 struct http_txn *txn;
10918 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010919 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010920 const struct http_msg *msg;
10921 const char *hdr_name;
10922 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010923 int cnt;
10924 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010925 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010926
Willy Tarreau24e32d82012-04-23 23:55:44 +020010927 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010928 return 0;
10929
Willy Tarreaue333ec92012-04-16 16:26:40 +020010930 CHECK_HTTP_MESSAGE_FIRST();
10931
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010932 txn = smp->strm->txn;
10933 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010934
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010935 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010936 msg = &txn->req;
10937 hdr_name = "Cookie";
10938 hdr_name_len = 6;
10939 } else {
10940 msg = &txn->rsp;
10941 hdr_name = "Set-Cookie";
10942 hdr_name_len = 10;
10943 }
10944
Willy Tarreau9b28e032012-10-12 23:49:43 +020010945 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010946 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010947 ctx.idx = 0;
10948 cnt = 0;
10949
10950 while (1) {
10951 /* Note: val_beg == NULL every time we need to fetch a new header */
10952 if (!val_beg) {
10953 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10954 break;
10955
Willy Tarreau24e32d82012-04-23 23:55:44 +020010956 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010957 continue;
10958
10959 val_beg = ctx.line + ctx.val;
10960 val_end = val_beg + ctx.vlen;
10961 }
10962
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010963 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010964 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010965 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010966 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010967 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010968 &smp->data.u.str.str,
10969 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010970 cnt++;
10971 }
10972 }
10973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010974 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010975 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010976 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010977 return 1;
10978}
10979
Willy Tarreau51539362012-05-08 12:46:28 +020010980/* Fetch an cookie's integer value. The integer value is returned. It
10981 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10982 */
10983static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010984smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010985{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010986 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010987
10988 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010989 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010990 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010991 }
10992
10993 return ret;
10994}
10995
Willy Tarreau8797c062007-05-07 00:55:35 +020010996/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010997/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010998/************************************************************************/
10999
David Cournapeau16023ee2010-12-23 20:55:41 +090011000/*
11001 * Given a path string and its length, find the position of beginning of the
11002 * query string. Returns NULL if no query string is found in the path.
11003 *
11004 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11005 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011006 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011007 */
bedis4c75cca2012-10-05 08:38:24 +020011008static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011009{
11010 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011011
bedis4c75cca2012-10-05 08:38:24 +020011012 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011013 return p ? p + 1 : NULL;
11014}
11015
bedis4c75cca2012-10-05 08:38:24 +020011016static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011017{
bedis4c75cca2012-10-05 08:38:24 +020011018 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011019}
11020
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011021/* after increasing a pointer value, it can exceed the first buffer
11022 * size. This function transform the value of <ptr> according with
11023 * the expected position. <chunks> is an array of the one or two
11024 * avalaible chunks. The first value is the start of the first chunk,
11025 * the second value if the end+1 of the first chunks. The third value
11026 * is NULL or the start of the second chunk and the fourth value is
11027 * the end+1 of the second chunk. The function returns 1 if does a
11028 * wrap, else returns 0.
11029 */
11030static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11031{
11032 if (*ptr < chunks[1])
11033 return 0;
11034 if (!chunks[2])
11035 return 0;
11036 *ptr = chunks[2] + ( *ptr - chunks[1] );
11037 return 1;
11038}
11039
David Cournapeau16023ee2010-12-23 20:55:41 +090011040/*
11041 * Given a url parameter, find the starting position of the first occurence,
11042 * or NULL if the parameter is not found.
11043 *
11044 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11045 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011046 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011047 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011048 * or the second chunk. The caller must be check the position before using the
11049 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011050 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011051static const char *
11052find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011053 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011054 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011055{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011056 const char *pos, *last, *equal;
11057 const char **bufs = chunks;
11058 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011059
David Cournapeau16023ee2010-12-23 20:55:41 +090011060
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011061 pos = bufs[0];
11062 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011063 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011064 /* Check the equal. */
11065 equal = pos + url_param_name_l;
11066 if (fix_pointer_if_wrap(chunks, &equal)) {
11067 if (equal >= chunks[3])
11068 return NULL;
11069 } else {
11070 if (equal >= chunks[1])
11071 return NULL;
11072 }
11073 if (*equal == '=') {
11074 if (pos + url_param_name_l > last) {
11075 /* process wrap case, we detect a wrap. In this case, the
11076 * comparison is performed in two parts.
11077 */
11078
11079 /* This is the end, we dont have any other chunk. */
11080 if (bufs != chunks || !bufs[2])
11081 return NULL;
11082
11083 /* Compute the length of each part of the comparison. */
11084 l1 = last - pos;
11085 l2 = url_param_name_l - l1;
11086
11087 /* The second buffer is too short to contain the compared string. */
11088 if (bufs[2] + l2 > bufs[3])
11089 return NULL;
11090
11091 if (memcmp(pos, url_param_name, l1) == 0 &&
11092 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11093 return pos;
11094
11095 /* Perform wrapping and jump the string who fail the comparison. */
11096 bufs += 2;
11097 pos = bufs[0] + l2;
11098 last = bufs[1];
11099
11100 } else {
11101 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011102 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11103 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011104 pos += url_param_name_l + 1;
11105 if (fix_pointer_if_wrap(chunks, &pos))
11106 last = bufs[2];
11107 }
11108 }
11109
11110 while (1) {
11111 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011112 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011113 pos++;
11114 if (pos < last)
11115 break;
11116 /* process buffer wrapping. */
11117 if (bufs != chunks || !bufs[2])
11118 return NULL;
11119 bufs += 2;
11120 pos = bufs[0];
11121 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011122 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011123 pos++;
11124 }
11125 return NULL;
11126}
11127
11128/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011129 * Given a url parameter name and a query string, find the next value.
11130 * An empty url_param_name matches the first available parameter.
11131 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11132 * respectively provide a pointer to the value and its end.
11133 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011134 */
11135static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011136find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011137 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011138 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011139{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011140 const char *arg_start, *qs_end;
11141 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011142
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011143 arg_start = chunks[0];
11144 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011145 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011146 /* Looks for an argument name. */
11147 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011148 url_param_name, url_param_name_l,
11149 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011150 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011151 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011152 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011153 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011154 if (!arg_start)
11155 return 0;
11156
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011157 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011158 while (1) {
11159 /* looks for the first argument. */
11160 value_start = memchr(arg_start, '=', qs_end - arg_start);
11161 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011162 /* Check for wrapping. */
11163 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011164 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011165 chunks[2]) {
11166 arg_start = chunks[2];
11167 qs_end = chunks[3];
11168 continue;
11169 }
11170 return 0;
11171 }
11172 break;
11173 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011174 value_start++;
11175 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011176 else {
11177 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011178 value_start = arg_start + url_param_name_l + 1;
11179
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011180 /* Check for pointer wrapping. */
11181 if (fix_pointer_if_wrap(chunks, &value_start)) {
11182 /* Update the end pointer. */
11183 qs_end = chunks[3];
11184
11185 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011186 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011187 return 0;
11188 }
11189 }
11190
David Cournapeau16023ee2010-12-23 20:55:41 +090011191 value_end = value_start;
11192
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011193 while (1) {
11194 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11195 value_end++;
11196 if (value_end < qs_end)
11197 break;
11198 /* process buffer wrapping. */
11199 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011200 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011201 chunks[2]) {
11202 value_end = chunks[2];
11203 qs_end = chunks[3];
11204 continue;
11205 }
11206 break;
11207 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011208
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011209 *vstart = value_start;
11210 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011211 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011212}
11213
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011214/* This scans a URL-encoded query string. It takes an optionally wrapping
11215 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11216 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11217 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011218 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011219static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011220smp_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 +090011221{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011222 const char *vstart, *vend;
11223 struct chunk *temp;
11224 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011225
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011226 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011227 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011228 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011229 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011230 return 0;
11231
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011232 /* Create sample. If the value is contiguous, return the pointer as CONST,
11233 * if the value is wrapped, copy-it in a buffer.
11234 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011235 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011236 if (chunks[2] &&
11237 vstart >= chunks[0] && vstart <= chunks[1] &&
11238 vend >= chunks[2] && vend <= chunks[3]) {
11239 /* Wrapped case. */
11240 temp = get_trash_chunk();
11241 memcpy(temp->str, vstart, chunks[1] - vstart);
11242 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011243 smp->data.u.str.str = temp->str;
11244 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011245 } else {
11246 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011247 smp->data.u.str.str = (char *)vstart;
11248 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011249 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11250 }
11251
11252 /* Update context, check wrapping. */
11253 chunks[0] = vend;
11254 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11255 chunks[1] = chunks[3];
11256 chunks[2] = NULL;
11257 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011258
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011259 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011260 smp->flags |= SMP_F_NOT_LAST;
11261
David Cournapeau16023ee2010-12-23 20:55:41 +090011262 return 1;
11263}
11264
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011265/* This function iterates over each parameter of the query string. It uses
11266 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011267 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11268 * An optional parameter name is passed in args[0], otherwise any parameter is
11269 * considered. It supports an optional delimiter argument for the beginning of
11270 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011271 */
11272static int
11273smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11274{
11275 struct http_msg *msg;
11276 char delim = '?';
11277 const char *name;
11278 int name_len;
11279
Dragan Dosen26f77e52015-05-25 10:02:11 +020011280 if (!args ||
11281 (args[0].type && args[0].type != ARGT_STR) ||
11282 (args[1].type && args[1].type != ARGT_STR))
11283 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011284
Dragan Dosen26f77e52015-05-25 10:02:11 +020011285 name = "";
11286 name_len = 0;
11287 if (args->type == ARGT_STR) {
11288 name = args->data.str.str;
11289 name_len = args->data.str.len;
11290 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011291
Dragan Dosen26f77e52015-05-25 10:02:11 +020011292 if (args[1].type)
11293 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011294
Dragan Dosen26f77e52015-05-25 10:02:11 +020011295 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011296 CHECK_HTTP_MESSAGE_FIRST();
11297
11298 msg = &smp->strm->txn->req;
11299
11300 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11301 msg->sl.rq.u_l, delim);
11302 if (!smp->ctx.a[0])
11303 return 0;
11304
11305 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011306
11307 /* Assume that the context is filled with NULL pointer
11308 * before the first call.
11309 * smp->ctx.a[2] = NULL;
11310 * smp->ctx.a[3] = NULL;
11311 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011312 }
11313
11314 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11315}
11316
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011317/* This function iterates over each parameter of the body. This requires
11318 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011319 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11320 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11321 * optional second part if the body wraps at the end of the buffer. An optional
11322 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011323 */
11324static int
11325smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11326{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011327 struct http_msg *msg;
11328 unsigned long len;
11329 unsigned long block1;
11330 char *body;
11331 const char *name;
11332 int name_len;
11333
11334 if (!args || (args[0].type && args[0].type != ARGT_STR))
11335 return 0;
11336
11337 name = "";
11338 name_len = 0;
11339 if (args[0].type == ARGT_STR) {
11340 name = args[0].data.str.str;
11341 name_len = args[0].data.str.len;
11342 }
11343
11344 if (!smp->ctx.a[0]) { // first call, find the query string
11345 CHECK_HTTP_MESSAGE_FIRST();
11346
11347 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011348 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011349 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011350 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011351
11352 len = http_body_bytes(msg);
11353 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11354
11355 block1 = len;
11356 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11357 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11358
11359 if (block1 == len) {
11360 /* buffer is not wrapped (or empty) */
11361 smp->ctx.a[0] = body;
11362 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011363
11364 /* Assume that the context is filled with NULL pointer
11365 * before the first call.
11366 * smp->ctx.a[2] = NULL;
11367 * smp->ctx.a[3] = NULL;
11368 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011369 }
11370 else {
11371 /* buffer is wrapped, we need to defragment it */
11372 smp->ctx.a[0] = body;
11373 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011374 smp->ctx.a[2] = msg->chn->buf->data;
11375 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011376 }
11377 }
11378 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11379}
11380
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011381/* Return the signed integer value for the specified url parameter (see url_param
11382 * above).
11383 */
11384static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011385smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011386{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011387 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011388
11389 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011390 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011391 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011392 }
11393
11394 return ret;
11395}
11396
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011397/* This produces a 32-bit hash of the concatenation of the first occurrence of
11398 * the Host header followed by the path component if it begins with a slash ('/').
11399 * This means that '*' will not be added, resulting in exactly the first Host
11400 * entry. If no Host header is found, then the path is used. The resulting value
11401 * is hashed using the url hash followed by a full avalanche hash and provides a
11402 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11403 * high-traffic sites without having to store whole paths.
11404 * this differs from the base32 functions in that it includes the url parameters
11405 * as well as the path
11406 */
11407static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011408smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011409{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011410 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011411 struct hdr_ctx ctx;
11412 unsigned int hash = 0;
11413 char *ptr, *beg, *end;
11414 int len;
11415
11416 CHECK_HTTP_MESSAGE_FIRST();
11417
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011418 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011419 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011420 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011421 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11422 ptr = ctx.line + ctx.val;
11423 len = ctx.vlen;
11424 while (len--)
11425 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11426 }
11427
11428 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011429 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011430 beg = http_get_path(txn);
11431 if (!beg)
11432 beg = end;
11433
11434 for (ptr = beg; ptr < end ; ptr++);
11435
11436 if (beg < ptr && *beg == '/') {
11437 while (beg < ptr)
11438 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11439 }
11440 hash = full_hash(hash);
11441
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011442 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011443 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011444 smp->flags = SMP_F_VOL_1ST;
11445 return 1;
11446}
11447
11448/* This concatenates the source address with the 32-bit hash of the Host and
11449 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11450 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11451 * on the source address length. The URL hash is stored before the address so
11452 * that in environments where IPv6 is insignificant, truncating the output to
11453 * 8 bytes would still work.
11454 */
11455static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011456smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011457{
11458 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011459 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011460
Dragan Dosendb5af612016-06-16 11:23:01 +020011461 if (!cli_conn)
11462 return 0;
11463
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011464 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011465 return 0;
11466
11467 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011468 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11469 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011470
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011471 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011472 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011473 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011474 temp->len += 4;
11475 break;
11476 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011477 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011478 temp->len += 16;
11479 break;
11480 default:
11481 return 0;
11482 }
11483
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011484 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011485 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011486 return 1;
11487}
11488
Willy Tarreau185b5c42012-04-26 15:11:51 +020011489/* This function is used to validate the arguments passed to any "hdr" fetch
11490 * keyword. These keywords support an optional positive or negative occurrence
11491 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11492 * is assumed that the types are already the correct ones. Returns 0 on error,
11493 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11494 * error message in case of error, that the caller is responsible for freeing.
11495 * The initial location must either be freeable or NULL.
11496 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011497int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011498{
11499 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011500 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011501 return 0;
11502 }
11503 return 1;
11504}
11505
Willy Tarreau276fae92013-07-25 14:36:01 +020011506/* takes an UINT value on input supposed to represent the time since EPOCH,
11507 * adds an optional offset found in args[0] and emits a string representing
11508 * the date in RFC-1123/5322 format.
11509 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011510static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011511{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011512 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011513 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11514 struct chunk *temp;
11515 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011516 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011517 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011518
11519 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011520 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011521 curr_date += args[0].data.sint;
11522
11523 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011524 if (!tm)
11525 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011526
11527 temp = get_trash_chunk();
11528 temp->len = snprintf(temp->str, temp->size - temp->len,
11529 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11530 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11531 tm->tm_hour, tm->tm_min, tm->tm_sec);
11532
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011533 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011534 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011535 return 1;
11536}
11537
Thierry FOURNIERad903512014-04-11 17:51:01 +020011538/* Match language range with language tag. RFC2616 14.4:
11539 *
11540 * A language-range matches a language-tag if it exactly equals
11541 * the tag, or if it exactly equals a prefix of the tag such
11542 * that the first tag character following the prefix is "-".
11543 *
11544 * Return 1 if the strings match, else return 0.
11545 */
11546static inline int language_range_match(const char *range, int range_len,
11547 const char *tag, int tag_len)
11548{
11549 const char *end = range + range_len;
11550 const char *tend = tag + tag_len;
11551 while (range < end) {
11552 if (*range == '-' && tag == tend)
11553 return 1;
11554 if (*range != *tag || tag == tend)
11555 return 0;
11556 range++;
11557 tag++;
11558 }
11559 /* Return true only if the last char of the tag is matched. */
11560 return tag == tend;
11561}
11562
11563/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011564static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011565{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011566 const char *al = smp->data.u.str.str;
11567 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011568 const char *token;
11569 int toklen;
11570 int qvalue;
11571 const char *str;
11572 const char *w;
11573 int best_q = 0;
11574
11575 /* Set the constant to the sample, because the output of the
11576 * function will be peek in the constant configuration string.
11577 */
11578 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011579 smp->data.u.str.size = 0;
11580 smp->data.u.str.str = "";
11581 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011582
11583 /* Parse the accept language */
11584 while (1) {
11585
11586 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011587 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011588 al++;
11589 if (al >= end)
11590 break;
11591
11592 /* Start of the fisrt word. */
11593 token = al;
11594
11595 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011596 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011597 al++;
11598 if (al == token)
11599 goto expect_comma;
11600
11601 /* Length of the token. */
11602 toklen = al - token;
11603 qvalue = 1000;
11604
11605 /* Check if the token exists in the list. If the token not exists,
11606 * jump to the next token.
11607 */
11608 str = args[0].data.str.str;
11609 w = str;
11610 while (1) {
11611 if (*str == ';' || *str == '\0') {
11612 if (language_range_match(token, toklen, w, str-w))
11613 goto look_for_q;
11614 if (*str == '\0')
11615 goto expect_comma;
11616 w = str + 1;
11617 }
11618 str++;
11619 }
11620 goto expect_comma;
11621
11622look_for_q:
11623
11624 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011625 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011626 al++;
11627 if (al >= end)
11628 goto process_value;
11629
11630 /* If ',' is found, process the result */
11631 if (*al == ',')
11632 goto process_value;
11633
11634 /* If the character is different from ';', look
11635 * for the end of the header part in best effort.
11636 */
11637 if (*al != ';')
11638 goto expect_comma;
11639
11640 /* Assumes that the char is ';', now expect "q=". */
11641 al++;
11642
11643 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011644 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011645 al++;
11646 if (al >= end)
11647 goto process_value;
11648
11649 /* Expect 'q'. If no 'q', continue in best effort */
11650 if (*al != 'q')
11651 goto process_value;
11652 al++;
11653
11654 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011655 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011656 al++;
11657 if (al >= end)
11658 goto process_value;
11659
11660 /* Expect '='. If no '=', continue in best effort */
11661 if (*al != '=')
11662 goto process_value;
11663 al++;
11664
11665 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011666 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011667 al++;
11668 if (al >= end)
11669 goto process_value;
11670
11671 /* Parse the q value. */
11672 qvalue = parse_qvalue(al, &al);
11673
11674process_value:
11675
11676 /* If the new q value is the best q value, then store the associated
11677 * language in the response. If qvalue is the biggest value (1000),
11678 * break the process.
11679 */
11680 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011681 smp->data.u.str.str = (char *)w;
11682 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011683 if (qvalue >= 1000)
11684 break;
11685 best_q = qvalue;
11686 }
11687
11688expect_comma:
11689
11690 /* Expect comma or end. If the end is detected, quit the loop. */
11691 while (al < end && *al != ',')
11692 al++;
11693 if (al >= end)
11694 break;
11695
11696 /* Comma is found, jump it and restart the analyzer. */
11697 al++;
11698 }
11699
11700 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011701 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11702 smp->data.u.str.str = args[1].data.str.str;
11703 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011704 }
11705
11706 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011707 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011708}
11709
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011710/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011711static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011712{
11713 /* If the constant flag is set or if not size is avalaible at
11714 * the end of the buffer, copy the string in other buffer
11715 * before decoding.
11716 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011717 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011718 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011719 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11720 smp->data.u.str.str = str->str;
11721 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011722 smp->flags &= ~SMP_F_CONST;
11723 }
11724
11725 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011726 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11727 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011728 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011729}
11730
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011731static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11732{
11733 struct proxy *fe = strm_fe(smp->strm);
11734 int idx, i;
11735 struct cap_hdr *hdr;
11736 int len;
11737
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011738 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011739 return 0;
11740
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011741 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011742
11743 /* Check the availibity of the capture id. */
11744 if (idx > fe->nb_req_cap - 1)
11745 return 0;
11746
11747 /* Look for the original configuration. */
11748 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11749 hdr != NULL && i != idx ;
11750 i--, hdr = hdr->next);
11751 if (!hdr)
11752 return 0;
11753
11754 /* check for the memory allocation */
11755 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011756 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011757 if (smp->strm->req_cap[hdr->index] == NULL)
11758 return 0;
11759
11760 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011761 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011762 if (len > hdr->len)
11763 len = hdr->len;
11764
11765 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011766 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011767 smp->strm->req_cap[idx][len] = '\0';
11768
11769 return 1;
11770}
11771
11772static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11773{
11774 struct proxy *fe = strm_fe(smp->strm);
11775 int idx, i;
11776 struct cap_hdr *hdr;
11777 int len;
11778
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011779 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011780 return 0;
11781
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011782 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011783
11784 /* Check the availibity of the capture id. */
11785 if (idx > fe->nb_rsp_cap - 1)
11786 return 0;
11787
11788 /* Look for the original configuration. */
11789 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11790 hdr != NULL && i != idx ;
11791 i--, hdr = hdr->next);
11792 if (!hdr)
11793 return 0;
11794
11795 /* check for the memory allocation */
11796 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011797 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011798 if (smp->strm->res_cap[hdr->index] == NULL)
11799 return 0;
11800
11801 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011802 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011803 if (len > hdr->len)
11804 len = hdr->len;
11805
11806 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011807 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011808 smp->strm->res_cap[idx][len] = '\0';
11809
11810 return 1;
11811}
11812
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011813/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011814 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011815 * the relevant part of the request line accordingly. Then it updates various
11816 * pointers to the next elements which were moved, and the total buffer length.
11817 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011818 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11819 * error, though this can be revisited when this code is finally exploited.
11820 *
11821 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11822 * query string and 3 to replace uri.
11823 *
11824 * In query string case, the mark question '?' must be set at the start of the
11825 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011826 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011827int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011828 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011829{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011830 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011831 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011832 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011833 int delta;
11834
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011835 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011836 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011837 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011838 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11839
11840 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011841 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011842 txn->req.sl.rq.m_l += delta;
11843 txn->req.sl.rq.u += delta;
11844 txn->req.sl.rq.v += delta;
11845 break;
11846
11847 case 1: // path
11848 cur_ptr = http_get_path(txn);
11849 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011850 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011851
11852 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011853 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011854 cur_end++;
11855
11856 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011857 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011858 txn->req.sl.rq.u_l += delta;
11859 txn->req.sl.rq.v += delta;
11860 break;
11861
11862 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011863 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011864 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011865 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11866 while (cur_ptr < cur_end && *cur_ptr != '?')
11867 cur_ptr++;
11868
11869 /* skip the question mark or indicate that we must insert it
11870 * (but only if the format string is not empty then).
11871 */
11872 if (cur_ptr < cur_end)
11873 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011874 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011875 offset = 0;
11876
11877 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011878 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011879 txn->req.sl.rq.u_l += delta;
11880 txn->req.sl.rq.v += delta;
11881 break;
11882
11883 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011884 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011885 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11886
11887 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011888 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011889 txn->req.sl.rq.u_l += delta;
11890 txn->req.sl.rq.v += delta;
11891 break;
11892
11893 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011894 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011895 }
11896
11897 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011898 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011899 txn->req.sl.rq.l += delta;
11900 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011901 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011902 return 0;
11903}
11904
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011905/* This function replace the HTTP status code and the associated message. The
11906 * variable <status> contains the new status code. This function never fails.
11907 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011908void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011909{
11910 struct http_txn *txn = s->txn;
11911 char *cur_ptr, *cur_end;
11912 int delta;
11913 char *res;
11914 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011915 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011916 int msg_len;
11917
11918 chunk_reset(&trash);
11919
11920 res = ultoa_o(status, trash.str, trash.size);
11921 c_l = res - trash.str;
11922
11923 trash.str[c_l] = ' ';
11924 trash.len = c_l + 1;
11925
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011926 /* Do we have a custom reason format string? */
11927 if (msg == NULL)
11928 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011929 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011930 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11931 trash.len += msg_len;
11932
11933 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11934 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11935
11936 /* commit changes and adjust message */
11937 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11938
11939 /* adjust res line offsets and lengths */
11940 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11941 txn->rsp.sl.st.c_l = c_l;
11942 txn->rsp.sl.st.r_l = msg_len;
11943
11944 delta = trash.len - (cur_end - cur_ptr);
11945 txn->rsp.sl.st.l += delta;
11946 txn->hdr_idx.v[0].len += delta;
11947 http_msg_move_end(&txn->rsp, delta);
11948}
11949
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011950/* This function executes one of the set-{method,path,query,uri} actions. It
11951 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011952 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011953 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011954 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11955 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011956 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011957enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011958 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011959{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011960 struct chunk *replace;
11961 enum act_return ret = ACT_RET_ERR;
11962
11963 replace = alloc_trash_chunk();
11964 if (!replace)
11965 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011966
11967 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011968 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011969 replace->str[replace->len++] = '?';
11970 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11971 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011972
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011973 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11974
11975 ret = ACT_RET_CONT;
11976
11977leave:
11978 free_trash_chunk(replace);
11979 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011980}
11981
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011982/* This function is just a compliant action wrapper for "set-status". */
11983enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011984 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011985{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011986 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011987 return ACT_RET_CONT;
11988}
11989
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011990/* parse an http-request action among :
11991 * set-method
11992 * set-path
11993 * set-query
11994 * set-uri
11995 *
11996 * All of them accept a single argument of type string representing a log-format.
11997 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11998 * 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 +020011999 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012000 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012001enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12002 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012003{
12004 int cur_arg = *orig_arg;
12005
Thierry FOURNIER42148732015-09-02 17:17:33 +020012006 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012007
12008 switch (args[0][4]) {
12009 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012010 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012011 rule->action_ptr = http_action_set_req_line;
12012 break;
12013 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012014 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012015 rule->action_ptr = http_action_set_req_line;
12016 break;
12017 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012018 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012019 rule->action_ptr = http_action_set_req_line;
12020 break;
12021 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012022 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012023 rule->action_ptr = http_action_set_req_line;
12024 break;
12025 default:
12026 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012027 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012028 }
12029
12030 if (!*args[cur_arg] ||
12031 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12032 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012033 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012034 }
12035
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012036 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012037 px->conf.args.ctx = ARGC_HRQ;
12038 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12039 (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 +010012040 return ACT_RET_PRS_ERR;
12041 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012042
12043 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012044 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012045}
12046
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012047/* parse set-status action:
12048 * This action accepts a single argument of type int representing
12049 * an http status code. It returns ACT_RET_PRS_OK on success,
12050 * ACT_RET_PRS_ERR on error.
12051 */
12052enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12053 struct act_rule *rule, char **err)
12054{
12055 char *error;
12056
Thierry FOURNIER42148732015-09-02 17:17:33 +020012057 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012058 rule->action_ptr = action_http_set_status;
12059
12060 /* Check if an argument is available */
12061 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012062 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012063 return ACT_RET_PRS_ERR;
12064 }
12065
12066 /* convert status code as integer */
12067 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12068 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12069 memprintf(err, "expects an integer status code between 100 and 999");
12070 return ACT_RET_PRS_ERR;
12071 }
12072
12073 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012074
12075 /* set custom reason string */
12076 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12077 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12078 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12079 (*orig_arg)++;
12080 rule->arg.status.reason = strdup(args[*orig_arg]);
12081 (*orig_arg)++;
12082 }
12083
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012084 return ACT_RET_PRS_OK;
12085}
12086
Willy Tarreau53275e82017-11-24 07:52:01 +010012087/* This function executes the "reject" HTTP action. It clears the request and
12088 * response buffer without sending any response. It can be useful as an HTTP
12089 * alternative to the silent-drop action to defend against DoS attacks, and may
12090 * also be used with HTTP/2 to close a connection instead of just a stream.
12091 * The txn status is unchanged, indicating no response was sent. The termination
12092 * flags will indicate "PR". It always returns ACT_RET_STOP.
12093 */
12094enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12095 struct session *sess, struct stream *s, int flags)
12096{
12097 channel_abort(&s->req);
12098 channel_abort(&s->res);
12099 s->req.analysers = 0;
12100 s->res.analysers = 0;
12101
12102 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12103 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12104 if (sess->listener && sess->listener->counters)
12105 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12106
12107 if (!(s->flags & SF_ERR_MASK))
12108 s->flags |= SF_ERR_PRXCOND;
12109 if (!(s->flags & SF_FINST_MASK))
12110 s->flags |= SF_FINST_R;
12111
12112 return ACT_RET_CONT;
12113}
12114
12115/* parse the "reject" action:
12116 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12117 * ACT_RET_PRS_ERR on error.
12118 */
12119enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12120 struct act_rule *rule, char **err)
12121{
12122 rule->action = ACT_CUSTOM;
12123 rule->action_ptr = http_action_reject;
12124 return ACT_RET_PRS_OK;
12125}
12126
Willy Tarreaua9083d02015-05-08 15:27:59 +020012127/* This function executes the "capture" action. It executes a fetch expression,
12128 * turns the result into a string and puts it in a capture slot. It always
12129 * returns 1. If an error occurs the action is cancelled, but the rule
12130 * processing continues.
12131 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012132enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012133 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012134{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012135 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012136 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012137 char **cap = s->req_cap;
12138 int len;
12139
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012140 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 +020012141 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012142 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012143
12144 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012145 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012146
12147 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012148 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012149
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012150 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012151 if (len > h->len)
12152 len = h->len;
12153
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012154 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012155 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012156 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012157}
12158
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012159/* This function executes the "capture" action and store the result in a
12160 * capture slot if exists. It executes a fetch expression, turns the result
12161 * into a string and puts it in a capture slot. It always returns 1. If an
12162 * error occurs the action is cancelled, but the rule processing continues.
12163 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012164enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012165 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012166{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012167 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012168 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012169 char **cap = s->req_cap;
12170 struct proxy *fe = strm_fe(s);
12171 int len;
12172 int i;
12173
12174 /* Look for the original configuration. */
12175 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012176 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012177 i--, h = h->next);
12178 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012179 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012180
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012181 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 +020012182 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012183 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012184
12185 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012186 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012187
12188 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012189 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012190
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012191 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012192 if (len > h->len)
12193 len = h->len;
12194
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012195 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012196 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012197 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012198}
12199
Christopher Faulet29730ba2017-09-18 15:26:32 +020012200/* Check an "http-request capture" action.
12201 *
12202 * The function returns 1 in success case, otherwise, it returns 0 and err is
12203 * filled.
12204 */
12205int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12206{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012207 if (rule->action_ptr != http_action_req_capture_by_id)
12208 return 1;
12209
Christopher Faulet29730ba2017-09-18 15:26:32 +020012210 if (rule->arg.capid.idx >= px->nb_req_cap) {
12211 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12212 rule->arg.capid.idx);
12213 return 0;
12214 }
12215
12216 return 1;
12217}
12218
Willy Tarreaua9083d02015-05-08 15:27:59 +020012219/* parse an "http-request capture" action. It takes a single argument which is
12220 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012221 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012222 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012223 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012224enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12225 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012226{
12227 struct sample_expr *expr;
12228 struct cap_hdr *hdr;
12229 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012230 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012231
12232 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12233 if (strcmp(args[cur_arg], "if") == 0 ||
12234 strcmp(args[cur_arg], "unless") == 0)
12235 break;
12236
12237 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012238 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012239 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012240 }
12241
Willy Tarreaua9083d02015-05-08 15:27:59 +020012242 cur_arg = *orig_arg;
12243 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12244 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012245 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012246
12247 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12248 memprintf(err,
12249 "fetch method '%s' extracts information from '%s', none of which is available here",
12250 args[cur_arg-1], sample_src_names(expr->fetch->use));
12251 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012252 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012253 }
12254
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012255 if (!args[cur_arg] || !*args[cur_arg]) {
12256 memprintf(err, "expects 'len or 'id'");
12257 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012258 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012259 }
12260
Willy Tarreaua9083d02015-05-08 15:27:59 +020012261 if (strcmp(args[cur_arg], "len") == 0) {
12262 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012263
12264 if (!(px->cap & PR_CAP_FE)) {
12265 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012266 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012267 }
12268
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012269 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012270
Willy Tarreaua9083d02015-05-08 15:27:59 +020012271 if (!args[cur_arg]) {
12272 memprintf(err, "missing length value");
12273 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012274 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012275 }
12276 /* we copy the table name for now, it will be resolved later */
12277 len = atoi(args[cur_arg]);
12278 if (len <= 0) {
12279 memprintf(err, "length must be > 0");
12280 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012281 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012282 }
12283 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012284
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012285 if (!len) {
12286 memprintf(err, "a positive 'len' argument is mandatory");
12287 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012288 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012289 }
12290
Vincent Bernat02779b62016-04-03 13:48:43 +020012291 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012292 hdr->next = px->req_cap;
12293 hdr->name = NULL; /* not a header capture */
12294 hdr->namelen = 0;
12295 hdr->len = len;
12296 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12297 hdr->index = px->nb_req_cap++;
12298
12299 px->req_cap = hdr;
12300 px->to_log |= LW_REQHDR;
12301
Thierry FOURNIER42148732015-09-02 17:17:33 +020012302 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012304 rule->arg.cap.expr = expr;
12305 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012306 }
12307
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012308 else if (strcmp(args[cur_arg], "id") == 0) {
12309 int id;
12310 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012311
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012312 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012313
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012314 if (!args[cur_arg]) {
12315 memprintf(err, "missing id value");
12316 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012317 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012318 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012319
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012320 id = strtol(args[cur_arg], &error, 10);
12321 if (*error != '\0') {
12322 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12323 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012324 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012325 }
12326 cur_arg++;
12327
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012328 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012329
Thierry FOURNIER42148732015-09-02 17:17:33 +020012330 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012331 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012332 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012333 rule->arg.capid.expr = expr;
12334 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012335 }
12336
12337 else {
12338 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12339 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012340 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012341 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012342
12343 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012344 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012345}
12346
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012347/* This function executes the "capture" action and store the result in a
12348 * capture slot if exists. It executes a fetch expression, turns the result
12349 * into a string and puts it in a capture slot. It always returns 1. If an
12350 * error occurs the action is cancelled, but the rule processing continues.
12351 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012352enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012353 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012354{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012355 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012356 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012357 char **cap = s->res_cap;
12358 struct proxy *fe = strm_fe(s);
12359 int len;
12360 int i;
12361
12362 /* Look for the original configuration. */
12363 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012364 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012365 i--, h = h->next);
12366 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012367 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012368
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012369 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 +020012370 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012371 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012372
12373 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012374 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012375
12376 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012377 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012378
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012379 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012380 if (len > h->len)
12381 len = h->len;
12382
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012383 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012384 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012385 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012386}
12387
Christopher Faulet29730ba2017-09-18 15:26:32 +020012388/* Check an "http-response capture" action.
12389 *
12390 * The function returns 1 in success case, otherwise, it returns 0 and err is
12391 * filled.
12392 */
12393int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12394{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012395 if (rule->action_ptr != http_action_res_capture_by_id)
12396 return 1;
12397
Christopher Faulet29730ba2017-09-18 15:26:32 +020012398 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12399 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12400 rule->arg.capid.idx);
12401 return 0;
12402 }
12403
12404 return 1;
12405}
12406
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012407/* parse an "http-response capture" action. It takes a single argument which is
12408 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12409 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012410 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012411 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012412enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12413 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012414{
12415 struct sample_expr *expr;
12416 int cur_arg;
12417 int id;
12418 char *error;
12419
12420 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12421 if (strcmp(args[cur_arg], "if") == 0 ||
12422 strcmp(args[cur_arg], "unless") == 0)
12423 break;
12424
12425 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012426 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012427 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012428 }
12429
12430 cur_arg = *orig_arg;
12431 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12432 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012433 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012434
12435 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12436 memprintf(err,
12437 "fetch method '%s' extracts information from '%s', none of which is available here",
12438 args[cur_arg-1], sample_src_names(expr->fetch->use));
12439 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012440 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012441 }
12442
12443 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012444 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012445 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012446 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012447 }
12448
12449 if (strcmp(args[cur_arg], "id") != 0) {
12450 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12451 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012452 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012453 }
12454
12455 cur_arg++;
12456
12457 if (!args[cur_arg]) {
12458 memprintf(err, "missing id value");
12459 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012460 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012461 }
12462
12463 id = strtol(args[cur_arg], &error, 10);
12464 if (*error != '\0') {
12465 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12466 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012467 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012468 }
12469 cur_arg++;
12470
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012471 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012472
Thierry FOURNIER42148732015-09-02 17:17:33 +020012473 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012474 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012475 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012476 rule->arg.capid.expr = expr;
12477 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012478
12479 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012480 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012481}
12482
William Lallemand73025dd2014-04-24 14:38:37 +020012483/*
12484 * Return the struct http_req_action_kw associated to a keyword.
12485 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012486struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012487{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012488 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012489}
12490
12491/*
12492 * Return the struct http_res_action_kw associated to a keyword.
12493 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012494struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012495{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012496 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012497}
12498
Willy Tarreau12207b32016-11-22 19:48:51 +010012499
12500/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12501 * now.
12502 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012503static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau12207b32016-11-22 19:48:51 +010012504{
12505 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12506 return 1;
12507
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012508 if (*args[2]) {
12509 struct proxy *px;
12510
12511 px = proxy_find_by_name(args[2], 0, 0);
12512 if (px)
12513 appctx->ctx.errors.iid = px->uuid;
12514 else
12515 appctx->ctx.errors.iid = atoi(args[2]);
12516
12517 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012518 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012519 appctx->ctx.cli.msg = "No such proxy.\n";
12520 appctx->st0 = CLI_ST_PRINT;
12521 return 1;
12522 }
12523 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012524 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012525 appctx->ctx.errors.iid = -1; // dump all proxies
12526
Willy Tarreau35069f82016-11-25 09:16:37 +010012527 appctx->ctx.errors.flag = 0;
12528 if (strcmp(args[3], "request") == 0)
12529 appctx->ctx.errors.flag |= 4; // ignore response
12530 else if (strcmp(args[3], "response") == 0)
12531 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012532 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012533 return 0;
12534}
12535
12536/* This function dumps all captured errors onto the stream interface's
12537 * read buffer. It returns 0 if the output buffer is full and it needs
12538 * to be called again, otherwise non-zero.
12539 */
12540static int cli_io_handler_show_errors(struct appctx *appctx)
12541{
12542 struct stream_interface *si = appctx->owner;
12543 extern const char *monthname[12];
12544
12545 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12546 return 1;
12547
12548 chunk_reset(&trash);
12549
12550 if (!appctx->ctx.errors.px) {
12551 /* the function had not been called yet, let's prepare the
12552 * buffer for a response.
12553 */
12554 struct tm tm;
12555
12556 get_localtime(date.tv_sec, &tm);
12557 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12558 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12559 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12560 error_snapshot_id);
12561
Willy Tarreau06d80a92017-10-19 14:32:15 +020012562 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012563 /* Socket buffer full. Let's try again later from the same point */
12564 si_applet_cant_put(si);
12565 return 0;
12566 }
12567
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012568 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012569 appctx->ctx.errors.bol = 0;
12570 appctx->ctx.errors.ptr = -1;
12571 }
12572
12573 /* we have two inner loops here, one for the proxy, the other one for
12574 * the buffer.
12575 */
12576 while (appctx->ctx.errors.px) {
12577 struct error_snapshot *es;
12578
Willy Tarreau35069f82016-11-25 09:16:37 +010012579 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012580 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012581 if (appctx->ctx.errors.flag & 2) // skip req
12582 goto next;
12583 }
12584 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012585 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012586 if (appctx->ctx.errors.flag & 4) // skip resp
12587 goto next;
12588 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012589
12590 if (!es->when.tv_sec)
12591 goto next;
12592
12593 if (appctx->ctx.errors.iid >= 0 &&
12594 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12595 es->oe->uuid != appctx->ctx.errors.iid)
12596 goto next;
12597
12598 if (appctx->ctx.errors.ptr < 0) {
12599 /* just print headers now */
12600
12601 char pn[INET6_ADDRSTRLEN];
12602 struct tm tm;
12603 int port;
12604
12605 get_localtime(es->when.tv_sec, &tm);
12606 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12607 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12608 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12609
12610 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12611 case AF_INET:
12612 case AF_INET6:
12613 port = get_host_port(&es->src);
12614 break;
12615 default:
12616 port = 0;
12617 }
12618
Willy Tarreau35069f82016-11-25 09:16:37 +010012619 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012620 case 0:
12621 chunk_appendf(&trash,
12622 " frontend %s (#%d): invalid request\n"
12623 " backend %s (#%d)",
12624 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12625 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12626 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12627 break;
12628 case 1:
12629 chunk_appendf(&trash,
12630 " backend %s (#%d): invalid response\n"
12631 " frontend %s (#%d)",
12632 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12633 es->oe->id, es->oe->uuid);
12634 break;
12635 }
12636
12637 chunk_appendf(&trash,
12638 ", server %s (#%d), event #%u\n"
12639 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012640 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012641 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12642 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12643 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12644 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12645 es->ev_id,
12646 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012647 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012648 es->m_clen, es->m_blen,
12649 es->b_flags, es->b_out, es->b_tot,
12650 es->len, es->b_wrap, es->pos);
12651
Willy Tarreau06d80a92017-10-19 14:32:15 +020012652 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012653 /* Socket buffer full. Let's try again later from the same point */
12654 si_applet_cant_put(si);
12655 return 0;
12656 }
12657 appctx->ctx.errors.ptr = 0;
12658 appctx->ctx.errors.sid = es->sid;
12659 }
12660
12661 if (appctx->ctx.errors.sid != es->sid) {
12662 /* the snapshot changed while we were dumping it */
12663 chunk_appendf(&trash,
12664 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012665 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012666 si_applet_cant_put(si);
12667 return 0;
12668 }
12669 goto next;
12670 }
12671
12672 /* OK, ptr >= 0, so we have to dump the current line */
12673 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12674 int newptr;
12675 int newline;
12676
12677 newline = appctx->ctx.errors.bol;
12678 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12679 if (newptr == appctx->ctx.errors.ptr)
12680 return 0;
12681
Willy Tarreau06d80a92017-10-19 14:32:15 +020012682 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012683 /* Socket buffer full. Let's try again later from the same point */
12684 si_applet_cant_put(si);
12685 return 0;
12686 }
12687 appctx->ctx.errors.ptr = newptr;
12688 appctx->ctx.errors.bol = newline;
12689 };
12690 next:
12691 appctx->ctx.errors.bol = 0;
12692 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012693 appctx->ctx.errors.flag ^= 1;
12694 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012695 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012696 }
12697
12698 /* dump complete */
12699 return 1;
12700}
12701
12702/* register cli keywords */
12703static struct cli_kw_list cli_kws = {{ },{
12704 { { "show", "errors", NULL },
12705 "show errors : report last request and response errors for each proxy",
12706 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12707 },
12708 {{},}
12709}};
12710
Willy Tarreau4a568972010-05-12 08:08:50 +020012711/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012712/* All supported ACL keywords must be declared here. */
12713/************************************************************************/
12714
12715/* Note: must not be declared <const> as its list will be overwritten.
12716 * Please take care of keeping this list alphabetically sorted.
12717 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012718static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012719 { "base", "base", PAT_MATCH_STR },
12720 { "base_beg", "base", PAT_MATCH_BEG },
12721 { "base_dir", "base", PAT_MATCH_DIR },
12722 { "base_dom", "base", PAT_MATCH_DOM },
12723 { "base_end", "base", PAT_MATCH_END },
12724 { "base_len", "base", PAT_MATCH_LEN },
12725 { "base_reg", "base", PAT_MATCH_REG },
12726 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012727
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012728 { "cook", "req.cook", PAT_MATCH_STR },
12729 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12730 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12731 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12732 { "cook_end", "req.cook", PAT_MATCH_END },
12733 { "cook_len", "req.cook", PAT_MATCH_LEN },
12734 { "cook_reg", "req.cook", PAT_MATCH_REG },
12735 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012736
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012737 { "hdr", "req.hdr", PAT_MATCH_STR },
12738 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12739 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12740 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12741 { "hdr_end", "req.hdr", PAT_MATCH_END },
12742 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12743 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12744 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012745
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012746 /* these two declarations uses strings with list storage (in place
12747 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12748 * and delete functions are relative to the list management. The parse
12749 * and match method are related to the corresponding fetch methods. This
12750 * is very particular ACL declaration mode.
12751 */
12752 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12753 { "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 +020012754
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012755 { "path", "path", PAT_MATCH_STR },
12756 { "path_beg", "path", PAT_MATCH_BEG },
12757 { "path_dir", "path", PAT_MATCH_DIR },
12758 { "path_dom", "path", PAT_MATCH_DOM },
12759 { "path_end", "path", PAT_MATCH_END },
12760 { "path_len", "path", PAT_MATCH_LEN },
12761 { "path_reg", "path", PAT_MATCH_REG },
12762 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012763
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012764 { "req_ver", "req.ver", PAT_MATCH_STR },
12765 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012766
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012767 { "scook", "res.cook", PAT_MATCH_STR },
12768 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12769 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12770 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12771 { "scook_end", "res.cook", PAT_MATCH_END },
12772 { "scook_len", "res.cook", PAT_MATCH_LEN },
12773 { "scook_reg", "res.cook", PAT_MATCH_REG },
12774 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012775
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012776 { "shdr", "res.hdr", PAT_MATCH_STR },
12777 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12778 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12779 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12780 { "shdr_end", "res.hdr", PAT_MATCH_END },
12781 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12782 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12783 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012784
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012785 { "url", "url", PAT_MATCH_STR },
12786 { "url_beg", "url", PAT_MATCH_BEG },
12787 { "url_dir", "url", PAT_MATCH_DIR },
12788 { "url_dom", "url", PAT_MATCH_DOM },
12789 { "url_end", "url", PAT_MATCH_END },
12790 { "url_len", "url", PAT_MATCH_LEN },
12791 { "url_reg", "url", PAT_MATCH_REG },
12792 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012793
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012794 { "urlp", "urlp", PAT_MATCH_STR },
12795 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12796 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12797 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12798 { "urlp_end", "urlp", PAT_MATCH_END },
12799 { "urlp_len", "urlp", PAT_MATCH_LEN },
12800 { "urlp_reg", "urlp", PAT_MATCH_REG },
12801 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012802
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012803 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012804}};
12805
12806/************************************************************************/
12807/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012808/************************************************************************/
12809/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012810static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012811 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012812 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012813 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12814
Willy Tarreau87b09662015-04-03 00:22:06 +020012815 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012816 { "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 +020012817
12818 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012819 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12820 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12821 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012822
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012823 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12824 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012825
Willy Tarreau409bcde2013-01-08 00:31:00 +010012826 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12827 * are only here to match the ACL's name, are request-only and are used
12828 * for ACL compatibility only.
12829 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012830 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12831 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012832 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12833 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012834
12835 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12836 * only here to match the ACL's name, are request-only and are used for
12837 * ACL compatibility only.
12838 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012839 { "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 +020012840 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012841 { "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 +020012842 { "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 +010012843
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012844 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012845 { "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 +010012846 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012847 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012848 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012849 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012850
12851 /* HTTP protocol on the request path */
12852 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012853 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012854
12855 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012856 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12857 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012858
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012859 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012860 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12861 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012862 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012863
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012864 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012865 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12866
Willy Tarreau18ed2562013-01-14 15:56:36 +010012867 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012868 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12869 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012870
Willy Tarreau18ed2562013-01-14 15:56:36 +010012871 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012872 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012873 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12874 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012875
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012876 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012877 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012878 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012879 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012880 { "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 +010012881 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012882 { "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 +010012883
12884 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012885 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012886 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12887 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012888
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012889 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012890 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012891 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012892 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012893 { "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 +010012894 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012895 { "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 +010012896
Willy Tarreau409bcde2013-01-08 00:31:00 +010012897 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012898 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012899 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12900 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012901 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012902
12903 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012904 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012905 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012906 { "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 +020012907 { "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 +010012908
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012909 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012910 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012911 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012912 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012913 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012914 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012915 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012916 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12917 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012918 { "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 +010012919 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012920}};
12921
Willy Tarreau8797c062007-05-07 00:55:35 +020012922
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012923/************************************************************************/
12924/* All supported converter keywords must be declared here. */
12925/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012926/* Note: must not be declared <const> as its list will be overwritten */
12927static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012928 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012929 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012930 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12931 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012932 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012933 { NULL, NULL, 0, 0, 0 },
12934}};
12935
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012936
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012937/************************************************************************/
12938/* All supported http-request action keywords must be declared here. */
12939/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012940struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012941 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012942 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012943 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012944 { "set-method", parse_set_req_line },
12945 { "set-path", parse_set_req_line },
12946 { "set-query", parse_set_req_line },
12947 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012948 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012949 }
12950};
12951
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012952struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012953 .kw = {
12954 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012955 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012956 { NULL, NULL }
12957 }
12958};
12959
Willy Tarreau8797c062007-05-07 00:55:35 +020012960__attribute__((constructor))
12961static void __http_protocol_init(void)
12962{
12963 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012964 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012965 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012966 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012967 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012968 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012969}
12970
12971
Willy Tarreau58f10d72006-12-04 02:26:12 +010012972/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012973 * Local variables:
12974 * c-indent-level: 8
12975 * c-basic-offset: 8
12976 * End:
12977 */