blob: efa6d6a36930f3f4636a83c0c1ae0a0b41868631 [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;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004881 channel_dont_close(req); /* don't fail on early shutr */
4882 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004883 }
4884 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4885 }
4886
Willy Tarreau80a92c02014-03-12 10:41:13 +01004887 /* in most states, we should abort in case of early close */
4888 channel_auto_close(req);
4889
Willy Tarreauefdf0942014-04-24 20:08:57 +02004890 if (req->to_forward) {
4891 /* We can't process the buffer's contents yet */
4892 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004893 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004894 }
4895
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004896 if (msg->msg_state < HTTP_MSG_DONE) {
4897 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4898 ? http_msg_forward_chunked_body(s, msg)
4899 : http_msg_forward_body(s, msg));
4900 if (!ret)
4901 goto missing_data_or_waiting;
4902 if (ret < 0)
4903 goto return_bad_req;
4904 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004905
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004906 /* other states, DONE...TUNNEL */
4907 /* we don't want to forward closes on DONE except in tunnel mode. */
4908 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4909 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004910
Christopher Faulet894da4c2017-07-18 11:29:07 +02004911 http_resync_states(s);
4912 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004913 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4914 if (req->flags & CF_SHUTW) {
4915 /* request errors are most likely due to the
4916 * server aborting the transfer. */
4917 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004918 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004919 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004920 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004921 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004922 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004923 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004924 }
4925
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004926 /* If "option abortonclose" is set on the backend, we want to monitor
4927 * the client's connection and forward any shutdown notification to the
4928 * server, which will decide whether to close or to go on processing the
4929 * request. We only do that in tunnel mode, and not in other modes since
4930 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004931 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004932 channel_auto_read(req);
4933 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4934 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4935 s->si[1].flags |= SI_FL_NOLINGER;
4936 channel_auto_close(req);
4937 }
4938 else if (s->txn->meth == HTTP_METH_POST) {
4939 /* POST requests may require to read extra CRLF sent by broken
4940 * browsers and which could cause an RST to be sent upon close
4941 * on some systems (eg: Linux). */
4942 channel_auto_read(req);
4943 }
4944 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004945
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004946 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004947 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004948 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004949 if (!(s->flags & SF_ERR_MASK))
4950 s->flags |= SF_ERR_CLICL;
4951 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004952 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004953 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004954 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004955 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004956 }
4957
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004958 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4959 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004960 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004961 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004962
4963 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004964 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004965
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004966 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004967 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004968 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004969 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004971 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004972 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004973 * And when content-length is used, we never want to let the possible
4974 * shutdown be forwarded to the other side, as the state machine will
4975 * take care of it once the client responds. It's also important to
4976 * prevent TIME_WAITs from accumulating on the backend side, and for
4977 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004978 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004979 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004980 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004981
Willy Tarreau5c620922011-05-11 19:56:11 +02004982 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004983 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004984 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004985 * modes are already handled by the stream sock layer. We must not do
4986 * this in content-length mode because it could present the MSG_MORE
4987 * flag with the last block of forwarded data, which would cause an
4988 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004989 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004990 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004991 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004992
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004993 return 0;
4994
Willy Tarreaud98cf932009-12-27 22:54:55 +01004995 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004996 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004997 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004998 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004999
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005000 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005001 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005002 txn->req.msg_state = HTTP_MSG_ERROR;
5003 if (txn->status) {
5004 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005005 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005006 } else {
5007 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005008 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005009 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005010 req->analysers &= AN_REQ_FLT_END;
5011 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 +01005012
Willy Tarreaue7dff022015-04-03 01:14:29 +02005013 if (!(s->flags & SF_ERR_MASK))
5014 s->flags |= SF_ERR_PRXCOND;
5015 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005016 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005017 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005018 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005019 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005020 }
5021 return 0;
5022
5023 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005024 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005025 txn->req.msg_state = HTTP_MSG_ERROR;
5026 if (txn->status) {
5027 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005028 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005029 } else {
5030 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005031 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005032 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005033 req->analysers &= AN_REQ_FLT_END;
5034 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 +01005035
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005036 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5037 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005038 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005039 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005040
Willy Tarreaue7dff022015-04-03 01:14:29 +02005041 if (!(s->flags & SF_ERR_MASK))
5042 s->flags |= SF_ERR_SRVCL;
5043 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005044 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005045 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005046 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005047 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005048 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005049 return 0;
5050}
5051
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5053 * processing can continue on next analysers, or zero if it either needs more
5054 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005055 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005056 * when it has nothing left to do, and may remove any analyser when it wants to
5057 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005058 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005059int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005060{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005061 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005062 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005063 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005064 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005065 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005066 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005067 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005068
Willy Tarreau87b09662015-04-03 00:22:06 +02005069 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 +02005070 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005071 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005072 rep,
5073 rep->rex, rep->wex,
5074 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005075 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005076 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005077
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005078 /*
5079 * Now parse the partial (or complete) lines.
5080 * We will check the response syntax, and also join multi-line
5081 * headers. An index of all the lines will be elaborated while
5082 * parsing.
5083 *
5084 * For the parsing, we use a 28 states FSM.
5085 *
5086 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005087 * rep->buf->p = beginning of response
5088 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5089 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005090 * msg->eol = end of current header or line (LF or CRLF)
5091 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092 */
5093
Willy Tarreau628c40c2014-04-24 19:11:26 +02005094 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005095 /* There's a protected area at the end of the buffer for rewriting
5096 * purposes. We don't want to start to parse the request if the
5097 * protected area is affected, because we may have to move processed
5098 * data later, which is much more complicated.
5099 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005100 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005101 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005102 /* some data has still not left the buffer, wake us once that's done */
5103 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5104 goto abort_response;
5105 channel_dont_close(rep);
5106 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005107 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005108 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005109 }
5110
Willy Tarreau379357a2013-06-08 12:55:46 +02005111 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5112 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5113 buffer_slow_realign(rep->buf);
5114
Willy Tarreau9b28e032012-10-12 23:49:43 +02005115 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005116 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005117 }
5118
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005119 /* 1: we might have to print this header in debug mode */
5120 if (unlikely((global.mode & MODE_DEBUG) &&
5121 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005122 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005124
Willy Tarreau9b28e032012-10-12 23:49:43 +02005125 sol = rep->buf->p;
5126 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005127 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005128
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005129 sol += hdr_idx_first_pos(&txn->hdr_idx);
5130 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005131
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 while (cur_idx) {
5133 eol = sol + txn->hdr_idx.v[cur_idx].len;
5134 debug_hdr("srvhdr", s, sol, eol);
5135 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5136 cur_idx = txn->hdr_idx.v[cur_idx].next;
5137 }
5138 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005140 /*
5141 * Now we quickly check if we have found a full valid response.
5142 * If not so, we check the FD and buffer states before leaving.
5143 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005144 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005145 * responses are checked first.
5146 *
5147 * Depending on whether the client is still there or not, we
5148 * may send an error response back or not. Note that normally
5149 * we should only check for HTTP status there, and check I/O
5150 * errors somewhere else.
5151 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005152
Willy Tarreau655dce92009-11-08 13:10:58 +01005153 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005154 /* Invalid response */
5155 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5156 /* we detected a parsing error. We want to archive this response
5157 * in the dedicated proxy area for later troubleshooting.
5158 */
5159 hdr_response_bad:
5160 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005161 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005162
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005163 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005164 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005165 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005166 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005167 }
Willy Tarreau64648412010-03-05 10:41:54 +01005168 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005169 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005170 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005171 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005172 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005173 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005174 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005175
Willy Tarreaue7dff022015-04-03 01:14:29 +02005176 if (!(s->flags & SF_ERR_MASK))
5177 s->flags |= SF_ERR_PRXCOND;
5178 if (!(s->flags & SF_FINST_MASK))
5179 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005180
5181 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005182 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005183
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005184 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005185 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005186 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005187 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005188 goto hdr_response_bad;
5189 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005192 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005194 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005195 else if (txn->flags & TX_NOT_FIRST)
5196 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005197
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005198 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005199 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005200 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005201 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005202 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005203
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005204 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005205 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005206 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005207
5208 /* Check to see if the server refused the early data.
5209 * If so, just send a 425
5210 */
5211 if (objt_cs(s->si[1].end)) {
5212 struct connection *conn = objt_cs(s->si[1].end)->conn;
5213
5214 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5215 txn->status = 425;
5216 }
5217
Willy Tarreau350f4872014-11-28 14:42:25 +01005218 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005219 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005220 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005221
Willy Tarreaue7dff022015-04-03 01:14:29 +02005222 if (!(s->flags & SF_ERR_MASK))
5223 s->flags |= SF_ERR_SRVCL;
5224 if (!(s->flags & SF_FINST_MASK))
5225 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005226 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005227 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005228
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005229 /* read timeout : return a 504 to the client. */
5230 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005231 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005232 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005233
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005234 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005235 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005236 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005237 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005238 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005239
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005240 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005241 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005242 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005243 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005244 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005245 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005246
Willy Tarreaue7dff022015-04-03 01:14:29 +02005247 if (!(s->flags & SF_ERR_MASK))
5248 s->flags |= SF_ERR_SRVTO;
5249 if (!(s->flags & SF_FINST_MASK))
5250 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005251 return 0;
5252 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005253
Willy Tarreauf003d372012-11-26 13:35:37 +01005254 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005255 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005256 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5257 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005258 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005259 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005260
Christopher Faulet0184ea72017-01-05 14:06:34 +01005261 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005262 channel_auto_close(rep);
5263
5264 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005265 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005266 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005267
Willy Tarreaue7dff022015-04-03 01:14:29 +02005268 if (!(s->flags & SF_ERR_MASK))
5269 s->flags |= SF_ERR_CLICL;
5270 if (!(s->flags & SF_FINST_MASK))
5271 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005272
Willy Tarreau87b09662015-04-03 00:22:06 +02005273 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005274 return 0;
5275 }
5276
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005277 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005278 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005279 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005280 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005281 else if (txn->flags & TX_NOT_FIRST)
5282 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005283
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005284 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005285 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005286 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005287 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005288 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005289
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005290 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005291 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005293 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005294 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005295 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005296
Willy Tarreaue7dff022015-04-03 01:14:29 +02005297 if (!(s->flags & SF_ERR_MASK))
5298 s->flags |= SF_ERR_SRVCL;
5299 if (!(s->flags & SF_FINST_MASK))
5300 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005301 return 0;
5302 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005303
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005304 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005305 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005306 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005307 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005308 else if (txn->flags & TX_NOT_FIRST)
5309 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005310
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005311 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005312 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005313 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005314
Willy Tarreaue7dff022015-04-03 01:14:29 +02005315 if (!(s->flags & SF_ERR_MASK))
5316 s->flags |= SF_ERR_CLICL;
5317 if (!(s->flags & SF_FINST_MASK))
5318 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005319
Willy Tarreau87b09662015-04-03 00:22:06 +02005320 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005321 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005322 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005323
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005324 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005325 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005326 return 0;
5327 }
5328
5329 /* More interesting part now : we know that we have a complete
5330 * response which at least looks like HTTP. We have an indicator
5331 * of each header's length, so we can parse them quickly.
5332 */
5333
5334 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005335 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005336
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005337 /*
5338 * 1: get the status code
5339 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005340 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005341 if (n < 1 || n > 5)
5342 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005343 /* when the client triggers a 4xx from the server, it's most often due
5344 * to a missing object or permission. These events should be tracked
5345 * because if they happen often, it may indicate a brute force or a
5346 * vulnerability scan.
5347 */
5348 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005349 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005350
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005351 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005352 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005353
Willy Tarreau91852eb2015-05-01 13:26:00 +02005354 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5355 * exactly one digit "." one digit. This check may be disabled using
5356 * option accept-invalid-http-response.
5357 */
5358 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5359 if (msg->sl.st.v_l != 8) {
5360 msg->err_pos = 0;
5361 goto hdr_response_bad;
5362 }
5363
5364 if (rep->buf->p[4] != '/' ||
5365 !isdigit((unsigned char)rep->buf->p[5]) ||
5366 rep->buf->p[6] != '.' ||
5367 !isdigit((unsigned char)rep->buf->p[7])) {
5368 msg->err_pos = 4;
5369 goto hdr_response_bad;
5370 }
5371 }
5372
Willy Tarreau5b154472009-12-21 20:11:07 +01005373 /* check if the response is HTTP/1.1 or above */
5374 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005375 ((rep->buf->p[5] > '1') ||
5376 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005377 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005378
5379 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005380 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 +01005381
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005382 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005383 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005384
Willy Tarreau9b28e032012-10-12 23:49:43 +02005385 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005386
Willy Tarreau39650402010-03-15 19:44:39 +01005387 /* Adjust server's health based on status code. Note: status codes 501
5388 * and 505 are triggered on demand by client request, so we must not
5389 * count them as server failures.
5390 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005391 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005392 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005393 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005394 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005395 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005396 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005397
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005399 * We may be facing a 100-continue response, or any other informational
5400 * 1xx response which is non-final, in which case this is not the right
5401 * response, and we're waiting for the next one. Let's allow this response
5402 * to go to the client and wait for the next one. There's an exception for
5403 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005404 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005405 if (txn->status < 200 &&
5406 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005407 hdr_idx_init(&txn->hdr_idx);
5408 msg->next -= channel_forward(rep, msg->next);
5409 msg->msg_state = HTTP_MSG_RPBEFORE;
5410 txn->status = 0;
5411 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005412 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005413 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005414 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005415
Willy Tarreaua14ad722017-07-07 11:36:32 +02005416 /*
5417 * 2: check for cacheability.
5418 */
5419
5420 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005421 case 200:
5422 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005423 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005424 case 206:
5425 case 300:
5426 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005427 case 404:
5428 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005429 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005430 case 414:
5431 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005432 break;
5433 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005434 /* RFC7231#6.1:
5435 * Responses with status codes that are defined as
5436 * cacheable by default (e.g., 200, 203, 204, 206,
5437 * 300, 301, 404, 405, 410, 414, and 501 in this
5438 * specification) can be reused by a cache with
5439 * heuristic expiration unless otherwise indicated
5440 * by the method definition or explicit cache
5441 * controls [RFC7234]; all other status codes are
5442 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005443 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005444 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005445 break;
5446 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005447
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005448 /*
5449 * 3: we may need to capture headers
5450 */
5451 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005452 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005453 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005454 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005455
Willy Tarreau557f1992015-05-01 10:05:17 +02005456 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5457 * by RFC7230#3.3.3 :
5458 *
5459 * The length of a message body is determined by one of the following
5460 * (in order of precedence):
5461 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005462 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5463 * the connection will become a tunnel immediately after the empty
5464 * line that concludes the header fields. A client MUST ignore
5465 * any Content-Length or Transfer-Encoding header fields received
5466 * in such a message. Any 101 response (Switching Protocols) is
5467 * managed in the same manner.
5468 *
5469 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005470 * (Informational), 204 (No Content), or 304 (Not Modified) status
5471 * code is always terminated by the first empty line after the
5472 * header fields, regardless of the header fields present in the
5473 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005474 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005475 * 3. If a Transfer-Encoding header field is present and the chunked
5476 * transfer coding (Section 4.1) is the final encoding, the message
5477 * body length is determined by reading and decoding the chunked
5478 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005479 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005480 * If a Transfer-Encoding header field is present in a response and
5481 * the chunked transfer coding is not the final encoding, the
5482 * message body length is determined by reading the connection until
5483 * it is closed by the server. If a Transfer-Encoding header field
5484 * is present in a request and the chunked transfer coding is not
5485 * the final encoding, the message body length cannot be determined
5486 * reliably; the server MUST respond with the 400 (Bad Request)
5487 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005488 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005489 * If a message is received with both a Transfer-Encoding and a
5490 * Content-Length header field, the Transfer-Encoding overrides the
5491 * Content-Length. Such a message might indicate an attempt to
5492 * perform request smuggling (Section 9.5) or response splitting
5493 * (Section 9.4) and ought to be handled as an error. A sender MUST
5494 * remove the received Content-Length field prior to forwarding such
5495 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005496 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005497 * 4. If a message is received without Transfer-Encoding and with
5498 * either multiple Content-Length header fields having differing
5499 * field-values or a single Content-Length header field having an
5500 * invalid value, then the message framing is invalid and the
5501 * recipient MUST treat it as an unrecoverable error. If this is a
5502 * request message, the server MUST respond with a 400 (Bad Request)
5503 * status code and then close the connection. If this is a response
5504 * message received by a proxy, the proxy MUST close the connection
5505 * to the server, discard the received response, and send a 502 (Bad
5506 * Gateway) response to the client. If this is a response message
5507 * received by a user agent, the user agent MUST close the
5508 * connection to the server and discard the received response.
5509 *
5510 * 5. If a valid Content-Length header field is present without
5511 * Transfer-Encoding, its decimal value defines the expected message
5512 * body length in octets. If the sender closes the connection or
5513 * the recipient times out before the indicated number of octets are
5514 * received, the recipient MUST consider the message to be
5515 * incomplete and close the connection.
5516 *
5517 * 6. If this is a request message and none of the above are true, then
5518 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005519 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005520 * 7. Otherwise, this is a response message without a declared message
5521 * body length, so the message body length is determined by the
5522 * number of octets received prior to the server closing the
5523 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005524 */
5525
5526 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005527 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005528 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005529 * FIXME: should we parse anyway and return an error on chunked encoding ?
5530 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005531 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5532 txn->status == 101)) {
5533 /* Either we've established an explicit tunnel, or we're
5534 * switching the protocol. In both cases, we're very unlikely
5535 * to understand the next protocols. We have to switch to tunnel
5536 * mode, so that we transfer the request and responses then let
5537 * this protocol pass unmodified. When we later implement specific
5538 * parsers for such protocols, we'll want to check the Upgrade
5539 * header which contains information about that protocol for
5540 * responses with status 101 (eg: see RFC2817 about TLS).
5541 */
5542 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5543 msg->flags |= HTTP_MSGF_XFER_LEN;
5544 goto end;
5545 }
5546
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005547 if (txn->meth == HTTP_METH_HEAD ||
5548 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005549 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005550 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005551 goto skip_content_length;
5552 }
5553
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005554 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005555 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005556 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005557 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005558 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5559 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005560 /* bad transfer-encoding (chunked followed by something else) */
5561 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005562 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005563 break;
5564 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005565 }
5566
Willy Tarreau1c913912015-04-30 10:57:51 +02005567 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005568 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005569 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005570 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5571 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5572 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005573 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574 signed long long cl;
5575
Willy Tarreauad14f752011-09-02 20:33:27 +02005576 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005577 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005578 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005579 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005580
Willy Tarreauad14f752011-09-02 20:33:27 +02005581 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005582 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005583 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005584 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005585
Willy Tarreauad14f752011-09-02 20:33:27 +02005586 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005587 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005588 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005589 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005590
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005591 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005592 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005593 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005594 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005595
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005596 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005597 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005598 }
5599
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005600 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005601 /* Now we have to check if we need to modify the Connection header.
5602 * This is more difficult on the response than it is on the request,
5603 * because we can have two different HTTP versions and we don't know
5604 * how the client will interprete a response. For instance, let's say
5605 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5606 * HTTP/1.1 response without any header. Maybe it will bound itself to
5607 * HTTP/1.0 because it only knows about it, and will consider the lack
5608 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5609 * the lack of header as a keep-alive. Thus we will use two flags
5610 * indicating how a request MAY be understood by the client. In case
5611 * of multiple possibilities, we'll fix the header to be explicit. If
5612 * ambiguous cases such as both close and keepalive are seen, then we
5613 * will fall back to explicit close. Note that we won't take risks with
5614 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005615 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005616 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005617 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5618 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5619 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5620 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005621 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005622
Willy Tarreau70dffda2014-01-30 03:07:23 +01005623 /* this situation happens when combining pretend-keepalive with httpclose. */
5624 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005625 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005626 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005627 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5628
Willy Tarreau60466522010-01-18 19:08:45 +01005629 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005630 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005631 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5632 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005633
Willy Tarreau60466522010-01-18 19:08:45 +01005634 /* now adjust header transformations depending on current state */
5635 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5636 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5637 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005638 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005639 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005640 }
Willy Tarreau60466522010-01-18 19:08:45 +01005641 else { /* SCL / KAL */
5642 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005643 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005644 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005645 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005646
Willy Tarreau60466522010-01-18 19:08:45 +01005647 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005648 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005649
Willy Tarreau60466522010-01-18 19:08:45 +01005650 /* Some keep-alive responses are converted to Server-close if
5651 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005652 */
Willy Tarreau60466522010-01-18 19:08:45 +01005653 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5654 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005655 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005656 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005657 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005658 }
5659
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005660 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005661 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005662 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005663
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005664 /* end of job, return OK */
5665 rep->analysers &= ~an_bit;
5666 rep->analyse_exp = TICK_ETERNITY;
5667 channel_auto_close(rep);
5668 return 1;
5669
5670 abort_keep_alive:
5671 /* A keep-alive request to the server failed on a network error.
5672 * The client is required to retry. We need to close without returning
5673 * any other information so that the client retries.
5674 */
5675 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005676 rep->analysers &= AN_RES_FLT_END;
5677 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005678 channel_auto_close(rep);
5679 s->logs.logwait = 0;
5680 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005681 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005682 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005683 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005684 return 0;
5685}
5686
5687/* This function performs all the processing enabled for the current response.
5688 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005689 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005690 * other functions. It works like process_request (see indications above).
5691 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005692int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005693{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005694 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005695 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005696 struct http_msg *msg = &txn->rsp;
5697 struct proxy *cur_proxy;
5698 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005699 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005700
Willy Tarreau87b09662015-04-03 00:22:06 +02005701 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 +02005702 now_ms, __FUNCTION__,
5703 s,
5704 rep,
5705 rep->rex, rep->wex,
5706 rep->flags,
5707 rep->buf->i,
5708 rep->analysers);
5709
5710 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5711 return 0;
5712
Willy Tarreau70730dd2014-04-24 18:06:27 +02005713 /* The stats applet needs to adjust the Connection header but we don't
5714 * apply any filter there.
5715 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005716 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5717 rep->analysers &= ~an_bit;
5718 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005719 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005720 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005721
Willy Tarreau58975672014-04-24 21:13:57 +02005722 /*
5723 * We will have to evaluate the filters.
5724 * As opposed to version 1.2, now they will be evaluated in the
5725 * filters order and not in the header order. This means that
5726 * each filter has to be validated among all headers.
5727 *
5728 * Filters are tried with ->be first, then with ->fe if it is
5729 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005730 *
5731 * Maybe we are in resume condiion. In this case I choose the
5732 * "struct proxy" which contains the rule list matching the resume
5733 * pointer. If none of theses "struct proxy" match, I initialise
5734 * the process with the first one.
5735 *
5736 * In fact, I check only correspondance betwwen the current list
5737 * pointer and the ->fe rule list. If it doesn't match, I initialize
5738 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005739 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005740 if (s->current_rule_list == &sess->fe->http_res_rules)
5741 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005742 else
5743 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005744 while (1) {
5745 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005746
Willy Tarreau58975672014-04-24 21:13:57 +02005747 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005748 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005749 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005750
Willy Tarreau51d861a2015-05-22 17:30:48 +02005751 if (ret == HTTP_RULE_RES_BADREQ)
5752 goto return_srv_prx_502;
5753
5754 if (ret == HTTP_RULE_RES_DONE) {
5755 rep->analysers &= ~an_bit;
5756 rep->analyse_exp = TICK_ETERNITY;
5757 return 1;
5758 }
5759 }
5760
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005761 /* we need to be called again. */
5762 if (ret == HTTP_RULE_RES_YIELD) {
5763 channel_dont_close(rep);
5764 return 0;
5765 }
5766
Willy Tarreau58975672014-04-24 21:13:57 +02005767 /* try headers filters */
5768 if (rule_set->rsp_exp != NULL) {
5769 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5770 return_bad_resp:
5771 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005772 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005773 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005774 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005775 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005776 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005777 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005778 txn->status = 502;
5779 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005780 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005781 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005782 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005783 if (!(s->flags & SF_ERR_MASK))
5784 s->flags |= SF_ERR_PRXCOND;
5785 if (!(s->flags & SF_FINST_MASK))
5786 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005787 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005788 }
Willy Tarreau58975672014-04-24 21:13:57 +02005789 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005790
Willy Tarreau58975672014-04-24 21:13:57 +02005791 /* has the response been denied ? */
5792 if (txn->flags & TX_SVDENY) {
5793 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005794 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005795
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005796 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5797 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005798 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005799 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005800
Willy Tarreau58975672014-04-24 21:13:57 +02005801 goto return_srv_prx_502;
5802 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005803
Willy Tarreau58975672014-04-24 21:13:57 +02005804 /* add response headers from the rule sets in the same order */
5805 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005806 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005807 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005808 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005809 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005810 ret = acl_pass(ret);
5811 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5812 ret = !ret;
5813 if (!ret)
5814 continue;
5815 }
5816 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5817 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005818 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005819
Willy Tarreau58975672014-04-24 21:13:57 +02005820 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005821 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005822 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005823 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005824 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005825
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005826 /* After this point, this anayzer can't return yield, so we can
5827 * remove the bit corresponding to this analyzer from the list.
5828 *
5829 * Note that the intermediate returns and goto found previously
5830 * reset the analyzers.
5831 */
5832 rep->analysers &= ~an_bit;
5833 rep->analyse_exp = TICK_ETERNITY;
5834
Willy Tarreau58975672014-04-24 21:13:57 +02005835 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005836 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005837 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005838
Willy Tarreau58975672014-04-24 21:13:57 +02005839 /*
5840 * Now check for a server cookie.
5841 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005842 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005843 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005844
Willy Tarreau58975672014-04-24 21:13:57 +02005845 /*
5846 * Check for cache-control or pragma headers if required.
5847 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005848 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005849 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005850
Willy Tarreau58975672014-04-24 21:13:57 +02005851 /*
5852 * Add server cookie in the response if needed
5853 */
5854 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5855 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005856 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005857 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5858 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5859 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5860 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5861 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005862 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005863 /* the server is known, it's not the one the client requested, or the
5864 * cookie's last seen date needs to be refreshed. We have to
5865 * insert a set-cookie here, except if we want to insert only on POST
5866 * requests and this one isn't. Note that servers which don't have cookies
5867 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005868 */
Willy Tarreau58975672014-04-24 21:13:57 +02005869 if (!objt_server(s->target)->cookie) {
5870 chunk_printf(&trash,
5871 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5872 s->be->cookie_name);
5873 }
5874 else {
5875 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005876
Willy Tarreau58975672014-04-24 21:13:57 +02005877 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5878 /* emit last_date, which is mandatory */
5879 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5880 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5881 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005882
Willy Tarreau58975672014-04-24 21:13:57 +02005883 if (s->be->cookie_maxlife) {
5884 /* emit first_date, which is either the original one or
5885 * the current date.
5886 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005887 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005888 s30tob64(txn->cookie_first_date ?
5889 txn->cookie_first_date >> 2 :
5890 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005891 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005892 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005893 }
Willy Tarreau58975672014-04-24 21:13:57 +02005894 chunk_appendf(&trash, "; path=/");
5895 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005896
Willy Tarreau58975672014-04-24 21:13:57 +02005897 if (s->be->cookie_domain)
5898 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005899
Willy Tarreau58975672014-04-24 21:13:57 +02005900 if (s->be->ck_opts & PR_CK_HTTPONLY)
5901 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005902
Willy Tarreau58975672014-04-24 21:13:57 +02005903 if (s->be->ck_opts & PR_CK_SECURE)
5904 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005905
Willy Tarreau58975672014-04-24 21:13:57 +02005906 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5907 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005908
Willy Tarreau58975672014-04-24 21:13:57 +02005909 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005910 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005911 /* the server did not change, only the date was updated */
5912 txn->flags |= TX_SCK_UPDATED;
5913 else
5914 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005915
Willy Tarreau58975672014-04-24 21:13:57 +02005916 /* Here, we will tell an eventual cache on the client side that we don't
5917 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5918 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5919 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005920 */
Willy Tarreau58975672014-04-24 21:13:57 +02005921 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005922
Willy Tarreau58975672014-04-24 21:13:57 +02005923 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005924
Willy Tarreau58975672014-04-24 21:13:57 +02005925 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5926 "Cache-control: private", 22) < 0))
5927 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005928 }
Willy Tarreau58975672014-04-24 21:13:57 +02005929 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005930
Willy Tarreau58975672014-04-24 21:13:57 +02005931 /*
5932 * Check if result will be cacheable with a cookie.
5933 * We'll block the response if security checks have caught
5934 * nasty things such as a cacheable cookie.
5935 */
5936 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5937 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5938 (s->be->options & PR_O_CHK_CACHE)) {
5939 /* we're in presence of a cacheable response containing
5940 * a set-cookie header. We'll block it as requested by
5941 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005942 */
Willy Tarreau58975672014-04-24 21:13:57 +02005943 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005944 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005945
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005946 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5947 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005948 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005949 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005950
Christopher Faulet767a84b2017-11-24 16:50:31 +01005951 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5952 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005953 send_log(s->be, LOG_ALERT,
5954 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5955 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5956 goto return_srv_prx_502;
5957 }
Willy Tarreau03945942009-12-22 16:50:27 +01005958
Willy Tarreau70730dd2014-04-24 18:06:27 +02005959 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005960 /*
5961 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5962 * If an "Upgrade" token is found, the header is left untouched in order
5963 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005964 * if anything but "Upgrade" is present in the Connection header. We don't
5965 * want to touch any 101 response either since it's switching to another
5966 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005967 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005968 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005969 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005970 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005971 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5972 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005973
Willy Tarreau58975672014-04-24 21:13:57 +02005974 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5975 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5976 /* we want a keep-alive response here. Keep-alive header
5977 * required if either side is not 1.1.
5978 */
5979 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5980 want_flags |= TX_CON_KAL_SET;
5981 }
5982 else {
5983 /* we want a close response here. Close header required if
5984 * the server is 1.1, regardless of the client.
5985 */
5986 if (msg->flags & HTTP_MSGF_VER_11)
5987 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005988 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005989
Willy Tarreau58975672014-04-24 21:13:57 +02005990 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5991 http_change_connection_header(txn, msg, want_flags);
5992 }
5993
5994 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005995 /* Always enter in the body analyzer */
5996 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5997 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005998
Willy Tarreau58975672014-04-24 21:13:57 +02005999 /* if the user wants to log as soon as possible, without counting
6000 * bytes from the server, then this is the right moment. We have
6001 * to temporarily assign bytes_out to log what we currently have.
6002 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006003 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006004 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6005 s->logs.bytes_out = txn->rsp.eoh;
6006 s->do_log(s);
6007 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006008 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006009 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006010}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006011
Willy Tarreaud98cf932009-12-27 22:54:55 +01006012/* This function is an analyser which forwards response body (including chunk
6013 * sizes if any). It is called as soon as we must forward, even if we forward
6014 * zero byte. The only situation where it must not be called is when we're in
6015 * tunnel mode and we want to forward till the close. It's used both to forward
6016 * remaining data and to resync after end of body. It expects the msg_state to
6017 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006018 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006019 *
6020 * It is capable of compressing response data both in content-length mode and
6021 * in chunked mode. The state machines follows different flows depending on
6022 * whether content-length and chunked modes are used, since there are no
6023 * trailers in content-length :
6024 *
6025 * chk-mode cl-mode
6026 * ,----- BODY -----.
6027 * / \
6028 * V size > 0 V chk-mode
6029 * .--> SIZE -------------> DATA -------------> CRLF
6030 * | | size == 0 | last byte |
6031 * | v final crlf v inspected |
6032 * | TRAILERS -----------> DONE |
6033 * | |
6034 * `----------------------------------------------'
6035 *
6036 * Compression only happens in the DATA state, and must be flushed in final
6037 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6038 * is performed at once on final states for all bytes parsed, or when leaving
6039 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006040 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006041int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006042{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006043 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006044 struct http_txn *txn = s->txn;
6045 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006046 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006047
Christopher Faulet814d2702017-03-30 11:33:44 +02006048 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6049 now_ms, __FUNCTION__,
6050 s,
6051 res,
6052 res->rex, res->wex,
6053 res->flags,
6054 res->buf->i,
6055 res->analysers);
6056
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006057 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6058 return 0;
6059
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006060 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006061 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006062 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006063 /* Output closed while we were sending data. We must abort and
6064 * wake the other side up.
6065 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006066 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006067 msg->msg_state = HTTP_MSG_ERROR;
6068 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006069 return 1;
6070 }
6071
Willy Tarreau4fe41902010-06-07 22:27:41 +02006072 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006073 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006074
Christopher Fauletd7c91962015-04-30 11:48:27 +02006075 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006076 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6077 ? HTTP_MSG_CHUNK_SIZE
6078 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006079 }
6080
Willy Tarreauefdf0942014-04-24 20:08:57 +02006081 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006082 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006083 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006084 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006085 }
6086
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006087 if (msg->msg_state < HTTP_MSG_DONE) {
6088 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6089 ? http_msg_forward_chunked_body(s, msg)
6090 : http_msg_forward_body(s, msg));
6091 if (!ret)
6092 goto missing_data_or_waiting;
6093 if (ret < 0)
6094 goto return_bad_res;
6095 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006096
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006097 /* other states, DONE...TUNNEL */
6098 /* for keep-alive we don't want to forward closes on DONE */
6099 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6100 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6101 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006102
Christopher Faulet894da4c2017-07-18 11:29:07 +02006103 http_resync_states(s);
6104 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006105 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6106 if (res->flags & CF_SHUTW) {
6107 /* response errors are most likely due to the
6108 * client aborting the transfer. */
6109 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006110 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006111 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006112 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 +01006113 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006114 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006115 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006116 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006117 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006118
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006119 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006120 if (res->flags & CF_SHUTW)
6121 goto aborted_xfer;
6122
6123 /* stop waiting for data if the input is closed before the end. If the
6124 * client side was already closed, it means that the client has aborted,
6125 * so we don't want to count this as a server abort. Otherwise it's a
6126 * server abort.
6127 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006128 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006129 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006130 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006131 /* If we have some pending data, we continue the processing */
6132 if (!buffer_pending(res->buf)) {
6133 if (!(s->flags & SF_ERR_MASK))
6134 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006135 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006136 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006137 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006138 goto return_bad_res_stats_ok;
6139 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006140 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006141
Willy Tarreau40dba092010-03-04 18:14:51 +01006142 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006143 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006144 goto return_bad_res;
6145
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006146 /* When TE: chunked is used, we need to get there again to parse
6147 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006148 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6149 * or if there are filters registered on the stream, we don't want to
6150 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006151 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006152 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006153 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006154 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6155 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006156 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006157
Willy Tarreau5c620922011-05-11 19:56:11 +02006158 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006159 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006160 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006161 * modes are already handled by the stream sock layer. We must not do
6162 * this in content-length mode because it could present the MSG_MORE
6163 * flag with the last block of forwarded data, which would cause an
6164 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006165 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006166 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006167 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006168
Willy Tarreau87b09662015-04-03 00:22:06 +02006169 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006170 return 0;
6171
Willy Tarreau40dba092010-03-04 18:14:51 +01006172 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006173 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006174 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006175 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006176
6177 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006178 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006179 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006180 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006181 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006182 res->analysers &= AN_RES_FLT_END;
6183 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 +01006184 if (objt_server(s->target))
6185 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006186
Willy Tarreaue7dff022015-04-03 01:14:29 +02006187 if (!(s->flags & SF_ERR_MASK))
6188 s->flags |= SF_ERR_PRXCOND;
6189 if (!(s->flags & SF_FINST_MASK))
6190 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006191 return 0;
6192
6193 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006194 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006195 txn->rsp.msg_state = HTTP_MSG_ERROR;
6196 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006197 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006198 res->analysers &= AN_RES_FLT_END;
6199 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 +01006200
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006201 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6202 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006203 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006204 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006205
Willy Tarreaue7dff022015-04-03 01:14:29 +02006206 if (!(s->flags & SF_ERR_MASK))
6207 s->flags |= SF_ERR_CLICL;
6208 if (!(s->flags & SF_FINST_MASK))
6209 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006210 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006211}
6212
6213
6214static inline int
6215http_msg_forward_body(struct stream *s, struct http_msg *msg)
6216{
6217 struct channel *chn = msg->chn;
6218 int ret;
6219
6220 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6221
6222 if (msg->msg_state == HTTP_MSG_ENDING)
6223 goto ending;
6224
6225 /* Neither content-length, nor transfer-encoding was found, so we must
6226 * read the body until the server connection is closed. In that case, we
6227 * eat data as they come. Of course, this happens for response only. */
6228 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6229 unsigned long long len = (chn->buf->i - msg->next);
6230 msg->chunk_len += len;
6231 msg->body_len += len;
6232 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006233 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6234 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6235 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006236 msg->next += ret;
6237 msg->chunk_len -= ret;
6238 if (msg->chunk_len) {
6239 /* input empty or output full */
6240 if (chn->buf->i > msg->next)
6241 chn->flags |= CF_WAKE_WRITE;
6242 goto missing_data_or_waiting;
6243 }
6244
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006245 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6246 * always set for a request. */
6247 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6248 /* The server still sending data that should be filtered */
6249 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6250 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006251 msg->msg_state = HTTP_MSG_TUNNEL;
6252 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006253 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006254
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006255 msg->msg_state = HTTP_MSG_ENDING;
6256
6257 ending:
6258 /* we may have some pending data starting at res->buf->p such as a last
6259 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006260 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6261 /* default_ret */ msg->next,
6262 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006263 b_adv(chn->buf, ret);
6264 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006265 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6266 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006267 if (msg->next)
6268 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006269
Christopher Fauletda02e172015-12-04 09:25:05 +01006270 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6271 /* default_ret */ 1,
6272 /* on_error */ goto error,
6273 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006274 if (msg->msg_state == HTTP_MSG_ENDING)
6275 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006276 return 1;
6277
6278 missing_data_or_waiting:
6279 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006280 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6281 /* default_ret */ msg->next,
6282 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006283 b_adv(chn->buf, ret);
6284 msg->next -= ret;
6285 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6286 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006287 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006288 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006289 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006290 return 0;
6291 error:
6292 return -1;
6293}
6294
6295static inline int
6296http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6297{
6298 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006299 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006300 int ret;
6301
6302 /* Here we have the guarantee to be in one of the following state:
6303 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6304 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6305
6306 switch_states:
6307 switch (msg->msg_state) {
6308 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006309 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6310 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6311 /* on_error */ goto error);
6312 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006313 msg->chunk_len -= ret;
6314 if (msg->chunk_len) {
6315 /* input empty or output full */
6316 if (chn->buf->i > msg->next)
6317 chn->flags |= CF_WAKE_WRITE;
6318 goto missing_data_or_waiting;
6319 }
6320
6321 /* nothing left to forward for this chunk*/
6322 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6323 /* fall through for HTTP_MSG_CHUNK_CRLF */
6324
6325 case HTTP_MSG_CHUNK_CRLF:
6326 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006327 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006328 if (ret == 0)
6329 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006330 if (ret < 0) {
6331 msg->err_pos = chn->buf->i + ret;
6332 if (msg->err_pos < 0)
6333 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006334 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006335 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006336 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006337 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6338 /* fall through for HTTP_MSG_CHUNK_SIZE */
6339
6340 case HTTP_MSG_CHUNK_SIZE:
6341 /* read the chunk size and assign it to ->chunk_len,
6342 * then set ->next to point to the body and switch to
6343 * DATA or TRAILERS state.
6344 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006345 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006346 if (ret == 0)
6347 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006348 if (ret < 0) {
6349 msg->err_pos = chn->buf->i + ret;
6350 if (msg->err_pos < 0)
6351 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006352 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006353 }
6354
6355 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006356 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006357 msg->chunk_len = chunk;
6358 msg->body_len += chunk;
6359
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006360 if (msg->chunk_len) {
6361 msg->msg_state = HTTP_MSG_DATA;
6362 goto switch_states;
6363 }
6364 msg->msg_state = HTTP_MSG_TRAILERS;
6365 /* fall through for HTTP_MSG_TRAILERS */
6366
6367 case HTTP_MSG_TRAILERS:
6368 ret = http_forward_trailers(msg);
6369 if (ret < 0)
6370 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006371 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6372 /* default_ret */ 1,
6373 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006374 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006375 if (!ret)
6376 goto missing_data_or_waiting;
6377 break;
6378
6379 case HTTP_MSG_ENDING:
6380 goto ending;
6381
6382 default:
6383 /* This should no happen in this function */
6384 goto error;
6385 }
6386
6387 msg->msg_state = HTTP_MSG_ENDING;
6388 ending:
6389 /* we may have some pending data starting at res->buf->p such as a last
6390 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006391 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006392 /* default_ret */ msg->next,
6393 /* on_error */ goto error);
6394 b_adv(chn->buf, ret);
6395 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006396 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6397 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006398 if (msg->next)
6399 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006400
Christopher Fauletda02e172015-12-04 09:25:05 +01006401 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006402 /* default_ret */ 1,
6403 /* on_error */ goto error,
6404 /* on_wait */ goto waiting);
6405 msg->msg_state = HTTP_MSG_DONE;
6406 return 1;
6407
6408 missing_data_or_waiting:
6409 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006410 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006411 /* default_ret */ msg->next,
6412 /* on_error */ goto error);
6413 b_adv(chn->buf, ret);
6414 msg->next -= ret;
6415 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6416 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006417 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006418 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006419 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006420 return 0;
6421
6422 chunk_parsing_error:
6423 if (msg->err_pos >= 0) {
6424 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006425 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006426 msg->msg_state, strm_fe(s));
6427 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006428 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006429 msg, msg->msg_state, s->be);
6430 }
6431 error:
6432 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006433}
6434
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006436/* Iterate the same filter through all request headers.
6437 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006438 * Since it can manage the switch to another backend, it updates the per-proxy
6439 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006440 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006441int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006442{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006443 char *cur_ptr, *cur_end, *cur_next;
6444 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006445 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006446 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006447 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 last_hdr = 0;
6450
Willy Tarreau9b28e032012-10-12 23:49:43 +02006451 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006452 old_idx = 0;
6453
6454 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006455 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006457 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006458 (exp->action == ACT_ALLOW ||
6459 exp->action == ACT_DENY ||
6460 exp->action == ACT_TARPIT))
6461 return 0;
6462
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006463 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 if (!cur_idx)
6465 break;
6466
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006467 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 cur_ptr = cur_next;
6469 cur_end = cur_ptr + cur_hdr->len;
6470 cur_next = cur_end + cur_hdr->cr + 1;
6471
6472 /* Now we have one header between cur_ptr and cur_end,
6473 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006474 */
6475
Willy Tarreau15a53a42015-01-21 13:39:42 +01006476 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 last_hdr = 1;
6481 break;
6482
6483 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006484 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 break;
6487
6488 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006489 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491 break;
6492
6493 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006494 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6495 if (trash.len < 0)
6496 return -1;
6497
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006498 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006499 /* FIXME: if the user adds a newline in the replacement, the
6500 * index will not be recalculated for now, and the new line
6501 * will not be counted as a new header.
6502 */
6503
6504 cur_end += delta;
6505 cur_next += delta;
6506 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006507 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006508 break;
6509
6510 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006511 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 cur_next += delta;
6513
Willy Tarreaufa355d42009-11-29 18:12:29 +01006514 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006515 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6516 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006517 cur_hdr->len = 0;
6518 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006519 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 break;
6521
6522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 }
6524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 /* keep the link from this header to next one in case of later
6526 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006527 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528 old_idx = cur_idx;
6529 }
6530 return 0;
6531}
6532
6533
6534/* Apply the filter to the request line.
6535 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6536 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 * Since it can manage the switch to another backend, it updates the per-proxy
6538 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006540int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006542 char *cur_ptr, *cur_end;
6543 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006544 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006545 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006546
Willy Tarreau3d300592007-03-18 18:34:41 +01006547 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006549 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 (exp->action == ACT_ALLOW ||
6551 exp->action == ACT_DENY ||
6552 exp->action == ACT_TARPIT))
6553 return 0;
6554 else if (exp->action == ACT_REMOVE)
6555 return 0;
6556
6557 done = 0;
6558
Willy Tarreau9b28e032012-10-12 23:49:43 +02006559 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006560 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561
6562 /* Now we have the request line between cur_ptr and cur_end */
6563
Willy Tarreau15a53a42015-01-21 13:39:42 +01006564 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006565 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006567 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 done = 1;
6569 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006572 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006573 done = 1;
6574 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006576 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006577 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006578 done = 1;
6579 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006581 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006582 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6583 if (trash.len < 0)
6584 return -1;
6585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006586 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006587 /* FIXME: if the user adds a newline in the replacement, the
6588 * index will not be recalculated for now, and the new line
6589 * will not be counted as a new header.
6590 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006591
Willy Tarreaufa355d42009-11-29 18:12:29 +01006592 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006593 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006594 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 HTTP_MSG_RQMETH,
6596 cur_ptr, cur_end + 1,
6597 NULL, NULL);
6598 if (unlikely(!cur_end))
6599 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006601 /* we have a full request and we know that we have either a CR
6602 * or an LF at <ptr>.
6603 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006604 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6605 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006606 /* there is no point trying this regex on headers */
6607 return 1;
6608 }
6609 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006610 return done;
6611}
Willy Tarreau97de6242006-12-27 17:18:38 +01006612
Willy Tarreau58f10d72006-12-04 02:26:12 +01006613
Willy Tarreau58f10d72006-12-04 02:26:12 +01006614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006615/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006616 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006617 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618 * unparsable request. Since it can manage the switch to another backend, it
6619 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006620 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006621int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006622{
Willy Tarreau192252e2015-04-04 01:47:55 +02006623 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006624 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006625 struct hdr_exp *exp;
6626
6627 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006628 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006630 /*
6631 * The interleaving of transformations and verdicts
6632 * makes it difficult to decide to continue or stop
6633 * the evaluation.
6634 */
6635
Willy Tarreau6c123b12010-01-28 20:22:06 +01006636 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6637 break;
6638
Willy Tarreau3d300592007-03-18 18:34:41 +01006639 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006640 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006641 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006642 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006643
6644 /* if this filter had a condition, evaluate it now and skip to
6645 * next filter if the condition does not match.
6646 */
6647 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006648 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006649 ret = acl_pass(ret);
6650 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6651 ret = !ret;
6652
6653 if (!ret)
6654 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006655 }
6656
6657 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006658 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 if (unlikely(ret < 0))
6660 return -1;
6661
6662 if (likely(ret == 0)) {
6663 /* The filter did not match the request, it can be
6664 * iterated through all headers.
6665 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006666 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6667 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006668 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006669 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006670 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006671}
6672
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006673
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006674/* Find the end of a cookie value contained between <s> and <e>. It works the
6675 * same way as with headers above except that the semi-colon also ends a token.
6676 * See RFC2965 for more information. Note that it requires a valid header to
6677 * return a valid result.
6678 */
6679char *find_cookie_value_end(char *s, const char *e)
6680{
6681 int quoted, qdpair;
6682
6683 quoted = qdpair = 0;
6684 for (; s < e; s++) {
6685 if (qdpair) qdpair = 0;
6686 else if (quoted) {
6687 if (*s == '\\') qdpair = 1;
6688 else if (*s == '"') quoted = 0;
6689 }
6690 else if (*s == '"') quoted = 1;
6691 else if (*s == ',' || *s == ';') return s;
6692 }
6693 return s;
6694}
6695
6696/* Delete a value in a header between delimiters <from> and <next> in buffer
6697 * <buf>. The number of characters displaced is returned, and the pointer to
6698 * the first delimiter is updated if required. The function tries as much as
6699 * possible to respect the following principles :
6700 * - replace <from> delimiter by the <next> one unless <from> points to a
6701 * colon, in which case <next> is simply removed
6702 * - set exactly one space character after the new first delimiter, unless
6703 * there are not enough characters in the block being moved to do so.
6704 * - remove unneeded spaces before the previous delimiter and after the new
6705 * one.
6706 *
6707 * It is the caller's responsibility to ensure that :
6708 * - <from> points to a valid delimiter or the colon ;
6709 * - <next> points to a valid delimiter or the final CR/LF ;
6710 * - there are non-space chars before <from> ;
6711 * - there is a CR/LF at or after <next>.
6712 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006713int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006714{
6715 char *prev = *from;
6716
6717 if (*prev == ':') {
6718 /* We're removing the first value, preserve the colon and add a
6719 * space if possible.
6720 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006721 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006722 next++;
6723 prev++;
6724 if (prev < next)
6725 *prev++ = ' ';
6726
Willy Tarreau2235b262016-11-05 15:50:20 +01006727 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 next++;
6729 } else {
6730 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006731 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 prev--;
6733 *from = prev;
6734
6735 /* copy the delimiter and if possible a space if we're
6736 * not at the end of the line.
6737 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006738 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006739 *prev++ = *next++;
6740 if (prev + 1 < next)
6741 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006742 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 next++;
6744 }
6745 }
6746 return buffer_replace2(buf, prev, next, NULL, 0);
6747}
6748
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006749/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006750 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006751 * desirable to call it only when needed. This code is quite complex because
6752 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6753 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006755void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006757 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006758 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006759 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006760 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6762 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006763
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006765 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006766 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006768 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006769 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006770 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006771
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006772 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006773 hdr_beg = hdr_next;
6774 hdr_end = hdr_beg + cur_hdr->len;
6775 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006776
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006777 /* We have one full header between hdr_beg and hdr_end, and the
6778 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006779 * "Cookie:" headers.
6780 */
6781
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006782 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006783 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006784 old_idx = cur_idx;
6785 continue;
6786 }
6787
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006788 del_from = NULL; /* nothing to be deleted */
6789 preserve_hdr = 0; /* assume we may kill the whole header */
6790
Willy Tarreau58f10d72006-12-04 02:26:12 +01006791 /* Now look for cookies. Conforming to RFC2109, we have to support
6792 * attributes whose name begin with a '$', and associate them with
6793 * the right cookie, if we want to delete this cookie.
6794 * So there are 3 cases for each cookie read :
6795 * 1) it's a special attribute, beginning with a '$' : ignore it.
6796 * 2) it's a server id cookie that we *MAY* want to delete : save
6797 * some pointers on it (last semi-colon, beginning of cookie...)
6798 * 3) it's an application cookie : we *MAY* have to delete a previous
6799 * "special" cookie.
6800 * At the end of loop, if a "special" cookie remains, we may have to
6801 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006802 * *MUST* delete it.
6803 *
6804 * Note: RFC2965 is unclear about the processing of spaces around
6805 * the equal sign in the ATTR=VALUE form. A careful inspection of
6806 * the RFC explicitly allows spaces before it, and not within the
6807 * tokens (attrs or values). An inspection of RFC2109 allows that
6808 * too but section 10.1.3 lets one think that spaces may be allowed
6809 * after the equal sign too, resulting in some (rare) buggy
6810 * implementations trying to do that. So let's do what servers do.
6811 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6812 * allowed quoted strings in values, with any possible character
6813 * after a backslash, including control chars and delimitors, which
6814 * causes parsing to become ambiguous. Browsers also allow spaces
6815 * within values even without quotes.
6816 *
6817 * We have to keep multiple pointers in order to support cookie
6818 * removal at the beginning, middle or end of header without
6819 * corrupting the header. All of these headers are valid :
6820 *
6821 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6822 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6823 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6824 * | | | | | | | | |
6825 * | | | | | | | | hdr_end <--+
6826 * | | | | | | | +--> next
6827 * | | | | | | +----> val_end
6828 * | | | | | +-----------> val_beg
6829 * | | | | +--------------> equal
6830 * | | | +----------------> att_end
6831 * | | +---------------------> att_beg
6832 * | +--------------------------> prev
6833 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006834 */
6835
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006836 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6837 /* Iterate through all cookies on this line */
6838
6839 /* find att_beg */
6840 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006841 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006842 att_beg++;
6843
6844 /* find att_end : this is the first character after the last non
6845 * space before the equal. It may be equal to hdr_end.
6846 */
6847 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006848
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006849 while (equal < hdr_end) {
6850 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006851 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006852 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006853 continue;
6854 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855 }
6856
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006857 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6858 * is between <att_beg> and <equal>, both may be identical.
6859 */
6860
6861 /* look for end of cookie if there is an equal sign */
6862 if (equal < hdr_end && *equal == '=') {
6863 /* look for the beginning of the value */
6864 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006865 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006866 val_beg++;
6867
6868 /* find the end of the value, respecting quotes */
6869 next = find_cookie_value_end(val_beg, hdr_end);
6870
6871 /* make val_end point to the first white space or delimitor after the value */
6872 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006873 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006874 val_end--;
6875 } else {
6876 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006877 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006878
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006879 /* We have nothing to do with attributes beginning with '$'. However,
6880 * they will automatically be removed if a header before them is removed,
6881 * since they're supposed to be linked together.
6882 */
6883 if (*att_beg == '$')
6884 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006885
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 /* Ignore cookies with no equal sign */
6887 if (equal == next) {
6888 /* This is not our cookie, so we must preserve it. But if we already
6889 * scheduled another cookie for removal, we cannot remove the
6890 * complete header, but we can remove the previous block itself.
6891 */
6892 preserve_hdr = 1;
6893 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006894 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 val_end += delta;
6896 next += delta;
6897 hdr_end += delta;
6898 hdr_next += delta;
6899 cur_hdr->len += delta;
6900 http_msg_move_end(&txn->req, delta);
6901 prev = del_from;
6902 del_from = NULL;
6903 }
6904 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 /* if there are spaces around the equal sign, we need to
6908 * strip them otherwise we'll get trouble for cookie captures,
6909 * or even for rewrites. Since this happens extremely rarely,
6910 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006911 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006912 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6913 int stripped_before = 0;
6914 int stripped_after = 0;
6915
6916 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006917 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006918 equal += stripped_before;
6919 val_beg += stripped_before;
6920 }
6921
6922 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006923 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 val_beg += stripped_after;
6925 stripped_before += stripped_after;
6926 }
6927
6928 val_end += stripped_before;
6929 next += stripped_before;
6930 hdr_end += stripped_before;
6931 hdr_next += stripped_before;
6932 cur_hdr->len += stripped_before;
6933 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006934 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006935 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 /* First, let's see if we want to capture this cookie. We check
6938 * that we don't already have a client side cookie, because we
6939 * can only capture one. Also as an optimisation, we ignore
6940 * cookies shorter than the declared name.
6941 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006942 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6943 (val_end - att_beg >= sess->fe->capture_namelen) &&
6944 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006945 int log_len = val_end - att_beg;
6946
Willy Tarreaubafbe012017-11-24 17:34:44 +01006947 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006948 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006949 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006950 if (log_len > sess->fe->capture_len)
6951 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 memcpy(txn->cli_cookie, att_beg, log_len);
6953 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006954 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006955 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006956
Willy Tarreaubca99692010-10-06 19:25:55 +02006957 /* Persistence cookies in passive, rewrite or insert mode have the
6958 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006959 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006960 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006961 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006962 * For cookies in prefix mode, the form is :
6963 *
6964 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006965 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006966 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6967 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6968 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006969 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006970
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006971 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6972 * have the server ID between val_beg and delim, and the original cookie between
6973 * delim+1 and val_end. Otherwise, delim==val_end :
6974 *
6975 * Cookie: NAME=SRV; # in all but prefix modes
6976 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6977 * | || || | |+-> next
6978 * | || || | +--> val_end
6979 * | || || +---------> delim
6980 * | || |+------------> val_beg
6981 * | || +-------------> att_end = equal
6982 * | |+-----------------> att_beg
6983 * | +------------------> prev
6984 * +-------------------------> hdr_beg
6985 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006986
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006987 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006988 for (delim = val_beg; delim < val_end; delim++)
6989 if (*delim == COOKIE_DELIM)
6990 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006991 } else {
6992 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006993 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006994 /* Now check if the cookie contains a date field, which would
6995 * appear after a vertical bar ('|') just after the server name
6996 * and before the delimiter.
6997 */
6998 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6999 if (vbar1) {
7000 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007001 * right is the last seen date. It is a base64 encoded
7002 * 30-bit value representing the UNIX date since the
7003 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007004 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007005 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007006 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007007 if (val_end - vbar1 >= 5) {
7008 val = b64tos30(vbar1);
7009 if (val > 0)
7010 txn->cookie_last_date = val << 2;
7011 }
7012 /* look for a second vertical bar */
7013 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7014 if (vbar1 && (val_end - vbar1 > 5)) {
7015 val = b64tos30(vbar1 + 1);
7016 if (val > 0)
7017 txn->cookie_first_date = val << 2;
7018 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007019 }
7020 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007021
Willy Tarreauf64d1412010-10-07 20:06:11 +02007022 /* if the cookie has an expiration date and the proxy wants to check
7023 * it, then we do that now. We first check if the cookie is too old,
7024 * then only if it has expired. We detect strict overflow because the
7025 * time resolution here is not great (4 seconds). Cookies with dates
7026 * in the future are ignored if their offset is beyond one day. This
7027 * allows an admin to fix timezone issues without expiring everyone
7028 * and at the same time avoids keeping unwanted side effects for too
7029 * long.
7030 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007031 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7032 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007033 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007034 txn->flags &= ~TX_CK_MASK;
7035 txn->flags |= TX_CK_OLD;
7036 delim = val_beg; // let's pretend we have not found the cookie
7037 txn->cookie_first_date = 0;
7038 txn->cookie_last_date = 0;
7039 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007040 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7041 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007042 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007043 txn->flags &= ~TX_CK_MASK;
7044 txn->flags |= TX_CK_EXPIRED;
7045 delim = val_beg; // let's pretend we have not found the cookie
7046 txn->cookie_first_date = 0;
7047 txn->cookie_last_date = 0;
7048 }
7049
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007050 /* Here, we'll look for the first running server which supports the cookie.
7051 * This allows to share a same cookie between several servers, for example
7052 * to dedicate backup servers to specific servers only.
7053 * However, to prevent clients from sticking to cookie-less backup server
7054 * when they have incidentely learned an empty cookie, we simply ignore
7055 * empty cookies and mark them as invalid.
7056 * The same behaviour is applied when persistence must be ignored.
7057 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007058 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007059 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007060
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007061 while (srv) {
7062 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7063 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007064 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007065 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007066 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007067 /* we found the server and we can use it */
7068 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007069 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007070 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007071 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 break;
7073 } else {
7074 /* we found a server, but it's down,
7075 * mark it as such and go on in case
7076 * another one is available.
7077 */
7078 txn->flags &= ~TX_CK_MASK;
7079 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007082 srv = srv->next;
7083 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007084
Willy Tarreauf64d1412010-10-07 20:06:11 +02007085 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007086 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007088 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007089 txn->flags |= TX_CK_UNUSED;
7090 else
7091 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007094 /* depending on the cookie mode, we may have to either :
7095 * - delete the complete cookie if we're in insert+indirect mode, so that
7096 * the server never sees it ;
7097 * - remove the server id from the cookie value, and tag the cookie as an
7098 * application cookie so that it does not get accidentely removed later,
7099 * if we're in cookie prefix mode
7100 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007101 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007102 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103
Willy Tarreau9b28e032012-10-12 23:49:43 +02007104 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007105 val_end += delta;
7106 next += delta;
7107 hdr_end += delta;
7108 hdr_next += delta;
7109 cur_hdr->len += delta;
7110 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007111
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007112 del_from = NULL;
7113 preserve_hdr = 1; /* we want to keep this cookie */
7114 }
7115 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007116 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007117 del_from = prev;
7118 }
7119 } else {
7120 /* This is not our cookie, so we must preserve it. But if we already
7121 * scheduled another cookie for removal, we cannot remove the
7122 * complete header, but we can remove the previous block itself.
7123 */
7124 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007125
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007126 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007127 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007128 if (att_beg >= del_from)
7129 att_beg += delta;
7130 if (att_end >= del_from)
7131 att_end += delta;
7132 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 val_end += delta;
7134 next += delta;
7135 hdr_end += delta;
7136 hdr_next += delta;
7137 cur_hdr->len += delta;
7138 http_msg_move_end(&txn->req, delta);
7139 prev = del_from;
7140 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007141 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007143
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007144 /* continue with next cookie on this header line */
7145 att_beg = next;
7146 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007148 /* There are no more cookies on this line.
7149 * We may still have one (or several) marked for deletion at the
7150 * end of the line. We must do this now in two ways :
7151 * - if some cookies must be preserved, we only delete from the
7152 * mark to the end of line ;
7153 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007155 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007156 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007157 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007158 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007159 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160 cur_hdr->len += delta;
7161 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007162 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163
7164 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007165 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7166 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007167 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007168 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007169 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007170 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007171 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172 }
7173
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007174 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007175 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007176 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177}
7178
7179
Willy Tarreaua15645d2007-03-18 16:22:39 +01007180/* Iterate the same filter through all response headers contained in <rtr>.
7181 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7182 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007183int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007184{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 char *cur_ptr, *cur_end, *cur_next;
7186 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007187 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007189 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190
7191 last_hdr = 0;
7192
Willy Tarreau9b28e032012-10-12 23:49:43 +02007193 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 old_idx = 0;
7195
7196 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007197 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007199 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 (exp->action == ACT_ALLOW ||
7201 exp->action == ACT_DENY))
7202 return 0;
7203
7204 cur_idx = txn->hdr_idx.v[old_idx].next;
7205 if (!cur_idx)
7206 break;
7207
7208 cur_hdr = &txn->hdr_idx.v[cur_idx];
7209 cur_ptr = cur_next;
7210 cur_end = cur_ptr + cur_hdr->len;
7211 cur_next = cur_end + cur_hdr->cr + 1;
7212
7213 /* Now we have one header between cur_ptr and cur_end,
7214 * and the next header starts at cur_next.
7215 */
7216
Willy Tarreau15a53a42015-01-21 13:39:42 +01007217 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218 switch (exp->action) {
7219 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007220 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221 last_hdr = 1;
7222 break;
7223
7224 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007225 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226 last_hdr = 1;
7227 break;
7228
7229 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007230 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7231 if (trash.len < 0)
7232 return -1;
7233
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007234 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 /* FIXME: if the user adds a newline in the replacement, the
7236 * index will not be recalculated for now, and the new line
7237 * will not be counted as a new header.
7238 */
7239
7240 cur_end += delta;
7241 cur_next += delta;
7242 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007243 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244 break;
7245
7246 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007247 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248 cur_next += delta;
7249
Willy Tarreaufa355d42009-11-29 18:12:29 +01007250 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7252 txn->hdr_idx.used--;
7253 cur_hdr->len = 0;
7254 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007255 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256 break;
7257
7258 }
7259 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260
7261 /* keep the link from this header to next one in case of later
7262 * removal of next header.
7263 */
7264 old_idx = cur_idx;
7265 }
7266 return 0;
7267}
7268
7269
7270/* Apply the filter to the status line in the response buffer <rtr>.
7271 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7272 * or -1 if a replacement resulted in an invalid status line.
7273 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007274int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 char *cur_ptr, *cur_end;
7277 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007278 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007279 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280
7281
Willy Tarreau3d300592007-03-18 18:34:41 +01007282 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007284 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 (exp->action == ACT_ALLOW ||
7286 exp->action == ACT_DENY))
7287 return 0;
7288 else if (exp->action == ACT_REMOVE)
7289 return 0;
7290
7291 done = 0;
7292
Willy Tarreau9b28e032012-10-12 23:49:43 +02007293 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007294 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295
7296 /* Now we have the status line between cur_ptr and cur_end */
7297
Willy Tarreau15a53a42015-01-21 13:39:42 +01007298 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 switch (exp->action) {
7300 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007301 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302 done = 1;
7303 break;
7304
7305 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007306 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 done = 1;
7308 break;
7309
7310 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007311 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7312 if (trash.len < 0)
7313 return -1;
7314
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007315 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007316 /* FIXME: if the user adds a newline in the replacement, the
7317 * index will not be recalculated for now, and the new line
7318 * will not be counted as a new header.
7319 */
7320
Willy Tarreaufa355d42009-11-29 18:12:29 +01007321 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007322 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007323 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007324 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007325 cur_ptr, cur_end + 1,
7326 NULL, NULL);
7327 if (unlikely(!cur_end))
7328 return -1;
7329
7330 /* we have a full respnse and we know that we have either a CR
7331 * or an LF at <ptr>.
7332 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007333 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007334 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335 /* there is no point trying this regex on headers */
7336 return 1;
7337 }
7338 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339 return done;
7340}
7341
7342
7343
7344/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007345 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007346 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7347 * unparsable response.
7348 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007349int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350{
Willy Tarreau192252e2015-04-04 01:47:55 +02007351 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007352 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007353 struct hdr_exp *exp;
7354
7355 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 int ret;
7357
7358 /*
7359 * The interleaving of transformations and verdicts
7360 * makes it difficult to decide to continue or stop
7361 * the evaluation.
7362 */
7363
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007364 if (txn->flags & TX_SVDENY)
7365 break;
7366
Willy Tarreau3d300592007-03-18 18:34:41 +01007367 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7369 exp->action == ACT_PASS)) {
7370 exp = exp->next;
7371 continue;
7372 }
7373
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007374 /* if this filter had a condition, evaluate it now and skip to
7375 * next filter if the condition does not match.
7376 */
7377 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007378 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007379 ret = acl_pass(ret);
7380 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7381 ret = !ret;
7382 if (!ret)
7383 continue;
7384 }
7385
Willy Tarreaua15645d2007-03-18 16:22:39 +01007386 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007387 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 if (unlikely(ret < 0))
7389 return -1;
7390
7391 if (likely(ret == 0)) {
7392 /* The filter did not match the response, it can be
7393 * iterated through all headers.
7394 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007395 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7396 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007397 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398 }
7399 return 0;
7400}
7401
7402
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007404 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007405 * desirable to call it only when needed. This function is also used when we
7406 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007408void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007410 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007411 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007412 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007413 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007415 char *hdr_beg, *hdr_end, *hdr_next;
7416 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 /* Iterate through the headers.
7419 * we start with the start line.
7420 */
7421 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007422 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423
7424 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7425 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007426 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427
7428 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007429 hdr_beg = hdr_next;
7430 hdr_end = hdr_beg + cur_hdr->len;
7431 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007432
Willy Tarreau24581ba2010-08-31 22:39:35 +02007433 /* We have one full header between hdr_beg and hdr_end, and the
7434 * next header starts at hdr_next. We're only interested in
7435 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007436 */
7437
Willy Tarreau24581ba2010-08-31 22:39:35 +02007438 is_cookie2 = 0;
7439 prev = hdr_beg + 10;
7440 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007441 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007442 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7443 if (!val) {
7444 old_idx = cur_idx;
7445 continue;
7446 }
7447 is_cookie2 = 1;
7448 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007449 }
7450
Willy Tarreau24581ba2010-08-31 22:39:35 +02007451 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7452 * <prev> points to the colon.
7453 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007454 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7457 * check-cache is enabled) and we are not interested in checking
7458 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007459 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007460 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007461 return;
7462
Willy Tarreau24581ba2010-08-31 22:39:35 +02007463 /* OK so now we know we have to process this response cookie.
7464 * The format of the Set-Cookie header is slightly different
7465 * from the format of the Cookie header in that it does not
7466 * support the comma as a cookie delimiter (thus the header
7467 * cannot be folded) because the Expires attribute described in
7468 * the original Netscape's spec may contain an unquoted date
7469 * with a comma inside. We have to live with this because
7470 * many browsers don't support Max-Age and some browsers don't
7471 * support quoted strings. However the Set-Cookie2 header is
7472 * clean.
7473 *
7474 * We have to keep multiple pointers in order to support cookie
7475 * removal at the beginning, middle or end of header without
7476 * corrupting the header (in case of set-cookie2). A special
7477 * pointer, <scav> points to the beginning of the set-cookie-av
7478 * fields after the first semi-colon. The <next> pointer points
7479 * either to the end of line (set-cookie) or next unquoted comma
7480 * (set-cookie2). All of these headers are valid :
7481 *
7482 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7483 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7484 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7485 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7486 * | | | | | | | | | |
7487 * | | | | | | | | +-> next hdr_end <--+
7488 * | | | | | | | +------------> scav
7489 * | | | | | | +--------------> val_end
7490 * | | | | | +--------------------> val_beg
7491 * | | | | +----------------------> equal
7492 * | | | +------------------------> att_end
7493 * | | +----------------------------> att_beg
7494 * | +------------------------------> prev
7495 * +-----------------------------------------> hdr_beg
7496 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007497
Willy Tarreau24581ba2010-08-31 22:39:35 +02007498 for (; prev < hdr_end; prev = next) {
7499 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007500
Willy Tarreau24581ba2010-08-31 22:39:35 +02007501 /* find att_beg */
7502 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007503 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007504 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007505
Willy Tarreau24581ba2010-08-31 22:39:35 +02007506 /* find att_end : this is the first character after the last non
7507 * space before the equal. It may be equal to hdr_end.
7508 */
7509 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510
Willy Tarreau24581ba2010-08-31 22:39:35 +02007511 while (equal < hdr_end) {
7512 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7513 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007514 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 continue;
7516 att_end = equal;
7517 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518
Willy Tarreau24581ba2010-08-31 22:39:35 +02007519 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7520 * is between <att_beg> and <equal>, both may be identical.
7521 */
7522
7523 /* look for end of cookie if there is an equal sign */
7524 if (equal < hdr_end && *equal == '=') {
7525 /* look for the beginning of the value */
7526 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007527 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007528 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007529
Willy Tarreau24581ba2010-08-31 22:39:35 +02007530 /* find the end of the value, respecting quotes */
7531 next = find_cookie_value_end(val_beg, hdr_end);
7532
7533 /* make val_end point to the first white space or delimitor after the value */
7534 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007535 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007536 val_end--;
7537 } else {
7538 /* <equal> points to next comma, semi-colon or EOL */
7539 val_beg = val_end = next = equal;
7540 }
7541
7542 if (next < hdr_end) {
7543 /* Set-Cookie2 supports multiple cookies, and <next> points to
7544 * a colon or semi-colon before the end. So skip all attr-value
7545 * pairs and look for the next comma. For Set-Cookie, since
7546 * commas are permitted in values, skip to the end.
7547 */
7548 if (is_cookie2)
7549 next = find_hdr_value_end(next, hdr_end);
7550 else
7551 next = hdr_end;
7552 }
7553
7554 /* Now everything is as on the diagram above */
7555
7556 /* Ignore cookies with no equal sign */
7557 if (equal == val_end)
7558 continue;
7559
7560 /* If there are spaces around the equal sign, we need to
7561 * strip them otherwise we'll get trouble for cookie captures,
7562 * or even for rewrites. Since this happens extremely rarely,
7563 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007565 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7566 int stripped_before = 0;
7567 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007568
Willy Tarreau24581ba2010-08-31 22:39:35 +02007569 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007570 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007571 equal += stripped_before;
7572 val_beg += stripped_before;
7573 }
7574
7575 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007576 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 val_beg += stripped_after;
7578 stripped_before += stripped_after;
7579 }
7580
7581 val_end += stripped_before;
7582 next += stripped_before;
7583 hdr_end += stripped_before;
7584 hdr_next += stripped_before;
7585 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007586 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007587 }
7588
7589 /* First, let's see if we want to capture this cookie. We check
7590 * that we don't already have a server side cookie, because we
7591 * can only capture one. Also as an optimisation, we ignore
7592 * cookies shorter than the declared name.
7593 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007594 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007595 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007596 (val_end - att_beg >= sess->fe->capture_namelen) &&
7597 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007598 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007599 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007600 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007602 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007603 if (log_len > sess->fe->capture_len)
7604 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007605 memcpy(txn->srv_cookie, att_beg, log_len);
7606 txn->srv_cookie[log_len] = 0;
7607 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608 }
7609
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007610 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007612 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007613 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7614 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007615 /* assume passive cookie by default */
7616 txn->flags &= ~TX_SCK_MASK;
7617 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007618
7619 /* If the cookie is in insert mode on a known server, we'll delete
7620 * this occurrence because we'll insert another one later.
7621 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007622 * a direct access.
7623 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007624 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007625 /* The "preserve" flag was set, we don't want to touch the
7626 * server's cookie.
7627 */
7628 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007629 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007630 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007631 /* this cookie must be deleted */
7632 if (*prev == ':' && next == hdr_end) {
7633 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007634 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007635 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7636 txn->hdr_idx.used--;
7637 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007638 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007639 hdr_next += delta;
7640 http_msg_move_end(&txn->rsp, delta);
7641 /* note: while both invalid now, <next> and <hdr_end>
7642 * are still equal, so the for() will stop as expected.
7643 */
7644 } else {
7645 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007646 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007647 next = prev;
7648 hdr_end += delta;
7649 hdr_next += delta;
7650 cur_hdr->len += delta;
7651 http_msg_move_end(&txn->rsp, delta);
7652 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007653 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007654 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007655 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007657 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007658 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 * with this server since we know it.
7660 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007661 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007662 next += delta;
7663 hdr_end += delta;
7664 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007666 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007667
Willy Tarreauf1348312010-10-07 15:54:11 +02007668 txn->flags &= ~TX_SCK_MASK;
7669 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007671 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007672 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007673 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007675 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007676 next += delta;
7677 hdr_end += delta;
7678 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007680 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681
Willy Tarreau827aee92011-03-10 16:55:02 +01007682 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007683 txn->flags &= ~TX_SCK_MASK;
7684 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685 }
7686 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007687 /* that's done for this cookie, check the next one on the same
7688 * line when next != hdr_end (only if is_cookie2).
7689 */
7690 }
7691 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007693 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694}
7695
7696
Willy Tarreaua15645d2007-03-18 16:22:39 +01007697/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007698 * Parses the Cache-Control and Pragma request header fields to determine if
7699 * the request may be served from the cache and/or if it is cacheable. Updates
7700 * s->txn->flags.
7701 */
7702void check_request_for_cacheability(struct stream *s, struct channel *chn)
7703{
7704 struct http_txn *txn = s->txn;
7705 char *p1, *p2;
7706 char *cur_ptr, *cur_end, *cur_next;
7707 int pragma_found;
7708 int cc_found;
7709 int cur_idx;
7710
7711 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7712 return; /* nothing more to do here */
7713
7714 cur_idx = 0;
7715 pragma_found = cc_found = 0;
7716 cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
7717
7718 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7719 struct hdr_idx_elem *cur_hdr;
7720 int val;
7721
7722 cur_hdr = &txn->hdr_idx.v[cur_idx];
7723 cur_ptr = cur_next;
7724 cur_end = cur_ptr + cur_hdr->len;
7725 cur_next = cur_end + cur_hdr->cr + 1;
7726
7727 /* We have one full header between cur_ptr and cur_end, and the
7728 * next header starts at cur_next.
7729 */
7730
7731 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7732 if (val) {
7733 if ((cur_end - (cur_ptr + val) >= 8) &&
7734 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7735 pragma_found = 1;
7736 continue;
7737 }
7738 }
7739
William Lallemand8a16fe02018-05-22 11:04:33 +02007740 /* Don't use the cache and don't try to store if we found the
7741 * Authorization header */
7742 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7743 if (val) {
7744 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7745 txn->flags |= TX_CACHE_IGNORE;
7746 continue;
7747 }
7748
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007749 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7750 if (!val)
7751 continue;
7752
7753 /* OK, right now we know we have a cache-control header at cur_ptr */
7754 cc_found = 1;
7755 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7756
7757 if (p1 >= cur_end) /* no more info */
7758 continue;
7759
7760 /* p1 is at the beginning of the value */
7761 p2 = p1;
7762 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7763 p2++;
7764
7765 /* we have a complete value between p1 and p2. We don't check the
7766 * values after max-age, max-stale nor min-fresh, we simply don't
7767 * use the cache when they're specified.
7768 */
7769 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7770 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7771 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7772 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7773 txn->flags |= TX_CACHE_IGNORE;
7774 continue;
7775 }
7776
7777 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7778 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7779 continue;
7780 }
7781 }
7782
7783 /* RFC7234#5.4:
7784 * When the Cache-Control header field is also present and
7785 * understood in a request, Pragma is ignored.
7786 * When the Cache-Control header field is not present in a
7787 * request, caches MUST consider the no-cache request
7788 * pragma-directive as having the same effect as if
7789 * "Cache-Control: no-cache" were present.
7790 */
7791 if (!cc_found && pragma_found)
7792 txn->flags |= TX_CACHE_IGNORE;
7793}
7794
7795/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007796 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007798void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007800 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007801 char *p1, *p2;
7802
7803 char *cur_ptr, *cur_end, *cur_next;
7804 int cur_idx;
7805
Willy Tarreau12b32f22017-12-21 16:08:09 +01007806 if (txn->status < 200) {
7807 /* do not try to cache interim responses! */
7808 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007810 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007811
7812 /* Iterate through the headers.
7813 * we start with the start line.
7814 */
7815 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007816 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817
7818 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7819 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007820 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007821
7822 cur_hdr = &txn->hdr_idx.v[cur_idx];
7823 cur_ptr = cur_next;
7824 cur_end = cur_ptr + cur_hdr->len;
7825 cur_next = cur_end + cur_hdr->cr + 1;
7826
7827 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007828 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 */
7830
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007831 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7832 if (val) {
7833 if ((cur_end - (cur_ptr + val) >= 8) &&
7834 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7835 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7836 return;
7837 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838 }
7839
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007840 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7841 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007842 continue;
7843
7844 /* OK, right now we know we have a cache-control header at cur_ptr */
7845
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007846 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007847
7848 if (p1 >= cur_end) /* no more info */
7849 continue;
7850
7851 /* p1 is at the beginning of the value */
7852 p2 = p1;
7853
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007854 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855 p2++;
7856
7857 /* we have a complete value between p1 and p2 */
7858 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007859 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7860 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7861 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7862 continue;
7863 }
7864
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865 /* we have something of the form no-cache="set-cookie" */
7866 if ((cur_end - p1 >= 21) &&
7867 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7868 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007869 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007870 continue;
7871 }
7872
7873 /* OK, so we know that either p2 points to the end of string or to a comma */
7874 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007875 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007876 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007877 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007878 return;
7879 }
7880
7881 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007882 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 continue;
7884 }
7885 }
7886}
7887
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888
Willy Tarreaub2513902006-12-17 14:52:38 +01007889/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007890 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007891 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007892 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007893 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007894 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007895 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007896 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007897 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007898int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007899{
7900 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007901 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007902 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007903
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007904 if (!uri_auth)
7905 return 0;
7906
Cyril Bonté70be45d2010-10-12 00:14:35 +02007907 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007908 return 0;
7909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007910 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007911 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007912 return 0;
7913
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007914 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007915 return 0;
7916
Willy Tarreaub2513902006-12-17 14:52:38 +01007917 return 1;
7918}
7919
Willy Tarreau4076a152009-04-02 15:18:36 +02007920/*
7921 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007922 * By default it tries to report the error position as msg->err_pos. However if
7923 * this one is not set, it will then report msg->next, which is the last known
7924 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007925 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007926 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007927void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007928 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007929 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007930{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007931 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007932 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007933 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007934
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007935 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007936 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007937 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007938 len1 = MIN(len1, es->len);
7939 len2 = es->len - len1; /* remaining data if buffer wraps */
7940
Willy Tarreauf3764b72016-03-31 13:45:10 +02007941 if (!es->buf)
7942 es->buf = malloc(global.tune.bufsize);
7943
7944 if (es->buf) {
7945 memcpy(es->buf, chn->buf->p, len1);
7946 if (len2)
7947 memcpy(es->buf + len1, chn->buf->data, len2);
7948 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007949
Willy Tarreau4076a152009-04-02 15:18:36 +02007950 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007951 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007952 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007953 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007954
Willy Tarreau4076a152009-04-02 15:18:36 +02007955 es->when = date; // user-visible date
7956 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007957 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007958 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007959 if (objt_conn(sess->origin))
7960 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007961 else
7962 memset(&es->src, 0, sizeof(es->src));
7963
Willy Tarreau078272e2010-12-12 12:46:33 +01007964 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007965 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007966 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007967 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007968 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007969 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007970 es->b_out = chn->buf->o;
7971 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007972 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007973 es->m_clen = msg->chunk_len;
7974 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007975 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007976}
Willy Tarreaub2513902006-12-17 14:52:38 +01007977
Willy Tarreau294c4732011-12-16 21:35:50 +01007978/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7979 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7980 * performed over the whole headers. Otherwise it must contain a valid header
7981 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7982 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7983 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7984 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007985 * -1. The value fetch stops at commas, so this function is suited for use with
7986 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007987 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007988 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007989unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007990 struct hdr_idx *idx, int occ,
7991 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007992{
Willy Tarreau294c4732011-12-16 21:35:50 +01007993 struct hdr_ctx local_ctx;
7994 char *ptr_hist[MAX_HDR_HISTORY];
7995 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007996 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007997 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007998
Willy Tarreau294c4732011-12-16 21:35:50 +01007999 if (!ctx) {
8000 local_ctx.idx = 0;
8001 ctx = &local_ctx;
8002 }
8003
Willy Tarreaubce70882009-09-07 11:51:47 +02008004 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008005 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008006 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008007 occ--;
8008 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008009 *vptr = ctx->line + ctx->val;
8010 *vlen = ctx->vlen;
8011 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008012 }
8013 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008014 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008015 }
8016
8017 /* negative occurrence, we scan all the list then walk back */
8018 if (-occ > MAX_HDR_HISTORY)
8019 return 0;
8020
Willy Tarreau294c4732011-12-16 21:35:50 +01008021 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008022 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008023 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8024 len_hist[hist_ptr] = ctx->vlen;
8025 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008026 hist_ptr = 0;
8027 found++;
8028 }
8029 if (-occ > found)
8030 return 0;
8031 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008032 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8033 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8034 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008035 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008036 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008037 if (hist_ptr >= MAX_HDR_HISTORY)
8038 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008039 *vptr = ptr_hist[hist_ptr];
8040 *vlen = len_hist[hist_ptr];
8041 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008042}
8043
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008044/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8045 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8046 * performed over the whole headers. Otherwise it must contain a valid header
8047 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8048 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8049 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8050 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8051 * -1. This function differs from http_get_hdr() in that it only returns full
8052 * line header values and does not stop at commas.
8053 * The return value is 0 if nothing was found, or non-zero otherwise.
8054 */
8055unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8056 struct hdr_idx *idx, int occ,
8057 struct hdr_ctx *ctx, char **vptr, int *vlen)
8058{
8059 struct hdr_ctx local_ctx;
8060 char *ptr_hist[MAX_HDR_HISTORY];
8061 int len_hist[MAX_HDR_HISTORY];
8062 unsigned int hist_ptr;
8063 int found;
8064
8065 if (!ctx) {
8066 local_ctx.idx = 0;
8067 ctx = &local_ctx;
8068 }
8069
8070 if (occ >= 0) {
8071 /* search from the beginning */
8072 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8073 occ--;
8074 if (occ <= 0) {
8075 *vptr = ctx->line + ctx->val;
8076 *vlen = ctx->vlen;
8077 return 1;
8078 }
8079 }
8080 return 0;
8081 }
8082
8083 /* negative occurrence, we scan all the list then walk back */
8084 if (-occ > MAX_HDR_HISTORY)
8085 return 0;
8086
8087 found = hist_ptr = 0;
8088 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8089 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8090 len_hist[hist_ptr] = ctx->vlen;
8091 if (++hist_ptr >= MAX_HDR_HISTORY)
8092 hist_ptr = 0;
8093 found++;
8094 }
8095 if (-occ > found)
8096 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008097
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008098 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008099 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8100 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8101 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008102 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008103 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008104 if (hist_ptr >= MAX_HDR_HISTORY)
8105 hist_ptr -= MAX_HDR_HISTORY;
8106 *vptr = ptr_hist[hist_ptr];
8107 *vlen = len_hist[hist_ptr];
8108 return 1;
8109}
8110
Willy Tarreaubaaee002006-06-26 02:48:02 +02008111/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008112 * Print a debug line with a header. Always stop at the first CR or LF char,
8113 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8114 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008115 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008116void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008117{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008118 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008119 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008120
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008121 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008122 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008123 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008124 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 +02008125
8126 for (max = 0; start + max < end; max++)
8127 if (start[max] == '\r' || start[max] == '\n')
8128 break;
8129
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008130 UBOUND(max, trash.size - trash.len - 3);
8131 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8132 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008133 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008134}
8135
Willy Tarreaueee5b512015-04-03 23:46:31 +02008136
8137/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8138 * The hdr_idx is allocated as well. In case of allocation failure, everything
8139 * allocated is freed and NULL is returned. Otherwise the new transaction is
8140 * assigned to the stream and returned.
8141 */
8142struct http_txn *http_alloc_txn(struct stream *s)
8143{
8144 struct http_txn *txn = s->txn;
8145
8146 if (txn)
8147 return txn;
8148
Willy Tarreaubafbe012017-11-24 17:34:44 +01008149 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008150 if (!txn)
8151 return txn;
8152
8153 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008154 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008155 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008156 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008157 return NULL;
8158 }
8159
8160 s->txn = txn;
8161 return txn;
8162}
8163
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008164void http_txn_reset_req(struct http_txn *txn)
8165{
8166 txn->req.flags = 0;
8167 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8168 txn->req.next = 0;
8169 txn->req.chunk_len = 0LL;
8170 txn->req.body_len = 0LL;
8171 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8172}
8173
8174void http_txn_reset_res(struct http_txn *txn)
8175{
8176 txn->rsp.flags = 0;
8177 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8178 txn->rsp.next = 0;
8179 txn->rsp.chunk_len = 0LL;
8180 txn->rsp.body_len = 0LL;
8181 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8182}
8183
Willy Tarreau0937bc42009-12-22 15:03:09 +01008184/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008185 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008186 * the required fields are properly allocated and that we only need to (re)init
8187 * them. This should be used before processing any new request.
8188 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008189void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008190{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008191 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008192 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008193
8194 txn->flags = 0;
8195 txn->status = -1;
8196
Willy Tarreauf64d1412010-10-07 20:06:11 +02008197 txn->cookie_first_date = 0;
8198 txn->cookie_last_date = 0;
8199
Willy Tarreaueee5b512015-04-03 23:46:31 +02008200 txn->srv_cookie = NULL;
8201 txn->cli_cookie = NULL;
8202 txn->uri = NULL;
8203
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008204 http_txn_reset_req(txn);
8205 http_txn_reset_res(txn);
8206
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008207 txn->req.chn = &s->req;
8208 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008209
8210 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008211
8212 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8213 if (fe->options2 & PR_O2_REQBUG_OK)
8214 txn->req.err_pos = -1; /* let buggy requests pass */
8215
Willy Tarreau0937bc42009-12-22 15:03:09 +01008216 if (txn->hdr_idx.v)
8217 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008218
8219 vars_init(&s->vars_txn, SCOPE_TXN);
8220 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008221}
8222
8223/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008224void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008225{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008226 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008227 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008228
8229 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008230 pool_free(pool_head_requri, txn->uri);
8231 pool_free(pool_head_capture, txn->cli_cookie);
8232 pool_free(pool_head_capture, txn->srv_cookie);
8233 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008234
William Lallemanda73203e2012-03-12 12:48:57 +01008235 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008236 txn->uri = NULL;
8237 txn->srv_cookie = NULL;
8238 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008239
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008240 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008241 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008242 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008243 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008244 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008245 }
8246
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008247 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008248 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008249 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008250 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008251 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008252 }
8253
Willy Tarreau6204cd92016-03-10 16:33:04 +01008254 vars_prune(&s->vars_txn, s->sess, s);
8255 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008256}
8257
8258/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008259void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008260{
8261 http_end_txn(s);
8262 http_init_txn(s);
8263
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008264 /* reinitialise the current rule list pointer to NULL. We are sure that
8265 * any rulelist match the NULL pointer.
8266 */
8267 s->current_rule_list = NULL;
8268
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008269 s->be = strm_fe(s);
8270 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008271 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008272 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008273 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008274 /* re-init store persistence */
8275 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008276 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008277
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008278 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008279
Willy Tarreau739cfba2010-01-25 23:11:14 +01008280 /* We must trim any excess data from the response buffer, because we
8281 * may have blocked an invalid response from a server that we don't
8282 * want to accidentely forward once we disable the analysers, nor do
8283 * we want those data to come along with next response. A typical
8284 * example of such data would be from a buggy server responding to
8285 * a HEAD with some data, or sending more than the advertised
8286 * content-length.
8287 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008288 if (unlikely(s->res.buf->i))
8289 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008290
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008291 /* Now we can realign the response buffer */
8292 buffer_realign(s->res.buf);
8293
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008294 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008295 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008296
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008297 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008298 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008299
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008300 s->req.rex = TICK_ETERNITY;
8301 s->req.wex = TICK_ETERNITY;
8302 s->req.analyse_exp = TICK_ETERNITY;
8303 s->res.rex = TICK_ETERNITY;
8304 s->res.wex = TICK_ETERNITY;
8305 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008306 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008307}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008308
Sasha Pachev218f0642014-06-16 12:05:59 -06008309void free_http_res_rules(struct list *r)
8310{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008311 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008312
8313 list_for_each_entry_safe(pr, tr, r, list) {
8314 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008315 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008316 free(pr);
8317 }
8318}
8319
8320void free_http_req_rules(struct list *r)
8321{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008322 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008323
8324 list_for_each_entry_safe(pr, tr, r, list) {
8325 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008326 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008327 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008328
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008329 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008330 free(pr);
8331 }
8332}
8333
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008334/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008335struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008336{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008337 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008338 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008339 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008340 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008341
Vincent Bernat02779b62016-04-03 13:48:43 +02008342 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008343 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008344 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008345 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008346 }
8347
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008348 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008349 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008350 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008351 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008352 int code;
8353 int hc;
8354
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008355 if (!strcmp(args[0], "tarpit")) {
8356 rule->action = ACT_HTTP_REQ_TARPIT;
8357 rule->deny_status = HTTP_ERR_500;
8358 }
8359 else {
8360 rule->action = ACT_ACTION_DENY;
8361 rule->deny_status = HTTP_ERR_403;
8362 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008363 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008364 if (strcmp(args[cur_arg], "deny_status") == 0) {
8365 cur_arg++;
8366 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008367 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8368 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008369 goto out_err;
8370 }
8371
8372 code = atol(args[cur_arg]);
8373 cur_arg++;
8374 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8375 if (http_err_codes[hc] == code) {
8376 rule->deny_status = hc;
8377 break;
8378 }
8379 }
8380
8381 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008382 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8383 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008384 }
8385 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008386 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008387 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008388 cur_arg = 1;
8389
8390 while(*args[cur_arg]) {
8391 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008392 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008393 cur_arg+=2;
8394 continue;
8395 } else
8396 break;
8397 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008398 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008399 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008400 cur_arg = 1;
8401
8402 if (!*args[cur_arg] ||
8403 (*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 +01008404 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8405 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008406 goto out_err;
8407 }
8408 rule->arg.nice = atoi(args[cur_arg]);
8409 if (rule->arg.nice < -1024)
8410 rule->arg.nice = -1024;
8411 else if (rule->arg.nice > 1024)
8412 rule->arg.nice = 1024;
8413 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008414 } else if (!strcmp(args[0], "set-tos")) {
8415#ifdef IP_TOS
8416 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008417 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008418 cur_arg = 1;
8419
8420 if (!*args[cur_arg] ||
8421 (*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 +01008422 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8423 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008424 goto out_err;
8425 }
8426
8427 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8428 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008429 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8430 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008431 goto out_err;
8432 }
8433 cur_arg++;
8434#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008435 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 +02008436 goto out_err;
8437#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008438 } else if (!strcmp(args[0], "set-mark")) {
8439#ifdef SO_MARK
8440 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008441 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008442 cur_arg = 1;
8443
8444 if (!*args[cur_arg] ||
8445 (*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 +01008446 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8447 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008448 goto out_err;
8449 }
8450
8451 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8452 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008453 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8454 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008455 goto out_err;
8456 }
8457 cur_arg++;
8458 global.last_checks |= LSTCHK_NETADM;
8459#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008460 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 +02008461 goto out_err;
8462#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008463 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008464 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008465 cur_arg = 1;
8466
8467 if (!*args[cur_arg] ||
8468 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8469 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008470 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8471 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008472 goto out_err;
8473 }
8474 if (strcmp(args[cur_arg], "silent") == 0)
8475 rule->arg.loglevel = -1;
8476 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8477 goto bad_log_level;
8478 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008479 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008480 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008481 cur_arg = 1;
8482
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008483 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8484 (*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 +01008485 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8486 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008487 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008488 }
8489
8490 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8491 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8492 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008493
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008494 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008495 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008496 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 +01008497 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008498 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8499 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008500 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008501 goto out_err;
8502 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008503 free(proxy->conf.lfs_file);
8504 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8505 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008506 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008507 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008508 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008509 cur_arg = 1;
8510
8511 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008512 (*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 +01008513 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8514 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008515 goto out_err;
8516 }
8517
8518 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8519 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8520 LIST_INIT(&rule->arg.hdr_add.fmt);
8521
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008522 error = NULL;
8523 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008524 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8525 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008526 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008527 goto out_err;
8528 }
8529
8530 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008531 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008532 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 +01008533 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008534 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8535 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008536 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008537 goto out_err;
8538 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008539
8540 free(proxy->conf.lfs_file);
8541 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8542 proxy->conf.lfs_line = proxy->conf.args.line;
8543 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008544 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008545 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008546 cur_arg = 1;
8547
8548 if (!*args[cur_arg] ||
8549 (*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 +01008550 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8551 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008552 goto out_err;
8553 }
8554
8555 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8556 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8557
8558 proxy->conf.args.ctx = ARGC_HRQ;
8559 free(proxy->conf.lfs_file);
8560 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8561 proxy->conf.lfs_line = proxy->conf.args.line;
8562 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008563 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008564 struct sample_expr *expr;
8565 unsigned int where;
8566 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008567 unsigned int tsc_num;
8568 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008569
8570 cur_arg = 1;
8571 proxy->conf.args.ctx = ARGC_TRK;
8572
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008573 tsc_num_str = &args[0][8];
8574 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8575 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8576 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8577 free(err);
8578 goto out_err;
8579 }
8580
Willy Tarreau09448f72014-06-25 18:12:15 +02008581 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8582 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008583 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8584 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008585 free(err);
8586 goto out_err;
8587 }
8588
8589 where = 0;
8590 if (proxy->cap & PR_CAP_FE)
8591 where |= SMP_VAL_FE_HRQ_HDR;
8592 if (proxy->cap & PR_CAP_BE)
8593 where |= SMP_VAL_BE_HRQ_HDR;
8594
8595 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008596 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8597 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8598 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8599 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008600 free(expr);
8601 goto out_err;
8602 }
8603
8604 if (strcmp(args[cur_arg], "table") == 0) {
8605 cur_arg++;
8606 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008607 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8608 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008609 free(expr);
8610 goto out_err;
8611 }
8612 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008613 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008614 cur_arg++;
8615 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008616 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008617 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008618 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008619 } else if (strcmp(args[0], "redirect") == 0) {
8620 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008621 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008622
Willy Tarreaube4653b2015-05-28 15:26:58 +02008623 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008624 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8625 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008626 goto out_err;
8627 }
8628
8629 /* this redirect rule might already contain a parsed condition which
8630 * we'll pass to the http-request rule.
8631 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008632 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008633 rule->arg.redir = redir;
8634 rule->cond = redir->cond;
8635 redir->cond = NULL;
8636 cur_arg = 2;
8637 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008638 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8639 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008640 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008641 /*
8642 * '+ 8' for 'add-acl('
8643 * '- 9' for 'add-acl(' + trailing ')'
8644 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008645 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008646
8647 cur_arg = 1;
8648
8649 if (!*args[cur_arg] ||
8650 (*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 +01008651 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8652 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008653 goto out_err;
8654 }
8655
8656 LIST_INIT(&rule->arg.map.key);
8657 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008658 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008659 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008660 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008661 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8662 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008663 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008664 goto out_err;
8665 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008666 free(proxy->conf.lfs_file);
8667 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8668 proxy->conf.lfs_line = proxy->conf.args.line;
8669 cur_arg += 1;
8670 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8671 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008672 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008673 /*
8674 * '+ 8' for 'del-acl('
8675 * '- 9' for 'del-acl(' + trailing ')'
8676 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008677 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008678
8679 cur_arg = 1;
8680
8681 if (!*args[cur_arg] ||
8682 (*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 +01008683 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8684 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008685 goto out_err;
8686 }
8687
8688 LIST_INIT(&rule->arg.map.key);
8689 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008690 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008691 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008692 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008693 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8694 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008695 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008696 goto out_err;
8697 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008698 free(proxy->conf.lfs_file);
8699 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8700 proxy->conf.lfs_line = proxy->conf.args.line;
8701 cur_arg += 1;
8702 } else if (strncmp(args[0], "del-map", 7) == 0) {
8703 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008704 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008705 /*
8706 * '+ 8' for 'del-map('
8707 * '- 9' for 'del-map(' + trailing ')'
8708 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008709 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008710
8711 cur_arg = 1;
8712
8713 if (!*args[cur_arg] ||
8714 (*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 +01008715 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8716 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008717 goto out_err;
8718 }
8719
8720 LIST_INIT(&rule->arg.map.key);
8721 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008722 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008723 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008724 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008725 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8726 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008727 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008728 goto out_err;
8729 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008730 free(proxy->conf.lfs_file);
8731 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8732 proxy->conf.lfs_line = proxy->conf.args.line;
8733 cur_arg += 1;
8734 } else if (strncmp(args[0], "set-map", 7) == 0) {
8735 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008736 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008737 /*
8738 * '+ 8' for 'set-map('
8739 * '- 9' for 'set-map(' + trailing ')'
8740 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008741 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008742
8743 cur_arg = 1;
8744
8745 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8746 (*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 +01008747 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8748 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008749 goto out_err;
8750 }
8751
8752 LIST_INIT(&rule->arg.map.key);
8753 LIST_INIT(&rule->arg.map.value);
8754 proxy->conf.args.ctx = ARGC_HRQ;
8755
8756 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008757 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008758 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008759 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008760 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8761 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008762 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008763 goto out_err;
8764 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008765
8766 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008767 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008768 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 +01008769 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008770 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8771 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008772 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008773 goto out_err;
8774 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008775 free(proxy->conf.lfs_file);
8776 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8777 proxy->conf.lfs_line = proxy->conf.args.line;
8778
8779 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008780 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8781 char *errmsg = NULL;
8782 cur_arg = 1;
8783 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008784 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008785 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008786 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008787 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8788 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008789 free(errmsg);
8790 goto out_err;
8791 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008792 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008793 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008794 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8795 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8796 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8797 "%s%s, but got '%s'%s.\n",
8798 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008799 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008800 }
8801
8802 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8803 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008804 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008805
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008806 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008807 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8808 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008809 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008810 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008811 }
8812 rule->cond = cond;
8813 }
8814 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008815 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8816 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8817 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008818 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008819 }
8820
8821 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008822 out_err:
8823 free(rule);
8824 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008825}
8826
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008827/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008828struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008829{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008830 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008831 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008832 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008833 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008834
8835 rule = calloc(1, sizeof(*rule));
8836 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008837 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008838 goto out_err;
8839 }
8840
8841 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008842 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008843 cur_arg = 1;
8844 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008845 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008846 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008847 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008848 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008849 cur_arg = 1;
8850
8851 if (!*args[cur_arg] ||
8852 (*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 +01008853 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8854 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008855 goto out_err;
8856 }
8857 rule->arg.nice = atoi(args[cur_arg]);
8858 if (rule->arg.nice < -1024)
8859 rule->arg.nice = -1024;
8860 else if (rule->arg.nice > 1024)
8861 rule->arg.nice = 1024;
8862 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008863 } else if (!strcmp(args[0], "set-tos")) {
8864#ifdef IP_TOS
8865 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008866 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008867 cur_arg = 1;
8868
8869 if (!*args[cur_arg] ||
8870 (*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 +01008871 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8872 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008873 goto out_err;
8874 }
8875
8876 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8877 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008878 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8879 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008880 goto out_err;
8881 }
8882 cur_arg++;
8883#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008884 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 +02008885 goto out_err;
8886#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008887 } else if (!strcmp(args[0], "set-mark")) {
8888#ifdef SO_MARK
8889 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008890 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008891 cur_arg = 1;
8892
8893 if (!*args[cur_arg] ||
8894 (*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 +01008895 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8896 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008897 goto out_err;
8898 }
8899
8900 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8901 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008902 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8903 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008904 goto out_err;
8905 }
8906 cur_arg++;
8907 global.last_checks |= LSTCHK_NETADM;
8908#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008909 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 +02008910 goto out_err;
8911#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008912 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008913 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008914 cur_arg = 1;
8915
8916 if (!*args[cur_arg] ||
8917 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8918 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008919 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8920 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008921 goto out_err;
8922 }
8923 if (strcmp(args[cur_arg], "silent") == 0)
8924 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008925 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008926 goto bad_log_level;
8927 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008928 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008929 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008930 cur_arg = 1;
8931
8932 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8933 (*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 +01008934 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8935 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008936 goto out_err;
8937 }
8938
8939 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8940 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8941 LIST_INIT(&rule->arg.hdr_add.fmt);
8942
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008943 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008944 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008945 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 +01008946 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008947 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8948 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008949 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008950 goto out_err;
8951 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008952 free(proxy->conf.lfs_file);
8953 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8954 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008955 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008956 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008957 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008958 cur_arg = 1;
8959
8960 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008961 (*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 +01008962 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8963 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008964 goto out_err;
8965 }
8966
8967 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8968 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8969 LIST_INIT(&rule->arg.hdr_add.fmt);
8970
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008971 error = NULL;
8972 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008973 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8974 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008975 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008976 goto out_err;
8977 }
8978
8979 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008980 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008981 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 +01008982 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008983 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8984 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008985 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008986 goto out_err;
8987 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008988
8989 free(proxy->conf.lfs_file);
8990 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8991 proxy->conf.lfs_line = proxy->conf.args.line;
8992 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008993 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008994 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008995 cur_arg = 1;
8996
8997 if (!*args[cur_arg] ||
8998 (*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 +01008999 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9000 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009001 goto out_err;
9002 }
9003
9004 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9005 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9006
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009007 proxy->conf.args.ctx = ARGC_HRS;
9008 free(proxy->conf.lfs_file);
9009 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9010 proxy->conf.lfs_line = proxy->conf.args.line;
9011 cur_arg += 1;
9012 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9013 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009014 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009015 /*
9016 * '+ 8' for 'add-acl('
9017 * '- 9' for 'add-acl(' + trailing ')'
9018 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009019 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009020
9021 cur_arg = 1;
9022
9023 if (!*args[cur_arg] ||
9024 (*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 +01009025 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9026 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009027 goto out_err;
9028 }
9029
9030 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009031 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009032 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009033 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009034 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009035 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9036 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009037 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009038 goto out_err;
9039 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009040 free(proxy->conf.lfs_file);
9041 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9042 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009043
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009044 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009045 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9046 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009047 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009048 /*
9049 * '+ 8' for 'del-acl('
9050 * '- 9' for 'del-acl(' + trailing ')'
9051 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009052 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009053
9054 cur_arg = 1;
9055
9056 if (!*args[cur_arg] ||
9057 (*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 +01009058 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9059 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009060 goto out_err;
9061 }
9062
9063 LIST_INIT(&rule->arg.map.key);
9064 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009065 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009066 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009067 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009068 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9069 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009070 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009071 goto out_err;
9072 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009073 free(proxy->conf.lfs_file);
9074 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9075 proxy->conf.lfs_line = proxy->conf.args.line;
9076 cur_arg += 1;
9077 } else if (strncmp(args[0], "del-map", 7) == 0) {
9078 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009079 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009080 /*
9081 * '+ 8' for 'del-map('
9082 * '- 9' for 'del-map(' + trailing ')'
9083 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009084 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009085
9086 cur_arg = 1;
9087
9088 if (!*args[cur_arg] ||
9089 (*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 +01009090 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9091 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009092 goto out_err;
9093 }
9094
9095 LIST_INIT(&rule->arg.map.key);
9096 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009097 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009098 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009099 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009100 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9101 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009102 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009103 goto out_err;
9104 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009105 free(proxy->conf.lfs_file);
9106 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9107 proxy->conf.lfs_line = proxy->conf.args.line;
9108 cur_arg += 1;
9109 } else if (strncmp(args[0], "set-map", 7) == 0) {
9110 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009111 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009112 /*
9113 * '+ 8' for 'set-map('
9114 * '- 9' for 'set-map(' + trailing ')'
9115 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009116 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009117
9118 cur_arg = 1;
9119
9120 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9121 (*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 +01009122 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9123 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009124 goto out_err;
9125 }
9126
9127 LIST_INIT(&rule->arg.map.key);
9128 LIST_INIT(&rule->arg.map.value);
9129
9130 proxy->conf.args.ctx = ARGC_HRS;
9131
9132 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009133 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009134 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009135 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009136 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9137 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009138 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009139 goto out_err;
9140 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009141
9142 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009143 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009144 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 +01009145 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009146 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9147 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009148 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009149 goto out_err;
9150 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009151
9152 free(proxy->conf.lfs_file);
9153 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9154 proxy->conf.lfs_line = proxy->conf.args.line;
9155
9156 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009157 } else if (strcmp(args[0], "redirect") == 0) {
9158 struct redirect_rule *redir;
9159 char *errmsg = NULL;
9160
9161 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009162 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9163 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009164 goto out_err;
9165 }
9166
9167 /* this redirect rule might already contain a parsed condition which
9168 * we'll pass to the http-request rule.
9169 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009170 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009171 rule->arg.redir = redir;
9172 rule->cond = redir->cond;
9173 redir->cond = NULL;
9174 cur_arg = 2;
9175 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009176 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009177 struct sample_expr *expr;
9178 unsigned int where;
9179 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009180 unsigned int tsc_num;
9181 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009182
9183 cur_arg = 1;
9184 proxy->conf.args.ctx = ARGC_TRK;
9185
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009186 tsc_num_str = &args[0][8];
9187 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9188 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9189 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9190 free(err);
9191 goto out_err;
9192 }
9193
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009194 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9195 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009196 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9197 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009198 free(err);
9199 goto out_err;
9200 }
9201
9202 where = 0;
9203 if (proxy->cap & PR_CAP_FE)
9204 where |= SMP_VAL_FE_HRS_HDR;
9205 if (proxy->cap & PR_CAP_BE)
9206 where |= SMP_VAL_BE_HRS_HDR;
9207
9208 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009209 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9210 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9211 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9212 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009213 free(expr);
9214 goto out_err;
9215 }
9216
9217 if (strcmp(args[cur_arg], "table") == 0) {
9218 cur_arg++;
9219 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009220 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9221 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009222 free(expr);
9223 goto out_err;
9224 }
9225 /* we copy the table name for now, it will be resolved later */
9226 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9227 cur_arg++;
9228 }
9229 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009230 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009231 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009232 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9233 char *errmsg = NULL;
9234 cur_arg = 1;
9235 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009236 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009237 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009238 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009239 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9240 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009241 free(errmsg);
9242 goto out_err;
9243 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009244 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009245 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009246 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9247 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9248 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9249 "%s%s, but got '%s'%s.\n",
9250 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009251 goto out_err;
9252 }
9253
9254 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9255 struct acl_cond *cond;
9256 char *errmsg = NULL;
9257
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009258 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009259 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9260 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009261 free(errmsg);
9262 goto out_err;
9263 }
9264 rule->cond = cond;
9265 }
9266 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009267 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9268 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9269 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009270 goto out_err;
9271 }
9272
9273 return rule;
9274 out_err:
9275 free(rule);
9276 return NULL;
9277}
9278
Willy Tarreau4baae242012-12-27 12:00:31 +01009279/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009280 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009281 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9282 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009283 */
9284struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009285 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009286{
9287 struct redirect_rule *rule;
9288 int cur_arg;
9289 int type = REDIRECT_TYPE_NONE;
9290 int code = 302;
9291 const char *destination = NULL;
9292 const char *cookie = NULL;
9293 int cookie_set = 0;
9294 unsigned int flags = REDIRECT_FLAG_NONE;
9295 struct acl_cond *cond = NULL;
9296
9297 cur_arg = 0;
9298 while (*(args[cur_arg])) {
9299 if (strcmp(args[cur_arg], "location") == 0) {
9300 if (!*args[cur_arg + 1])
9301 goto missing_arg;
9302
9303 type = REDIRECT_TYPE_LOCATION;
9304 cur_arg++;
9305 destination = args[cur_arg];
9306 }
9307 else if (strcmp(args[cur_arg], "prefix") == 0) {
9308 if (!*args[cur_arg + 1])
9309 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009310 type = REDIRECT_TYPE_PREFIX;
9311 cur_arg++;
9312 destination = args[cur_arg];
9313 }
9314 else if (strcmp(args[cur_arg], "scheme") == 0) {
9315 if (!*args[cur_arg + 1])
9316 goto missing_arg;
9317
9318 type = REDIRECT_TYPE_SCHEME;
9319 cur_arg++;
9320 destination = args[cur_arg];
9321 }
9322 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9323 if (!*args[cur_arg + 1])
9324 goto missing_arg;
9325
9326 cur_arg++;
9327 cookie = args[cur_arg];
9328 cookie_set = 1;
9329 }
9330 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9331 if (!*args[cur_arg + 1])
9332 goto missing_arg;
9333
9334 cur_arg++;
9335 cookie = args[cur_arg];
9336 cookie_set = 0;
9337 }
9338 else if (strcmp(args[cur_arg], "code") == 0) {
9339 if (!*args[cur_arg + 1])
9340 goto missing_arg;
9341
9342 cur_arg++;
9343 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009344 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009345 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009346 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009347 args[cur_arg - 1], args[cur_arg]);
9348 return NULL;
9349 }
9350 }
9351 else if (!strcmp(args[cur_arg],"drop-query")) {
9352 flags |= REDIRECT_FLAG_DROP_QS;
9353 }
9354 else if (!strcmp(args[cur_arg],"append-slash")) {
9355 flags |= REDIRECT_FLAG_APPEND_SLASH;
9356 }
9357 else if (strcmp(args[cur_arg], "if") == 0 ||
9358 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009359 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009360 if (!cond) {
9361 memprintf(errmsg, "error in condition: %s", *errmsg);
9362 return NULL;
9363 }
9364 break;
9365 }
9366 else {
9367 memprintf(errmsg,
9368 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9369 args[cur_arg]);
9370 return NULL;
9371 }
9372 cur_arg++;
9373 }
9374
9375 if (type == REDIRECT_TYPE_NONE) {
9376 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9377 return NULL;
9378 }
9379
Willy Tarreaube4653b2015-05-28 15:26:58 +02009380 if (dir && type != REDIRECT_TYPE_LOCATION) {
9381 memprintf(errmsg, "response only supports redirect type 'location'");
9382 return NULL;
9383 }
9384
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009385 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009386 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009387 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009388
9389 if (!use_fmt) {
9390 /* old-style static redirect rule */
9391 rule->rdr_str = strdup(destination);
9392 rule->rdr_len = strlen(destination);
9393 }
9394 else {
9395 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009396
9397 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9398 * if prefix == "/", we don't want to add anything, otherwise it
9399 * makes it hard for the user to configure a self-redirection.
9400 */
Godbachd9722032014-12-18 15:44:58 +08009401 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009402 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009403 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9404 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 +01009405 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9406 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009407 return NULL;
9408 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009409 free(curproxy->conf.lfs_file);
9410 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9411 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009412 }
9413 }
9414
Willy Tarreau4baae242012-12-27 12:00:31 +01009415 if (cookie) {
9416 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9417 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9418 */
9419 rule->cookie_len = strlen(cookie);
9420 if (cookie_set) {
9421 rule->cookie_str = malloc(rule->cookie_len + 10);
9422 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9423 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9424 rule->cookie_len += 9;
9425 } else {
9426 rule->cookie_str = malloc(rule->cookie_len + 21);
9427 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9428 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9429 rule->cookie_len += 20;
9430 }
9431 }
9432 rule->type = type;
9433 rule->code = code;
9434 rule->flags = flags;
9435 LIST_INIT(&rule->list);
9436 return rule;
9437
9438 missing_arg:
9439 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9440 return NULL;
9441}
9442
Willy Tarreau8797c062007-05-07 00:55:35 +02009443/************************************************************************/
9444/* The code below is dedicated to ACL parsing and matching */
9445/************************************************************************/
9446
9447
Willy Tarreau14174bc2012-04-16 14:34:04 +02009448/* This function ensures that the prerequisites for an L7 fetch are ready,
9449 * which means that a request or response is ready. If some data is missing,
9450 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009451 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9452 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009453 *
9454 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009455 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9456 * decide whether or not an HTTP message is present ;
9457 * 0 if the requested data cannot be fetched or if it is certain that
9458 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009459 * 1 if an HTTP message is ready
9460 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009461int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009462 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009463{
Willy Tarreau192252e2015-04-04 01:47:55 +02009464 struct http_txn *txn;
9465 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009466
Willy Tarreaube508f12016-03-10 11:47:01 +01009467 /* Note: it is possible that <s> is NULL when called before stream
9468 * initialization (eg: tcp-request connection), so this function is the
9469 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009470 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009471 if (!s)
9472 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009473
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009474 if (!s->txn) {
9475 if (unlikely(!http_alloc_txn(s)))
9476 return 0; /* not enough memory */
9477 http_init_txn(s);
9478 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009479 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009480 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009481
9482 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009483 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009484
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009485 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009486 /* If the buffer does not leave enough free space at the end,
9487 * we must first realign it.
9488 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009489 if (s->req.buf->p > s->req.buf->data &&
9490 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9491 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009492
Willy Tarreau14174bc2012-04-16 14:34:04 +02009493 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009494 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009495 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009496
9497 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009498 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009499 http_msg_analyzer(msg, &txn->hdr_idx);
9500
9501 /* Still no valid request ? */
9502 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009503 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009504 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009505 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009506 }
9507 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009508 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009509 return 0;
9510 }
9511
9512 /* OK we just got a valid HTTP request. We have some minor
9513 * preparation to perform so that further checks can rely
9514 * on HTTP tests.
9515 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009516
9517 /* If the request was parsed but was too large, we must absolutely
9518 * return an error so that it is not processed. At the moment this
9519 * cannot happen, but if the parsers are to change in the future,
9520 * we want this check to be maintained.
9521 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009522 if (unlikely(s->req.buf->i + s->req.buf->p >
9523 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009524 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009525 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009526 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009527 return 1;
9528 }
9529
Willy Tarreau9b28e032012-10-12 23:49:43 +02009530 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009531 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009532 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009533
Willy Tarreau506d0502013-07-06 13:29:24 +02009534 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9535 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009536 }
9537
Willy Tarreau506d0502013-07-06 13:29:24 +02009538 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009539 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009540 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009541
9542 /* otherwise everything's ready for the request */
9543 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009544 else {
9545 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009546 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9547 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009548 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009549 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009550 }
9551
9552 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009553 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009554 return 1;
9555}
Willy Tarreau8797c062007-05-07 00:55:35 +02009556
Willy Tarreau8797c062007-05-07 00:55:35 +02009557/* 1. Check on METHOD
9558 * We use the pre-parsed method if it is known, and store its number as an
9559 * integer. If it is unknown, we use the pointer and the length.
9560 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009561static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009562{
9563 int len, meth;
9564
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009565 len = strlen(text);
9566 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009567
9568 pattern->val.i = meth;
9569 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009570 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009571 pattern->len = len;
9572 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009573 else {
9574 pattern->ptr.str = NULL;
9575 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009576 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009577 return 1;
9578}
9579
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009580/* This function fetches the method of current HTTP request and stores
9581 * it in the global pattern struct as a chunk. There are two possibilities :
9582 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9583 * in <len> and <ptr> is NULL ;
9584 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9585 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009586 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009587 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009588static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009589smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009590{
9591 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009592 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009593
Willy Tarreau24e32d82012-04-23 23:55:44 +02009594 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009595
Willy Tarreaube508f12016-03-10 11:47:01 +01009596 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009597 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009598 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009599 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009600 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009601 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9602 /* ensure the indexes are not affected */
9603 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009604 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009605 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9606 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009607 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009608 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009609 return 1;
9610}
9611
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009612/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009613static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009614{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009615 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009616 struct pattern_list *lst;
9617 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009618
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009619 list_for_each_entry(lst, &expr->patterns, list) {
9620 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009621
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009622 /* well-known method */
9623 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009624 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009625 return pattern;
9626 else
9627 continue;
9628 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009629
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009630 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009631 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009632 continue;
9633
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009634 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009635 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9636 (!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 +01009637 return pattern;
9638 }
9639 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009640}
9641
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009642static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009643smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009644{
Willy Tarreaube508f12016-03-10 11:47:01 +01009645 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009646 char *ptr;
9647 int len;
9648
Willy Tarreauc0239e02012-04-16 14:42:55 +02009649 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009650
Willy Tarreaube508f12016-03-10 11:47:01 +01009651 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009652 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009653 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009654
9655 while ((len-- > 0) && (*ptr++ != '/'));
9656 if (len <= 0)
9657 return 0;
9658
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009659 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009660 smp->data.u.str.str = ptr;
9661 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009662
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009663 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009664 return 1;
9665}
9666
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009667static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009668smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009669{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009670 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009671 char *ptr;
9672 int len;
9673
Willy Tarreauc0239e02012-04-16 14:42:55 +02009674 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009675
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009676 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009677 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9678 return 0;
9679
Willy Tarreau8797c062007-05-07 00:55:35 +02009680 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009681 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009682
9683 while ((len-- > 0) && (*ptr++ != '/'));
9684 if (len <= 0)
9685 return 0;
9686
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009687 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009688 smp->data.u.str.str = ptr;
9689 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009690
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009691 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009692 return 1;
9693}
9694
9695/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009696static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009697smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009698{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009699 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009700 char *ptr;
9701 int len;
9702
Willy Tarreauc0239e02012-04-16 14:42:55 +02009703 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009704
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009705 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009706 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9707 return 0;
9708
Willy Tarreau8797c062007-05-07 00:55:35 +02009709 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009710 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009711
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009712 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009713 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009714 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009715 return 1;
9716}
9717
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009718static int
9719smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9720{
9721 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9722 return 0;
9723
9724 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009725 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009726 return 0;
9727 smp->strm->unique_id[0] = '\0';
9728 }
9729 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9730 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9731
9732 smp->data.type = SMP_T_STR;
9733 smp->data.u.str.str = smp->strm->unique_id;
9734 smp->flags = SMP_F_CONST;
9735 return 1;
9736}
9737
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009738/* Returns a string block containing all headers including the
9739 * empty line wich separes headers from the body. This is useful
9740 * form some headers analysis.
9741 */
9742static int
9743smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9744{
9745 struct http_msg *msg;
9746 struct hdr_idx *idx;
9747 struct http_txn *txn;
9748
9749 CHECK_HTTP_MESSAGE_FIRST();
9750
9751 txn = smp->strm->txn;
9752 idx = &txn->hdr_idx;
9753 msg = &txn->req;
9754
9755 smp->data.type = SMP_T_STR;
9756 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9757 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9758 (msg->chn->buf->p[msg->eoh] == '\r');
9759
9760 return 1;
9761}
9762
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009763/* Returns the header request in a length/value encoded format.
9764 * This is useful for exchanges with the SPOE.
9765 *
9766 * A "length value" is a multibyte code encoding numbers. It uses the
9767 * SPOE format. The encoding is the following:
9768 *
9769 * Each couple "header name" / "header value" is composed
9770 * like this:
9771 * "length value" "header name bytes"
9772 * "length value" "header value bytes"
9773 * When the last header is reached, the header name and the header
9774 * value are empty. Their length are 0
9775 */
9776static int
9777smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9778{
9779 struct http_msg *msg;
9780 struct chunk *temp;
9781 struct hdr_idx *idx;
9782 const char *cur_ptr, *cur_next, *p;
9783 int old_idx, cur_idx;
9784 struct hdr_idx_elem *cur_hdr;
9785 const char *hn, *hv;
9786 int hnl, hvl;
9787 int ret;
9788 struct http_txn *txn;
9789 char *buf;
9790 char *end;
9791
9792 CHECK_HTTP_MESSAGE_FIRST();
9793
9794 temp = get_trash_chunk();
9795 buf = temp->str;
9796 end = temp->str + temp->size;
9797
9798 txn = smp->strm->txn;
9799 idx = &txn->hdr_idx;
9800 msg = &txn->req;
9801
9802 /* Build array of headers. */
9803 old_idx = 0;
9804 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9805 while (1) {
9806 cur_idx = idx->v[old_idx].next;
9807 if (!cur_idx)
9808 break;
9809 old_idx = cur_idx;
9810
9811 cur_hdr = &idx->v[cur_idx];
9812 cur_ptr = cur_next;
9813 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9814
9815 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9816 * and the next header starts at cur_next. We'll check
9817 * this header in the list as well as against the default
9818 * rule.
9819 */
9820
9821 /* look for ': *'. */
9822 hn = cur_ptr;
9823 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9824 if (p >= cur_ptr+cur_hdr->len)
9825 continue;
9826 hnl = p - hn;
9827 p++;
9828 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9829 p++;
9830 if (p >= cur_ptr + cur_hdr->len)
9831 continue;
9832 hv = p;
9833 hvl = cur_ptr + cur_hdr->len-p;
9834
9835 /* encode the header name. */
9836 ret = encode_varint(hnl, &buf, end);
9837 if (ret == -1)
9838 return 0;
9839 if (buf + hnl > end)
9840 return 0;
9841 memcpy(buf, hn, hnl);
9842 buf += hnl;
9843
9844 /* encode and copy the value. */
9845 ret = encode_varint(hvl, &buf, end);
9846 if (ret == -1)
9847 return 0;
9848 if (buf + hvl > end)
9849 return 0;
9850 memcpy(buf, hv, hvl);
9851 buf += hvl;
9852 }
9853
9854 /* encode the end of the header list with empty
9855 * header name and header value.
9856 */
9857 ret = encode_varint(0, &buf, end);
9858 if (ret == -1)
9859 return 0;
9860 ret = encode_varint(0, &buf, end);
9861 if (ret == -1)
9862 return 0;
9863
9864 /* Initialise sample data which will be filled. */
9865 smp->data.type = SMP_T_BIN;
9866 smp->data.u.str.str = temp->str;
9867 smp->data.u.str.len = buf - temp->str;
9868 smp->data.u.str.size = temp->size;
9869
9870 return 1;
9871}
9872
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009873/* returns the longest available part of the body. This requires that the body
9874 * has been waited for using http-buffer-request.
9875 */
9876static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009877smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009878{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009879 struct http_msg *msg;
9880 unsigned long len;
9881 unsigned long block1;
9882 char *body;
9883 struct chunk *temp;
9884
9885 CHECK_HTTP_MESSAGE_FIRST();
9886
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009887 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009888 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009889 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009890 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009891
9892 len = http_body_bytes(msg);
9893 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9894
9895 block1 = len;
9896 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9897 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9898
9899 if (block1 == len) {
9900 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009901 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009902 smp->data.u.str.str = body;
9903 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009904 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9905 }
9906 else {
9907 /* buffer is wrapped, we need to defragment it */
9908 temp = get_trash_chunk();
9909 memcpy(temp->str, body, block1);
9910 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009911 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009912 smp->data.u.str.str = temp->str;
9913 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009914 smp->flags = SMP_F_VOL_TEST;
9915 }
9916 return 1;
9917}
9918
9919
9920/* returns the available length of the body. This requires that the body
9921 * has been waited for using http-buffer-request.
9922 */
9923static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009924smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009925{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009926 struct http_msg *msg;
9927
9928 CHECK_HTTP_MESSAGE_FIRST();
9929
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009930 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009931 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009932 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009933 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009934
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009935 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009936 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009937
9938 smp->flags = SMP_F_VOL_TEST;
9939 return 1;
9940}
9941
9942
9943/* returns the advertised length of the body, or the advertised size of the
9944 * chunks available in the buffer. This requires that the body has been waited
9945 * for using http-buffer-request.
9946 */
9947static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009948smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009949{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009950 struct http_msg *msg;
9951
9952 CHECK_HTTP_MESSAGE_FIRST();
9953
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009954 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009955 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009956 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009957 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009958
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009959 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009960 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009961
9962 smp->flags = SMP_F_VOL_TEST;
9963 return 1;
9964}
9965
9966
Willy Tarreau8797c062007-05-07 00:55:35 +02009967/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009968static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009969smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009970{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009971 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009972
Willy Tarreauc0239e02012-04-16 14:42:55 +02009973 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009974
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009975 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009976 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009977 smp->data.u.str.len = txn->req.sl.rq.u_l;
9978 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009979 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009980 return 1;
9981}
9982
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009983static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009984smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009985{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009986 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009987 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009988
Willy Tarreauc0239e02012-04-16 14:42:55 +02009989 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009990
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009991 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009992 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 +02009993 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009994 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009995
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009996 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009997 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009998 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009999 return 1;
10000}
10001
10002static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010003smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010004{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010005 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010006 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010007
Willy Tarreauc0239e02012-04-16 14:42:55 +020010008 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010009
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010010 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010011 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 +020010012 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10013 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010014
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010015 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010016 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010017 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010018 return 1;
10019}
10020
Willy Tarreau185b5c42012-04-26 15:11:51 +020010021/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10022 * Accepts an optional argument of type string containing the header field name,
10023 * and an optional argument of type signed or unsigned integer to request an
10024 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010025 * headers are considered from the first one. It does not stop on commas and
10026 * returns full lines instead (useful for User-Agent or Date for example).
10027 */
10028static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010029smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010030{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010031 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010032 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010033 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010034 int occ = 0;
10035 const char *name_str = NULL;
10036 int name_len = 0;
10037
10038 if (!ctx) {
10039 /* first call */
10040 ctx = &static_hdr_ctx;
10041 ctx->idx = 0;
10042 smp->ctx.a[0] = ctx;
10043 }
10044
10045 if (args) {
10046 if (args[0].type != ARGT_STR)
10047 return 0;
10048 name_str = args[0].data.str.str;
10049 name_len = args[0].data.str.len;
10050
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010051 if (args[1].type == ARGT_SINT)
10052 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010053 }
10054
10055 CHECK_HTTP_MESSAGE_FIRST();
10056
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010057 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010058 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 +020010059
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010060 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10061 /* search for header from the beginning */
10062 ctx->idx = 0;
10063
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010064 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010065 /* no explicit occurrence and single fetch => last header by default */
10066 occ = -1;
10067
10068 if (!occ)
10069 /* prepare to report multiple occurrences for ACL fetches */
10070 smp->flags |= SMP_F_NOT_LAST;
10071
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010072 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010073 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010074 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 +020010075 return 1;
10076
10077 smp->flags &= ~SMP_F_NOT_LAST;
10078 return 0;
10079}
10080
10081/* 6. Check on HTTP header count. The number of occurrences is returned.
10082 * Accepts exactly 1 argument of type string. It does not stop on commas and
10083 * returns full lines instead (useful for User-Agent or Date for example).
10084 */
10085static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010086smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010087{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010088 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010089 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010090 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010091 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010092 const char *name = NULL;
10093 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010094
Willy Tarreau601a4d12015-04-01 19:16:09 +020010095 if (args && args->type == ARGT_STR) {
10096 name = args->data.str.str;
10097 len = args->data.str.len;
10098 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010099
10100 CHECK_HTTP_MESSAGE_FIRST();
10101
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010102 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010103 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 +020010104
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010105 ctx.idx = 0;
10106 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010107 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010108 cnt++;
10109
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010110 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010111 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010112 smp->flags = SMP_F_VOL_HDR;
10113 return 1;
10114}
10115
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010116static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010117smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010118{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010119 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010120 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010121 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010122 struct chunk *temp;
10123 char del = ',';
10124
10125 if (args && args->type == ARGT_STR)
10126 del = *args[0].data.str.str;
10127
10128 CHECK_HTTP_MESSAGE_FIRST();
10129
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010130 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010131 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 +020010132
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010133 temp = get_trash_chunk();
10134
10135 ctx.idx = 0;
10136 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10137 if (temp->len)
10138 temp->str[temp->len++] = del;
10139 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10140 temp->len += ctx.del;
10141 }
10142
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010143 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010144 smp->data.u.str.str = temp->str;
10145 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010146 smp->flags = SMP_F_VOL_HDR;
10147 return 1;
10148}
10149
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010150/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10151 * Accepts an optional argument of type string containing the header field name,
10152 * and an optional argument of type signed or unsigned integer to request an
10153 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010154 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010155 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010156static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010157smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010158{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010159 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010160 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010161 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010162 int occ = 0;
10163 const char *name_str = NULL;
10164 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010165
Willy Tarreaua890d072013-04-02 12:01:06 +020010166 if (!ctx) {
10167 /* first call */
10168 ctx = &static_hdr_ctx;
10169 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010170 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010171 }
10172
Willy Tarreau185b5c42012-04-26 15:11:51 +020010173 if (args) {
10174 if (args[0].type != ARGT_STR)
10175 return 0;
10176 name_str = args[0].data.str.str;
10177 name_len = args[0].data.str.len;
10178
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010179 if (args[1].type == ARGT_SINT)
10180 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010181 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010182
Willy Tarreaue333ec92012-04-16 16:26:40 +020010183 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010184
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010185 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010186 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 +020010187
Willy Tarreau185b5c42012-04-26 15:11:51 +020010188 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010189 /* search for header from the beginning */
10190 ctx->idx = 0;
10191
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010192 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010193 /* no explicit occurrence and single fetch => last header by default */
10194 occ = -1;
10195
10196 if (!occ)
10197 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010198 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010199
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010200 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010201 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010202 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 +020010203 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010204
Willy Tarreau37406352012-04-23 16:16:37 +020010205 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010206 return 0;
10207}
10208
Willy Tarreauc11416f2007-06-17 16:58:38 +020010209/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010210 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010211 */
10212static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010213smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010214{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010215 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010216 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010217 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010218 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010219 const char *name = NULL;
10220 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010221
Willy Tarreau601a4d12015-04-01 19:16:09 +020010222 if (args && args->type == ARGT_STR) {
10223 name = args->data.str.str;
10224 len = args->data.str.len;
10225 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010226
Willy Tarreaue333ec92012-04-16 16:26:40 +020010227 CHECK_HTTP_MESSAGE_FIRST();
10228
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010229 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010230 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 +020010231
Willy Tarreau33a7e692007-06-10 19:45:56 +020010232 ctx.idx = 0;
10233 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010234 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010235 cnt++;
10236
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010237 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010238 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010239 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010240 return 1;
10241}
10242
Willy Tarreau185b5c42012-04-26 15:11:51 +020010243/* Fetch an HTTP header's integer value. The integer value is returned. It
10244 * takes a mandatory argument of type string and an optional one of type int
10245 * to designate a specific occurrence. It returns an unsigned integer, which
10246 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010247 */
10248static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010249smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010250{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010251 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010252
Willy Tarreauf853c462012-04-23 18:53:56 +020010253 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010254 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010255 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010256 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010257
Willy Tarreaud53e2422012-04-16 17:21:11 +020010258 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010259}
10260
Cyril Bonté69fa9922012-10-25 00:01:06 +020010261/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10262 * and an optional one of type int to designate a specific occurrence.
10263 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010264 */
10265static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010266smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010267{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010268 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010269
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010270 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010271 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010272 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010273 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010274 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010275 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010276 if (smp->data.u.str.len < temp->size - 1) {
10277 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10278 temp->str[smp->data.u.str.len] = '\0';
10279 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010280 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010281 break;
10282 }
10283 }
10284 }
10285
Willy Tarreaud53e2422012-04-16 17:21:11 +020010286 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010287 if (!(smp->flags & SMP_F_NOT_LAST))
10288 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010289 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010290 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010291}
10292
Willy Tarreau737b0c12007-06-10 21:28:46 +020010293/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10294 * the first '/' after the possible hostname, and ends before the possible '?'.
10295 */
10296static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010297smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010298{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010299 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010300 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010301
Willy Tarreauc0239e02012-04-16 14:42:55 +020010302 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010303
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010304 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010305 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010306 ptr = http_get_path(txn);
10307 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010308 return 0;
10309
10310 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010311 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010312 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010313
10314 while (ptr < end && *ptr != '?')
10315 ptr++;
10316
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010317 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010318 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010319 return 1;
10320}
10321
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010322/* This produces a concatenation of the first occurrence of the Host header
10323 * followed by the path component if it begins with a slash ('/'). This means
10324 * that '*' will not be added, resulting in exactly the first Host entry.
10325 * If no Host header is found, then the path is returned as-is. The returned
10326 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010327 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010328 */
10329static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010330smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010331{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010332 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010333 char *ptr, *end, *beg;
10334 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010335 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010336
10337 CHECK_HTTP_MESSAGE_FIRST();
10338
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010339 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010340 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010341 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010342 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010343
10344 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010345 temp = get_trash_chunk();
10346 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010347 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010348 smp->data.u.str.str = temp->str;
10349 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010350
10351 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010352 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010353 beg = http_get_path(txn);
10354 if (!beg)
10355 beg = end;
10356
10357 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10358
10359 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010360 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10361 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010362 }
10363
10364 smp->flags = SMP_F_VOL_1ST;
10365 return 1;
10366}
10367
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010368/* This produces a 32-bit hash of the concatenation of the first occurrence of
10369 * the Host header followed by the path component if it begins with a slash ('/').
10370 * This means that '*' will not be added, resulting in exactly the first Host
10371 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010372 * is hashed using the path hash followed by a full avalanche hash and provides a
10373 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010374 * high-traffic sites without having to store whole paths.
10375 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010376int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010377smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010378{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010379 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010380 struct hdr_ctx ctx;
10381 unsigned int hash = 0;
10382 char *ptr, *beg, *end;
10383 int len;
10384
10385 CHECK_HTTP_MESSAGE_FIRST();
10386
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010387 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010388 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010389 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010390 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10391 ptr = ctx.line + ctx.val;
10392 len = ctx.vlen;
10393 while (len--)
10394 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10395 }
10396
10397 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010398 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010399 beg = http_get_path(txn);
10400 if (!beg)
10401 beg = end;
10402
10403 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10404
10405 if (beg < ptr && *beg == '/') {
10406 while (beg < ptr)
10407 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10408 }
10409 hash = full_hash(hash);
10410
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010411 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010412 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010413 smp->flags = SMP_F_VOL_1ST;
10414 return 1;
10415}
10416
Willy Tarreau4a550602012-12-09 14:53:32 +010010417/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010418 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10419 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10420 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010421 * that in environments where IPv6 is insignificant, truncating the output to
10422 * 8 bytes would still work.
10423 */
10424static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010425smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010426{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010427 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010428 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010429
10430 if (!cli_conn)
10431 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010432
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010433 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010434 return 0;
10435
Willy Tarreau47ca5452012-12-23 20:22:19 +010010436 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010437 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010438 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010439
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010440 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010441 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010442 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010443 temp->len += 4;
10444 break;
10445 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010446 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010447 temp->len += 16;
10448 break;
10449 default:
10450 return 0;
10451 }
10452
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010453 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010454 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010455 return 1;
10456}
10457
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010458/* Extracts the query string, which comes after the question mark '?'. If no
10459 * question mark is found, nothing is returned. Otherwise it returns a sample
10460 * of type string carrying the whole query string.
10461 */
10462static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010463smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010464{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010465 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010466 char *ptr, *end;
10467
10468 CHECK_HTTP_MESSAGE_FIRST();
10469
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010470 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010471 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10472 end = ptr + txn->req.sl.rq.u_l;
10473
10474 /* look up the '?' */
10475 do {
10476 if (ptr == end)
10477 return 0;
10478 } while (*ptr++ != '?');
10479
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010480 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010481 smp->data.u.str.str = ptr;
10482 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010483 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10484 return 1;
10485}
10486
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010487static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010488smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010489{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010490 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10491 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10492 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010493
Willy Tarreau24e32d82012-04-23 23:55:44 +020010494 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010495
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010496 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010497 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010498 return 1;
10499}
10500
Willy Tarreau7f18e522010-10-22 20:04:13 +020010501/* return a valid test if the current request is the first one on the connection */
10502static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010503smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010504{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010505 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010506 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010507 return 1;
10508}
10509
Willy Tarreau34db1082012-04-19 17:16:54 +020010510/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010511static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010512smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010513{
10514
Willy Tarreau24e32d82012-04-23 23:55:44 +020010515 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010516 return 0;
10517
Willy Tarreauc0239e02012-04-16 14:42:55 +020010518 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010519
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010520 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010521 return 0;
10522
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010523 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010524 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010525 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010526 return 1;
10527}
Willy Tarreau8797c062007-05-07 00:55:35 +020010528
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010529/* Accepts exactly 1 argument of type userlist */
10530static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010531smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010532{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010533 if (!args || args->type != ARGT_USR)
10534 return 0;
10535
10536 CHECK_HTTP_MESSAGE_FIRST();
10537
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010538 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010539 return 0;
10540
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010541 /* if the user does not belong to the userlist or has a wrong password,
10542 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010543 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010544 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010545 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10546 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010547 return 0;
10548
10549 /* pat_match_auth() will need the user list */
10550 smp->ctx.a[0] = args->data.usr;
10551
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010552 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010553 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010554 smp->data.u.str.str = smp->strm->txn->auth.user;
10555 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010556
10557 return 1;
10558}
10559
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010560/* Try to find the next occurrence of a cookie name in a cookie header value.
10561 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10562 * the cookie value is returned into *value and *value_l, and the function
10563 * returns a pointer to the next pointer to search from if the value was found.
10564 * Otherwise if the cookie was not found, NULL is returned and neither value
10565 * nor value_l are touched. The input <hdr> string should first point to the
10566 * header's value, and the <hdr_end> pointer must point to the first character
10567 * not part of the value. <list> must be non-zero if value may represent a list
10568 * of values (cookie headers). This makes it faster to abort parsing when no
10569 * list is expected.
10570 */
David Carlier4686f792015-09-25 14:10:50 +010010571char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010572extract_cookie_value(char *hdr, const char *hdr_end,
10573 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010574 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010575{
10576 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10577 char *next;
10578
10579 /* we search at least a cookie name followed by an equal, and more
10580 * generally something like this :
10581 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10582 */
10583 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10584 /* Iterate through all cookies on this line */
10585
Willy Tarreau2235b262016-11-05 15:50:20 +010010586 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010587 att_beg++;
10588
10589 /* find att_end : this is the first character after the last non
10590 * space before the equal. It may be equal to hdr_end.
10591 */
10592 equal = att_end = att_beg;
10593
10594 while (equal < hdr_end) {
10595 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10596 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010597 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010598 continue;
10599 att_end = equal;
10600 }
10601
10602 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10603 * is between <att_beg> and <equal>, both may be identical.
10604 */
10605
10606 /* look for end of cookie if there is an equal sign */
10607 if (equal < hdr_end && *equal == '=') {
10608 /* look for the beginning of the value */
10609 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010610 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010611 val_beg++;
10612
10613 /* find the end of the value, respecting quotes */
10614 next = find_cookie_value_end(val_beg, hdr_end);
10615
10616 /* make val_end point to the first white space or delimitor after the value */
10617 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010618 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010619 val_end--;
10620 } else {
10621 val_beg = val_end = next = equal;
10622 }
10623
10624 /* We have nothing to do with attributes beginning with '$'. However,
10625 * they will automatically be removed if a header before them is removed,
10626 * since they're supposed to be linked together.
10627 */
10628 if (*att_beg == '$')
10629 continue;
10630
10631 /* Ignore cookies with no equal sign */
10632 if (equal == next)
10633 continue;
10634
10635 /* Now we have the cookie name between att_beg and att_end, and
10636 * its value between val_beg and val_end.
10637 */
10638
10639 if (att_end - att_beg == cookie_name_l &&
10640 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10641 /* let's return this value and indicate where to go on from */
10642 *value = val_beg;
10643 *value_l = val_end - val_beg;
10644 return next + 1;
10645 }
10646
10647 /* Set-Cookie headers only have the name in the first attr=value part */
10648 if (!list)
10649 break;
10650 }
10651
10652 return NULL;
10653}
10654
William Lallemanda43ba4e2014-01-28 18:14:25 +010010655/* Fetch a captured HTTP request header. The index is the position of
10656 * the "capture" option in the configuration file
10657 */
10658static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010659smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010660{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010661 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010662 int idx;
10663
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010664 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010665 return 0;
10666
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010667 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010668
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010669 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 +010010670 return 0;
10671
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010672 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010673 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010674 smp->data.u.str.str = smp->strm->req_cap[idx];
10675 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010676
10677 return 1;
10678}
10679
10680/* Fetch a captured HTTP response header. The index is the position of
10681 * the "capture" option in the configuration file
10682 */
10683static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010684smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010685{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010686 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010687 int idx;
10688
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010689 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010690 return 0;
10691
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010692 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010693
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010694 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 +010010695 return 0;
10696
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010697 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010698 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010699 smp->data.u.str.str = smp->strm->res_cap[idx];
10700 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010701
10702 return 1;
10703}
10704
William Lallemand65ad6e12014-01-31 15:08:02 +010010705/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10706static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010707smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010708{
10709 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010710 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010711 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010712
Willy Tarreau15e91e12015-04-04 00:52:09 +020010713 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010714 return 0;
10715
William Lallemand96a77852014-02-05 00:30:02 +010010716 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010717
William Lallemand96a77852014-02-05 00:30:02 +010010718 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10719 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010720
William Lallemand96a77852014-02-05 00:30:02 +010010721 temp = get_trash_chunk();
10722 temp->str = txn->uri;
10723 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010724 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010725 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010726 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010727
10728 return 1;
10729
10730}
10731
10732/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10733static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010734smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010735{
10736 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010737 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010738 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010739
Willy Tarreau15e91e12015-04-04 00:52:09 +020010740 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010741 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010742
William Lallemand65ad6e12014-01-31 15:08:02 +010010743 ptr = txn->uri;
10744
10745 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10746 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010747
William Lallemand65ad6e12014-01-31 15:08:02 +010010748 if (!*ptr)
10749 return 0;
10750
10751 ptr++; /* skip the space */
10752
10753 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010754 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010755 if (!ptr)
10756 return 0;
10757 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10758 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010759
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010760 smp->data.u.str = *temp;
10761 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010762 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010763 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010764
10765 return 1;
10766}
10767
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010768/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10769 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10770 */
10771static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010772smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010773{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010774 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010775
Willy Tarreau15e91e12015-04-04 00:52:09 +020010776 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010777 return 0;
10778
10779 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010780 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010781 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010782 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010783
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010784 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010785 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010786 smp->flags = SMP_F_CONST;
10787 return 1;
10788
10789}
10790
10791/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10792 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10793 */
10794static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010795smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010796{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010797 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010798
Willy Tarreau15e91e12015-04-04 00:52:09 +020010799 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010800 return 0;
10801
10802 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010803 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010804 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010805 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010806
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010807 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010808 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010809 smp->flags = SMP_F_CONST;
10810 return 1;
10811
10812}
10813
William Lallemand65ad6e12014-01-31 15:08:02 +010010814
Willy Tarreaue333ec92012-04-16 16:26:40 +020010815/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010816 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010817 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010818 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010819 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010820 * Accepts exactly 1 argument of type string. If the input options indicate
10821 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010822 * The returned sample is of type CSTR. Can be used to parse cookies in other
10823 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010824 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010825int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010826{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010827 struct http_txn *txn;
10828 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010829 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010830 const struct http_msg *msg;
10831 const char *hdr_name;
10832 int hdr_name_len;
10833 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010834 int occ = 0;
10835 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010836
Willy Tarreau24e32d82012-04-23 23:55:44 +020010837 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010838 return 0;
10839
Willy Tarreaua890d072013-04-02 12:01:06 +020010840 if (!ctx) {
10841 /* first call */
10842 ctx = &static_hdr_ctx;
10843 ctx->idx = 0;
10844 smp->ctx.a[2] = ctx;
10845 }
10846
Willy Tarreaue333ec92012-04-16 16:26:40 +020010847 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010848
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010849 txn = smp->strm->txn;
10850 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010851
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010852 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010853 msg = &txn->req;
10854 hdr_name = "Cookie";
10855 hdr_name_len = 6;
10856 } else {
10857 msg = &txn->rsp;
10858 hdr_name = "Set-Cookie";
10859 hdr_name_len = 10;
10860 }
10861
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010862 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010863 /* no explicit occurrence and single fetch => last cookie by default */
10864 occ = -1;
10865
10866 /* OK so basically here, either we want only one value and it's the
10867 * last one, or we want to iterate over all of them and we fetch the
10868 * next one.
10869 */
10870
Willy Tarreau9b28e032012-10-12 23:49:43 +020010871 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010872 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010873 /* search for the header from the beginning, we must first initialize
10874 * the search parameters.
10875 */
Willy Tarreau37406352012-04-23 16:16:37 +020010876 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010877 ctx->idx = 0;
10878 }
10879
Willy Tarreau28376d62012-04-26 21:26:10 +020010880 smp->flags |= SMP_F_VOL_HDR;
10881
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010882 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010883 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10884 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010885 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10886 goto out;
10887
Willy Tarreau24e32d82012-04-23 23:55:44 +020010888 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010889 continue;
10890
Willy Tarreau37406352012-04-23 16:16:37 +020010891 smp->ctx.a[0] = ctx->line + ctx->val;
10892 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010893 }
10894
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010895 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010896 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010897 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010898 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010899 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010900 &smp->data.u.str.str,
10901 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010902 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010903 found = 1;
10904 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010905 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010906 smp->flags |= SMP_F_NOT_LAST;
10907 return 1;
10908 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010909 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010910 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010911 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010912 /* all cookie headers and values were scanned. If we're looking for the
10913 * last occurrence, we may return it now.
10914 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010915 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010916 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010917 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010918}
10919
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010920/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010921 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010922 * multiple cookies may be parsed on the same line. The returned sample is of
10923 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010924 */
10925static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010926smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010927{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010928 struct http_txn *txn;
10929 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010930 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010931 const struct http_msg *msg;
10932 const char *hdr_name;
10933 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010934 int cnt;
10935 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010936 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010937
Willy Tarreau24e32d82012-04-23 23:55:44 +020010938 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010939 return 0;
10940
Willy Tarreaue333ec92012-04-16 16:26:40 +020010941 CHECK_HTTP_MESSAGE_FIRST();
10942
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010943 txn = smp->strm->txn;
10944 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010945
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010946 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010947 msg = &txn->req;
10948 hdr_name = "Cookie";
10949 hdr_name_len = 6;
10950 } else {
10951 msg = &txn->rsp;
10952 hdr_name = "Set-Cookie";
10953 hdr_name_len = 10;
10954 }
10955
Willy Tarreau9b28e032012-10-12 23:49:43 +020010956 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010957 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010958 ctx.idx = 0;
10959 cnt = 0;
10960
10961 while (1) {
10962 /* Note: val_beg == NULL every time we need to fetch a new header */
10963 if (!val_beg) {
10964 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10965 break;
10966
Willy Tarreau24e32d82012-04-23 23:55:44 +020010967 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010968 continue;
10969
10970 val_beg = ctx.line + ctx.val;
10971 val_end = val_beg + ctx.vlen;
10972 }
10973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010974 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010975 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010976 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010977 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010978 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010979 &smp->data.u.str.str,
10980 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010981 cnt++;
10982 }
10983 }
10984
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010985 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010986 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010987 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010988 return 1;
10989}
10990
Willy Tarreau51539362012-05-08 12:46:28 +020010991/* Fetch an cookie's integer value. The integer value is returned. It
10992 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10993 */
10994static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010995smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010996{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010997 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010998
10999 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011000 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011001 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011002 }
11003
11004 return ret;
11005}
11006
Willy Tarreau8797c062007-05-07 00:55:35 +020011007/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011008/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011009/************************************************************************/
11010
David Cournapeau16023ee2010-12-23 20:55:41 +090011011/*
11012 * Given a path string and its length, find the position of beginning of the
11013 * query string. Returns NULL if no query string is found in the path.
11014 *
11015 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11016 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011017 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011018 */
bedis4c75cca2012-10-05 08:38:24 +020011019static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011020{
11021 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011022
bedis4c75cca2012-10-05 08:38:24 +020011023 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011024 return p ? p + 1 : NULL;
11025}
11026
bedis4c75cca2012-10-05 08:38:24 +020011027static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011028{
bedis4c75cca2012-10-05 08:38:24 +020011029 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011030}
11031
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011032/* after increasing a pointer value, it can exceed the first buffer
11033 * size. This function transform the value of <ptr> according with
11034 * the expected position. <chunks> is an array of the one or two
11035 * avalaible chunks. The first value is the start of the first chunk,
11036 * the second value if the end+1 of the first chunks. The third value
11037 * is NULL or the start of the second chunk and the fourth value is
11038 * the end+1 of the second chunk. The function returns 1 if does a
11039 * wrap, else returns 0.
11040 */
11041static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11042{
11043 if (*ptr < chunks[1])
11044 return 0;
11045 if (!chunks[2])
11046 return 0;
11047 *ptr = chunks[2] + ( *ptr - chunks[1] );
11048 return 1;
11049}
11050
David Cournapeau16023ee2010-12-23 20:55:41 +090011051/*
11052 * Given a url parameter, find the starting position of the first occurence,
11053 * or NULL if the parameter is not found.
11054 *
11055 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11056 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011057 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011058 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011059 * or the second chunk. The caller must be check the position before using the
11060 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011061 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011062static const char *
11063find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011064 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011065 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011066{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011067 const char *pos, *last, *equal;
11068 const char **bufs = chunks;
11069 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011070
David Cournapeau16023ee2010-12-23 20:55:41 +090011071
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011072 pos = bufs[0];
11073 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011074 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011075 /* Check the equal. */
11076 equal = pos + url_param_name_l;
11077 if (fix_pointer_if_wrap(chunks, &equal)) {
11078 if (equal >= chunks[3])
11079 return NULL;
11080 } else {
11081 if (equal >= chunks[1])
11082 return NULL;
11083 }
11084 if (*equal == '=') {
11085 if (pos + url_param_name_l > last) {
11086 /* process wrap case, we detect a wrap. In this case, the
11087 * comparison is performed in two parts.
11088 */
11089
11090 /* This is the end, we dont have any other chunk. */
11091 if (bufs != chunks || !bufs[2])
11092 return NULL;
11093
11094 /* Compute the length of each part of the comparison. */
11095 l1 = last - pos;
11096 l2 = url_param_name_l - l1;
11097
11098 /* The second buffer is too short to contain the compared string. */
11099 if (bufs[2] + l2 > bufs[3])
11100 return NULL;
11101
11102 if (memcmp(pos, url_param_name, l1) == 0 &&
11103 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11104 return pos;
11105
11106 /* Perform wrapping and jump the string who fail the comparison. */
11107 bufs += 2;
11108 pos = bufs[0] + l2;
11109 last = bufs[1];
11110
11111 } else {
11112 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011113 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11114 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011115 pos += url_param_name_l + 1;
11116 if (fix_pointer_if_wrap(chunks, &pos))
11117 last = bufs[2];
11118 }
11119 }
11120
11121 while (1) {
11122 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011123 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011124 pos++;
11125 if (pos < last)
11126 break;
11127 /* process buffer wrapping. */
11128 if (bufs != chunks || !bufs[2])
11129 return NULL;
11130 bufs += 2;
11131 pos = bufs[0];
11132 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011133 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011134 pos++;
11135 }
11136 return NULL;
11137}
11138
11139/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011140 * Given a url parameter name and a query string, find the next value.
11141 * An empty url_param_name matches the first available parameter.
11142 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11143 * respectively provide a pointer to the value and its end.
11144 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011145 */
11146static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011147find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011148 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011149 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011150{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011151 const char *arg_start, *qs_end;
11152 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011153
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011154 arg_start = chunks[0];
11155 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011156 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011157 /* Looks for an argument name. */
11158 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011159 url_param_name, url_param_name_l,
11160 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011161 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011162 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011163 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011164 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011165 if (!arg_start)
11166 return 0;
11167
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011168 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011169 while (1) {
11170 /* looks for the first argument. */
11171 value_start = memchr(arg_start, '=', qs_end - arg_start);
11172 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011173 /* Check for wrapping. */
11174 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011175 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011176 chunks[2]) {
11177 arg_start = chunks[2];
11178 qs_end = chunks[3];
11179 continue;
11180 }
11181 return 0;
11182 }
11183 break;
11184 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011185 value_start++;
11186 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011187 else {
11188 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011189 value_start = arg_start + url_param_name_l + 1;
11190
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011191 /* Check for pointer wrapping. */
11192 if (fix_pointer_if_wrap(chunks, &value_start)) {
11193 /* Update the end pointer. */
11194 qs_end = chunks[3];
11195
11196 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011197 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011198 return 0;
11199 }
11200 }
11201
David Cournapeau16023ee2010-12-23 20:55:41 +090011202 value_end = value_start;
11203
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011204 while (1) {
11205 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11206 value_end++;
11207 if (value_end < qs_end)
11208 break;
11209 /* process buffer wrapping. */
11210 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011211 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011212 chunks[2]) {
11213 value_end = chunks[2];
11214 qs_end = chunks[3];
11215 continue;
11216 }
11217 break;
11218 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011219
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011220 *vstart = value_start;
11221 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011222 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011223}
11224
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011225/* This scans a URL-encoded query string. It takes an optionally wrapping
11226 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11227 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11228 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011229 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011230static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011231smp_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 +090011232{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011233 const char *vstart, *vend;
11234 struct chunk *temp;
11235 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011236
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011237 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011238 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011239 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011240 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011241 return 0;
11242
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011243 /* Create sample. If the value is contiguous, return the pointer as CONST,
11244 * if the value is wrapped, copy-it in a buffer.
11245 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011246 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011247 if (chunks[2] &&
11248 vstart >= chunks[0] && vstart <= chunks[1] &&
11249 vend >= chunks[2] && vend <= chunks[3]) {
11250 /* Wrapped case. */
11251 temp = get_trash_chunk();
11252 memcpy(temp->str, vstart, chunks[1] - vstart);
11253 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011254 smp->data.u.str.str = temp->str;
11255 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011256 } else {
11257 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011258 smp->data.u.str.str = (char *)vstart;
11259 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011260 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11261 }
11262
11263 /* Update context, check wrapping. */
11264 chunks[0] = vend;
11265 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11266 chunks[1] = chunks[3];
11267 chunks[2] = NULL;
11268 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011269
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011270 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011271 smp->flags |= SMP_F_NOT_LAST;
11272
David Cournapeau16023ee2010-12-23 20:55:41 +090011273 return 1;
11274}
11275
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011276/* This function iterates over each parameter of the query string. It uses
11277 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011278 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11279 * An optional parameter name is passed in args[0], otherwise any parameter is
11280 * considered. It supports an optional delimiter argument for the beginning of
11281 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011282 */
11283static int
11284smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11285{
11286 struct http_msg *msg;
11287 char delim = '?';
11288 const char *name;
11289 int name_len;
11290
Dragan Dosen26f77e52015-05-25 10:02:11 +020011291 if (!args ||
11292 (args[0].type && args[0].type != ARGT_STR) ||
11293 (args[1].type && args[1].type != ARGT_STR))
11294 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011295
Dragan Dosen26f77e52015-05-25 10:02:11 +020011296 name = "";
11297 name_len = 0;
11298 if (args->type == ARGT_STR) {
11299 name = args->data.str.str;
11300 name_len = args->data.str.len;
11301 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011302
Dragan Dosen26f77e52015-05-25 10:02:11 +020011303 if (args[1].type)
11304 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011305
Dragan Dosen26f77e52015-05-25 10:02:11 +020011306 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011307 CHECK_HTTP_MESSAGE_FIRST();
11308
11309 msg = &smp->strm->txn->req;
11310
11311 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11312 msg->sl.rq.u_l, delim);
11313 if (!smp->ctx.a[0])
11314 return 0;
11315
11316 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011317
11318 /* Assume that the context is filled with NULL pointer
11319 * before the first call.
11320 * smp->ctx.a[2] = NULL;
11321 * smp->ctx.a[3] = NULL;
11322 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011323 }
11324
11325 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11326}
11327
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011328/* This function iterates over each parameter of the body. This requires
11329 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011330 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11331 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11332 * optional second part if the body wraps at the end of the buffer. An optional
11333 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011334 */
11335static int
11336smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11337{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011338 struct http_msg *msg;
11339 unsigned long len;
11340 unsigned long block1;
11341 char *body;
11342 const char *name;
11343 int name_len;
11344
11345 if (!args || (args[0].type && args[0].type != ARGT_STR))
11346 return 0;
11347
11348 name = "";
11349 name_len = 0;
11350 if (args[0].type == ARGT_STR) {
11351 name = args[0].data.str.str;
11352 name_len = args[0].data.str.len;
11353 }
11354
11355 if (!smp->ctx.a[0]) { // first call, find the query string
11356 CHECK_HTTP_MESSAGE_FIRST();
11357
11358 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011359 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011360 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011361 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011362
11363 len = http_body_bytes(msg);
11364 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11365
11366 block1 = len;
11367 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11368 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11369
11370 if (block1 == len) {
11371 /* buffer is not wrapped (or empty) */
11372 smp->ctx.a[0] = body;
11373 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011374
11375 /* Assume that the context is filled with NULL pointer
11376 * before the first call.
11377 * smp->ctx.a[2] = NULL;
11378 * smp->ctx.a[3] = NULL;
11379 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011380 }
11381 else {
11382 /* buffer is wrapped, we need to defragment it */
11383 smp->ctx.a[0] = body;
11384 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011385 smp->ctx.a[2] = msg->chn->buf->data;
11386 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011387 }
11388 }
11389 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11390}
11391
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011392/* Return the signed integer value for the specified url parameter (see url_param
11393 * above).
11394 */
11395static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011396smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011397{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011398 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011399
11400 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011401 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011402 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011403 }
11404
11405 return ret;
11406}
11407
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011408/* This produces a 32-bit hash of the concatenation of the first occurrence of
11409 * the Host header followed by the path component if it begins with a slash ('/').
11410 * This means that '*' will not be added, resulting in exactly the first Host
11411 * entry. If no Host header is found, then the path is used. The resulting value
11412 * is hashed using the url hash followed by a full avalanche hash and provides a
11413 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11414 * high-traffic sites without having to store whole paths.
11415 * this differs from the base32 functions in that it includes the url parameters
11416 * as well as the path
11417 */
11418static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011419smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011420{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011421 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011422 struct hdr_ctx ctx;
11423 unsigned int hash = 0;
11424 char *ptr, *beg, *end;
11425 int len;
11426
11427 CHECK_HTTP_MESSAGE_FIRST();
11428
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011429 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011430 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011431 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011432 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11433 ptr = ctx.line + ctx.val;
11434 len = ctx.vlen;
11435 while (len--)
11436 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11437 }
11438
11439 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011440 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 +000011441 beg = http_get_path(txn);
11442 if (!beg)
11443 beg = end;
11444
11445 for (ptr = beg; ptr < end ; ptr++);
11446
11447 if (beg < ptr && *beg == '/') {
11448 while (beg < ptr)
11449 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11450 }
11451 hash = full_hash(hash);
11452
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011453 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011454 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011455 smp->flags = SMP_F_VOL_1ST;
11456 return 1;
11457}
11458
11459/* This concatenates the source address with the 32-bit hash of the Host and
11460 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11461 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11462 * on the source address length. The URL hash is stored before the address so
11463 * that in environments where IPv6 is insignificant, truncating the output to
11464 * 8 bytes would still work.
11465 */
11466static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011467smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011468{
11469 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011470 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011471
Dragan Dosendb5af612016-06-16 11:23:01 +020011472 if (!cli_conn)
11473 return 0;
11474
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011475 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011476 return 0;
11477
11478 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011479 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11480 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011481
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011482 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011483 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011484 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011485 temp->len += 4;
11486 break;
11487 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011488 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011489 temp->len += 16;
11490 break;
11491 default:
11492 return 0;
11493 }
11494
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011495 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011496 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011497 return 1;
11498}
11499
Willy Tarreau185b5c42012-04-26 15:11:51 +020011500/* This function is used to validate the arguments passed to any "hdr" fetch
11501 * keyword. These keywords support an optional positive or negative occurrence
11502 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11503 * is assumed that the types are already the correct ones. Returns 0 on error,
11504 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11505 * error message in case of error, that the caller is responsible for freeing.
11506 * The initial location must either be freeable or NULL.
11507 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011508int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011509{
11510 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011511 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011512 return 0;
11513 }
11514 return 1;
11515}
11516
Willy Tarreau276fae92013-07-25 14:36:01 +020011517/* takes an UINT value on input supposed to represent the time since EPOCH,
11518 * adds an optional offset found in args[0] and emits a string representing
11519 * the date in RFC-1123/5322 format.
11520 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011521static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011522{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011523 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011524 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11525 struct chunk *temp;
11526 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011527 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011528 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011529
11530 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011531 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011532 curr_date += args[0].data.sint;
11533
11534 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011535 if (!tm)
11536 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011537
11538 temp = get_trash_chunk();
11539 temp->len = snprintf(temp->str, temp->size - temp->len,
11540 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11541 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11542 tm->tm_hour, tm->tm_min, tm->tm_sec);
11543
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011544 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011545 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011546 return 1;
11547}
11548
Thierry FOURNIERad903512014-04-11 17:51:01 +020011549/* Match language range with language tag. RFC2616 14.4:
11550 *
11551 * A language-range matches a language-tag if it exactly equals
11552 * the tag, or if it exactly equals a prefix of the tag such
11553 * that the first tag character following the prefix is "-".
11554 *
11555 * Return 1 if the strings match, else return 0.
11556 */
11557static inline int language_range_match(const char *range, int range_len,
11558 const char *tag, int tag_len)
11559{
11560 const char *end = range + range_len;
11561 const char *tend = tag + tag_len;
11562 while (range < end) {
11563 if (*range == '-' && tag == tend)
11564 return 1;
11565 if (*range != *tag || tag == tend)
11566 return 0;
11567 range++;
11568 tag++;
11569 }
11570 /* Return true only if the last char of the tag is matched. */
11571 return tag == tend;
11572}
11573
11574/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011575static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011576{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011577 const char *al = smp->data.u.str.str;
11578 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011579 const char *token;
11580 int toklen;
11581 int qvalue;
11582 const char *str;
11583 const char *w;
11584 int best_q = 0;
11585
11586 /* Set the constant to the sample, because the output of the
11587 * function will be peek in the constant configuration string.
11588 */
11589 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011590 smp->data.u.str.size = 0;
11591 smp->data.u.str.str = "";
11592 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011593
11594 /* Parse the accept language */
11595 while (1) {
11596
11597 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011598 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011599 al++;
11600 if (al >= end)
11601 break;
11602
11603 /* Start of the fisrt word. */
11604 token = al;
11605
11606 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011607 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011608 al++;
11609 if (al == token)
11610 goto expect_comma;
11611
11612 /* Length of the token. */
11613 toklen = al - token;
11614 qvalue = 1000;
11615
11616 /* Check if the token exists in the list. If the token not exists,
11617 * jump to the next token.
11618 */
11619 str = args[0].data.str.str;
11620 w = str;
11621 while (1) {
11622 if (*str == ';' || *str == '\0') {
11623 if (language_range_match(token, toklen, w, str-w))
11624 goto look_for_q;
11625 if (*str == '\0')
11626 goto expect_comma;
11627 w = str + 1;
11628 }
11629 str++;
11630 }
11631 goto expect_comma;
11632
11633look_for_q:
11634
11635 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011636 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011637 al++;
11638 if (al >= end)
11639 goto process_value;
11640
11641 /* If ',' is found, process the result */
11642 if (*al == ',')
11643 goto process_value;
11644
11645 /* If the character is different from ';', look
11646 * for the end of the header part in best effort.
11647 */
11648 if (*al != ';')
11649 goto expect_comma;
11650
11651 /* Assumes that the char is ';', now expect "q=". */
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 'q'. If no 'q', continue in best effort */
11661 if (*al != 'q')
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 /* Expect '='. If no '=', continue in best effort */
11672 if (*al != '=')
11673 goto process_value;
11674 al++;
11675
11676 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011677 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011678 al++;
11679 if (al >= end)
11680 goto process_value;
11681
11682 /* Parse the q value. */
11683 qvalue = parse_qvalue(al, &al);
11684
11685process_value:
11686
11687 /* If the new q value is the best q value, then store the associated
11688 * language in the response. If qvalue is the biggest value (1000),
11689 * break the process.
11690 */
11691 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011692 smp->data.u.str.str = (char *)w;
11693 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011694 if (qvalue >= 1000)
11695 break;
11696 best_q = qvalue;
11697 }
11698
11699expect_comma:
11700
11701 /* Expect comma or end. If the end is detected, quit the loop. */
11702 while (al < end && *al != ',')
11703 al++;
11704 if (al >= end)
11705 break;
11706
11707 /* Comma is found, jump it and restart the analyzer. */
11708 al++;
11709 }
11710
11711 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011712 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11713 smp->data.u.str.str = args[1].data.str.str;
11714 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011715 }
11716
11717 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011718 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011719}
11720
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011721/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011722static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011723{
11724 /* If the constant flag is set or if not size is avalaible at
11725 * the end of the buffer, copy the string in other buffer
11726 * before decoding.
11727 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011728 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011729 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011730 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11731 smp->data.u.str.str = str->str;
11732 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011733 smp->flags &= ~SMP_F_CONST;
11734 }
11735
11736 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011737 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11738 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011739 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011740}
11741
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011742static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11743{
11744 struct proxy *fe = strm_fe(smp->strm);
11745 int idx, i;
11746 struct cap_hdr *hdr;
11747 int len;
11748
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011749 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011750 return 0;
11751
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011752 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011753
11754 /* Check the availibity of the capture id. */
11755 if (idx > fe->nb_req_cap - 1)
11756 return 0;
11757
11758 /* Look for the original configuration. */
11759 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11760 hdr != NULL && i != idx ;
11761 i--, hdr = hdr->next);
11762 if (!hdr)
11763 return 0;
11764
11765 /* check for the memory allocation */
11766 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011767 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011768 if (smp->strm->req_cap[hdr->index] == NULL)
11769 return 0;
11770
11771 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011772 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011773 if (len > hdr->len)
11774 len = hdr->len;
11775
11776 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011777 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011778 smp->strm->req_cap[idx][len] = '\0';
11779
11780 return 1;
11781}
11782
11783static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11784{
11785 struct proxy *fe = strm_fe(smp->strm);
11786 int idx, i;
11787 struct cap_hdr *hdr;
11788 int len;
11789
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011790 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011791 return 0;
11792
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011793 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011794
11795 /* Check the availibity of the capture id. */
11796 if (idx > fe->nb_rsp_cap - 1)
11797 return 0;
11798
11799 /* Look for the original configuration. */
11800 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11801 hdr != NULL && i != idx ;
11802 i--, hdr = hdr->next);
11803 if (!hdr)
11804 return 0;
11805
11806 /* check for the memory allocation */
11807 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011808 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011809 if (smp->strm->res_cap[hdr->index] == NULL)
11810 return 0;
11811
11812 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011813 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011814 if (len > hdr->len)
11815 len = hdr->len;
11816
11817 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011818 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011819 smp->strm->res_cap[idx][len] = '\0';
11820
11821 return 1;
11822}
11823
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011824/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011825 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011826 * the relevant part of the request line accordingly. Then it updates various
11827 * pointers to the next elements which were moved, and the total buffer length.
11828 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011829 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11830 * error, though this can be revisited when this code is finally exploited.
11831 *
11832 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11833 * query string and 3 to replace uri.
11834 *
11835 * In query string case, the mark question '?' must be set at the start of the
11836 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011837 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011838int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011839 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011840{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011841 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011842 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011843 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011844 int delta;
11845
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011846 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011847 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011848 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011849 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11850
11851 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011852 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011853 txn->req.sl.rq.m_l += delta;
11854 txn->req.sl.rq.u += delta;
11855 txn->req.sl.rq.v += delta;
11856 break;
11857
11858 case 1: // path
11859 cur_ptr = http_get_path(txn);
11860 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011861 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011862
11863 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011864 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 +010011865 cur_end++;
11866
11867 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011868 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011869 txn->req.sl.rq.u_l += delta;
11870 txn->req.sl.rq.v += delta;
11871 break;
11872
11873 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011874 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011875 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011876 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11877 while (cur_ptr < cur_end && *cur_ptr != '?')
11878 cur_ptr++;
11879
11880 /* skip the question mark or indicate that we must insert it
11881 * (but only if the format string is not empty then).
11882 */
11883 if (cur_ptr < cur_end)
11884 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011885 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011886 offset = 0;
11887
11888 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011889 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011890 txn->req.sl.rq.u_l += delta;
11891 txn->req.sl.rq.v += delta;
11892 break;
11893
11894 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011895 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011896 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11897
11898 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011899 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011900 txn->req.sl.rq.u_l += delta;
11901 txn->req.sl.rq.v += delta;
11902 break;
11903
11904 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011905 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011906 }
11907
11908 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011909 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011910 txn->req.sl.rq.l += delta;
11911 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011912 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011913 return 0;
11914}
11915
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011916/* This function replace the HTTP status code and the associated message. The
11917 * variable <status> contains the new status code. This function never fails.
11918 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011919void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011920{
11921 struct http_txn *txn = s->txn;
11922 char *cur_ptr, *cur_end;
11923 int delta;
11924 char *res;
11925 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011926 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011927 int msg_len;
11928
11929 chunk_reset(&trash);
11930
11931 res = ultoa_o(status, trash.str, trash.size);
11932 c_l = res - trash.str;
11933
11934 trash.str[c_l] = ' ';
11935 trash.len = c_l + 1;
11936
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011937 /* Do we have a custom reason format string? */
11938 if (msg == NULL)
11939 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011940 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011941 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11942 trash.len += msg_len;
11943
11944 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11945 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11946
11947 /* commit changes and adjust message */
11948 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11949
11950 /* adjust res line offsets and lengths */
11951 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11952 txn->rsp.sl.st.c_l = c_l;
11953 txn->rsp.sl.st.r_l = msg_len;
11954
11955 delta = trash.len - (cur_end - cur_ptr);
11956 txn->rsp.sl.st.l += delta;
11957 txn->hdr_idx.v[0].len += delta;
11958 http_msg_move_end(&txn->rsp, delta);
11959}
11960
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011961/* This function executes one of the set-{method,path,query,uri} actions. It
11962 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011963 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011964 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011965 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11966 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011967 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011968enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011969 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011970{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011971 struct chunk *replace;
11972 enum act_return ret = ACT_RET_ERR;
11973
11974 replace = alloc_trash_chunk();
11975 if (!replace)
11976 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011977
11978 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011979 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011980 replace->str[replace->len++] = '?';
11981 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11982 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011983
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011984 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11985
11986 ret = ACT_RET_CONT;
11987
11988leave:
11989 free_trash_chunk(replace);
11990 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011991}
11992
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011993/* This function is just a compliant action wrapper for "set-status". */
11994enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011995 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011996{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011997 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011998 return ACT_RET_CONT;
11999}
12000
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012001/* parse an http-request action among :
12002 * set-method
12003 * set-path
12004 * set-query
12005 * set-uri
12006 *
12007 * All of them accept a single argument of type string representing a log-format.
12008 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12009 * 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 +020012010 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012011 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012012enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12013 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012014{
12015 int cur_arg = *orig_arg;
12016
Thierry FOURNIER42148732015-09-02 17:17:33 +020012017 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012018
12019 switch (args[0][4]) {
12020 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012021 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012022 rule->action_ptr = http_action_set_req_line;
12023 break;
12024 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012025 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012026 rule->action_ptr = http_action_set_req_line;
12027 break;
12028 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012029 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012030 rule->action_ptr = http_action_set_req_line;
12031 break;
12032 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012033 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012034 rule->action_ptr = http_action_set_req_line;
12035 break;
12036 default:
12037 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012038 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012039 }
12040
12041 if (!*args[cur_arg] ||
12042 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12043 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012044 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012045 }
12046
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012047 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012048 px->conf.args.ctx = ARGC_HRQ;
12049 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12050 (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 +010012051 return ACT_RET_PRS_ERR;
12052 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012053
12054 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012055 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012056}
12057
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012058/* parse set-status action:
12059 * This action accepts a single argument of type int representing
12060 * an http status code. It returns ACT_RET_PRS_OK on success,
12061 * ACT_RET_PRS_ERR on error.
12062 */
12063enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12064 struct act_rule *rule, char **err)
12065{
12066 char *error;
12067
Thierry FOURNIER42148732015-09-02 17:17:33 +020012068 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012069 rule->action_ptr = action_http_set_status;
12070
12071 /* Check if an argument is available */
12072 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012073 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012074 return ACT_RET_PRS_ERR;
12075 }
12076
12077 /* convert status code as integer */
12078 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12079 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12080 memprintf(err, "expects an integer status code between 100 and 999");
12081 return ACT_RET_PRS_ERR;
12082 }
12083
12084 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012085
12086 /* set custom reason string */
12087 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12088 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12089 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12090 (*orig_arg)++;
12091 rule->arg.status.reason = strdup(args[*orig_arg]);
12092 (*orig_arg)++;
12093 }
12094
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012095 return ACT_RET_PRS_OK;
12096}
12097
Willy Tarreau53275e82017-11-24 07:52:01 +010012098/* This function executes the "reject" HTTP action. It clears the request and
12099 * response buffer without sending any response. It can be useful as an HTTP
12100 * alternative to the silent-drop action to defend against DoS attacks, and may
12101 * also be used with HTTP/2 to close a connection instead of just a stream.
12102 * The txn status is unchanged, indicating no response was sent. The termination
12103 * flags will indicate "PR". It always returns ACT_RET_STOP.
12104 */
12105enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12106 struct session *sess, struct stream *s, int flags)
12107{
12108 channel_abort(&s->req);
12109 channel_abort(&s->res);
12110 s->req.analysers = 0;
12111 s->res.analysers = 0;
12112
12113 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12114 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12115 if (sess->listener && sess->listener->counters)
12116 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12117
12118 if (!(s->flags & SF_ERR_MASK))
12119 s->flags |= SF_ERR_PRXCOND;
12120 if (!(s->flags & SF_FINST_MASK))
12121 s->flags |= SF_FINST_R;
12122
12123 return ACT_RET_CONT;
12124}
12125
12126/* parse the "reject" action:
12127 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12128 * ACT_RET_PRS_ERR on error.
12129 */
12130enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12131 struct act_rule *rule, char **err)
12132{
12133 rule->action = ACT_CUSTOM;
12134 rule->action_ptr = http_action_reject;
12135 return ACT_RET_PRS_OK;
12136}
12137
Willy Tarreaua9083d02015-05-08 15:27:59 +020012138/* This function executes the "capture" action. It executes a fetch expression,
12139 * turns the result into a string and puts it in a capture slot. It always
12140 * returns 1. If an error occurs the action is cancelled, but the rule
12141 * processing continues.
12142 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012143enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012144 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012145{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012146 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012147 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012148 char **cap = s->req_cap;
12149 int len;
12150
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012151 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 +020012152 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012153 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012154
12155 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012156 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012157
12158 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012159 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012160
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012161 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012162 if (len > h->len)
12163 len = h->len;
12164
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012165 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012166 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012167 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012168}
12169
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012170/* This function executes the "capture" action and store the result in a
12171 * capture slot if exists. It executes a fetch expression, turns the result
12172 * into a string and puts it in a capture slot. It always returns 1. If an
12173 * error occurs the action is cancelled, but the rule processing continues.
12174 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012175enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012176 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012177{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012178 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012179 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012180 char **cap = s->req_cap;
12181 struct proxy *fe = strm_fe(s);
12182 int len;
12183 int i;
12184
12185 /* Look for the original configuration. */
12186 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012187 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012188 i--, h = h->next);
12189 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012190 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012191
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012192 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 +020012193 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012194 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012195
12196 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012197 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012198
12199 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012200 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012201
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012202 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012203 if (len > h->len)
12204 len = h->len;
12205
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012206 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012207 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012208 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012209}
12210
Christopher Faulet29730ba2017-09-18 15:26:32 +020012211/* Check an "http-request capture" action.
12212 *
12213 * The function returns 1 in success case, otherwise, it returns 0 and err is
12214 * filled.
12215 */
12216int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12217{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012218 if (rule->action_ptr != http_action_req_capture_by_id)
12219 return 1;
12220
Christopher Faulet29730ba2017-09-18 15:26:32 +020012221 if (rule->arg.capid.idx >= px->nb_req_cap) {
12222 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12223 rule->arg.capid.idx);
12224 return 0;
12225 }
12226
12227 return 1;
12228}
12229
Willy Tarreaua9083d02015-05-08 15:27:59 +020012230/* parse an "http-request capture" action. It takes a single argument which is
12231 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012232 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012233 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012234 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012235enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12236 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012237{
12238 struct sample_expr *expr;
12239 struct cap_hdr *hdr;
12240 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012241 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012242
12243 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12244 if (strcmp(args[cur_arg], "if") == 0 ||
12245 strcmp(args[cur_arg], "unless") == 0)
12246 break;
12247
12248 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012249 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012250 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012251 }
12252
Willy Tarreaua9083d02015-05-08 15:27:59 +020012253 cur_arg = *orig_arg;
12254 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12255 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012256 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012257
12258 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12259 memprintf(err,
12260 "fetch method '%s' extracts information from '%s', none of which is available here",
12261 args[cur_arg-1], sample_src_names(expr->fetch->use));
12262 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012263 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012264 }
12265
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012266 if (!args[cur_arg] || !*args[cur_arg]) {
12267 memprintf(err, "expects 'len or 'id'");
12268 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012269 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012270 }
12271
Willy Tarreaua9083d02015-05-08 15:27:59 +020012272 if (strcmp(args[cur_arg], "len") == 0) {
12273 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012274
12275 if (!(px->cap & PR_CAP_FE)) {
12276 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012277 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012278 }
12279
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012280 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012281
Willy Tarreaua9083d02015-05-08 15:27:59 +020012282 if (!args[cur_arg]) {
12283 memprintf(err, "missing length value");
12284 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012285 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012286 }
12287 /* we copy the table name for now, it will be resolved later */
12288 len = atoi(args[cur_arg]);
12289 if (len <= 0) {
12290 memprintf(err, "length must be > 0");
12291 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012292 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012293 }
12294 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012295
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012296 if (!len) {
12297 memprintf(err, "a positive 'len' argument is mandatory");
12298 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012299 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012300 }
12301
Vincent Bernat02779b62016-04-03 13:48:43 +020012302 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303 hdr->next = px->req_cap;
12304 hdr->name = NULL; /* not a header capture */
12305 hdr->namelen = 0;
12306 hdr->len = len;
12307 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12308 hdr->index = px->nb_req_cap++;
12309
12310 px->req_cap = hdr;
12311 px->to_log |= LW_REQHDR;
12312
Thierry FOURNIER42148732015-09-02 17:17:33 +020012313 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012314 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012315 rule->arg.cap.expr = expr;
12316 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012317 }
12318
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012319 else if (strcmp(args[cur_arg], "id") == 0) {
12320 int id;
12321 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012322
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012323 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012324
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012325 if (!args[cur_arg]) {
12326 memprintf(err, "missing id value");
12327 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012328 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012329 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012330
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012331 id = strtol(args[cur_arg], &error, 10);
12332 if (*error != '\0') {
12333 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12334 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012335 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012336 }
12337 cur_arg++;
12338
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012339 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012340
Thierry FOURNIER42148732015-09-02 17:17:33 +020012341 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012342 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012343 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012344 rule->arg.capid.expr = expr;
12345 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012346 }
12347
12348 else {
12349 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12350 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012351 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012352 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012353
12354 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012355 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012356}
12357
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012358/* This function executes the "capture" action and store the result in a
12359 * capture slot if exists. It executes a fetch expression, turns the result
12360 * into a string and puts it in a capture slot. It always returns 1. If an
12361 * error occurs the action is cancelled, but the rule processing continues.
12362 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012363enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012364 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012365{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012366 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012367 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012368 char **cap = s->res_cap;
12369 struct proxy *fe = strm_fe(s);
12370 int len;
12371 int i;
12372
12373 /* Look for the original configuration. */
12374 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012375 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012376 i--, h = h->next);
12377 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012378 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012379
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012380 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 +020012381 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012382 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012383
12384 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012385 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012386
12387 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012388 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012389
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012390 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012391 if (len > h->len)
12392 len = h->len;
12393
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012394 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012395 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012396 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012397}
12398
Christopher Faulet29730ba2017-09-18 15:26:32 +020012399/* Check an "http-response capture" action.
12400 *
12401 * The function returns 1 in success case, otherwise, it returns 0 and err is
12402 * filled.
12403 */
12404int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12405{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012406 if (rule->action_ptr != http_action_res_capture_by_id)
12407 return 1;
12408
Christopher Faulet29730ba2017-09-18 15:26:32 +020012409 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12410 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12411 rule->arg.capid.idx);
12412 return 0;
12413 }
12414
12415 return 1;
12416}
12417
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012418/* parse an "http-response capture" action. It takes a single argument which is
12419 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12420 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012421 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012422 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012423enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12424 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012425{
12426 struct sample_expr *expr;
12427 int cur_arg;
12428 int id;
12429 char *error;
12430
12431 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12432 if (strcmp(args[cur_arg], "if") == 0 ||
12433 strcmp(args[cur_arg], "unless") == 0)
12434 break;
12435
12436 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012437 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012438 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012439 }
12440
12441 cur_arg = *orig_arg;
12442 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12443 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012444 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012445
12446 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12447 memprintf(err,
12448 "fetch method '%s' extracts information from '%s', none of which is available here",
12449 args[cur_arg-1], sample_src_names(expr->fetch->use));
12450 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012451 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012452 }
12453
12454 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012455 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012456 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012457 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012458 }
12459
12460 if (strcmp(args[cur_arg], "id") != 0) {
12461 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12462 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012463 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012464 }
12465
12466 cur_arg++;
12467
12468 if (!args[cur_arg]) {
12469 memprintf(err, "missing id value");
12470 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012471 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012472 }
12473
12474 id = strtol(args[cur_arg], &error, 10);
12475 if (*error != '\0') {
12476 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12477 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012478 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012479 }
12480 cur_arg++;
12481
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012482 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012483
Thierry FOURNIER42148732015-09-02 17:17:33 +020012484 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012485 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012486 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012487 rule->arg.capid.expr = expr;
12488 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012489
12490 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012491 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012492}
12493
William Lallemand73025dd2014-04-24 14:38:37 +020012494/*
12495 * Return the struct http_req_action_kw associated to a keyword.
12496 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012497struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012498{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012499 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012500}
12501
12502/*
12503 * Return the struct http_res_action_kw associated to a keyword.
12504 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012505struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012506{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012507 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012508}
12509
Willy Tarreau12207b32016-11-22 19:48:51 +010012510
12511/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12512 * now.
12513 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012514static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau12207b32016-11-22 19:48:51 +010012515{
12516 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12517 return 1;
12518
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012519 if (*args[2]) {
12520 struct proxy *px;
12521
12522 px = proxy_find_by_name(args[2], 0, 0);
12523 if (px)
12524 appctx->ctx.errors.iid = px->uuid;
12525 else
12526 appctx->ctx.errors.iid = atoi(args[2]);
12527
12528 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012529 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012530 appctx->ctx.cli.msg = "No such proxy.\n";
12531 appctx->st0 = CLI_ST_PRINT;
12532 return 1;
12533 }
12534 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012535 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012536 appctx->ctx.errors.iid = -1; // dump all proxies
12537
Willy Tarreau35069f82016-11-25 09:16:37 +010012538 appctx->ctx.errors.flag = 0;
12539 if (strcmp(args[3], "request") == 0)
12540 appctx->ctx.errors.flag |= 4; // ignore response
12541 else if (strcmp(args[3], "response") == 0)
12542 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012543 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012544 return 0;
12545}
12546
12547/* This function dumps all captured errors onto the stream interface's
12548 * read buffer. It returns 0 if the output buffer is full and it needs
12549 * to be called again, otherwise non-zero.
12550 */
12551static int cli_io_handler_show_errors(struct appctx *appctx)
12552{
12553 struct stream_interface *si = appctx->owner;
12554 extern const char *monthname[12];
12555
12556 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12557 return 1;
12558
12559 chunk_reset(&trash);
12560
12561 if (!appctx->ctx.errors.px) {
12562 /* the function had not been called yet, let's prepare the
12563 * buffer for a response.
12564 */
12565 struct tm tm;
12566
12567 get_localtime(date.tv_sec, &tm);
12568 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12569 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12570 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12571 error_snapshot_id);
12572
Willy Tarreau06d80a92017-10-19 14:32:15 +020012573 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012574 /* Socket buffer full. Let's try again later from the same point */
12575 si_applet_cant_put(si);
12576 return 0;
12577 }
12578
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012579 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012580 appctx->ctx.errors.bol = 0;
12581 appctx->ctx.errors.ptr = -1;
12582 }
12583
12584 /* we have two inner loops here, one for the proxy, the other one for
12585 * the buffer.
12586 */
12587 while (appctx->ctx.errors.px) {
12588 struct error_snapshot *es;
12589
Willy Tarreau35069f82016-11-25 09:16:37 +010012590 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012591 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012592 if (appctx->ctx.errors.flag & 2) // skip req
12593 goto next;
12594 }
12595 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012596 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012597 if (appctx->ctx.errors.flag & 4) // skip resp
12598 goto next;
12599 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012600
12601 if (!es->when.tv_sec)
12602 goto next;
12603
12604 if (appctx->ctx.errors.iid >= 0 &&
12605 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12606 es->oe->uuid != appctx->ctx.errors.iid)
12607 goto next;
12608
12609 if (appctx->ctx.errors.ptr < 0) {
12610 /* just print headers now */
12611
12612 char pn[INET6_ADDRSTRLEN];
12613 struct tm tm;
12614 int port;
12615
12616 get_localtime(es->when.tv_sec, &tm);
12617 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12618 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12619 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12620
12621 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12622 case AF_INET:
12623 case AF_INET6:
12624 port = get_host_port(&es->src);
12625 break;
12626 default:
12627 port = 0;
12628 }
12629
Willy Tarreau35069f82016-11-25 09:16:37 +010012630 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012631 case 0:
12632 chunk_appendf(&trash,
12633 " frontend %s (#%d): invalid request\n"
12634 " backend %s (#%d)",
12635 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12636 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12637 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12638 break;
12639 case 1:
12640 chunk_appendf(&trash,
12641 " backend %s (#%d): invalid response\n"
12642 " frontend %s (#%d)",
12643 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12644 es->oe->id, es->oe->uuid);
12645 break;
12646 }
12647
12648 chunk_appendf(&trash,
12649 ", server %s (#%d), event #%u\n"
12650 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012651 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012652 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12653 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12654 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12655 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12656 es->ev_id,
12657 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012658 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012659 es->m_clen, es->m_blen,
12660 es->b_flags, es->b_out, es->b_tot,
12661 es->len, es->b_wrap, es->pos);
12662
Willy Tarreau06d80a92017-10-19 14:32:15 +020012663 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012664 /* Socket buffer full. Let's try again later from the same point */
12665 si_applet_cant_put(si);
12666 return 0;
12667 }
12668 appctx->ctx.errors.ptr = 0;
12669 appctx->ctx.errors.sid = es->sid;
12670 }
12671
12672 if (appctx->ctx.errors.sid != es->sid) {
12673 /* the snapshot changed while we were dumping it */
12674 chunk_appendf(&trash,
12675 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012676 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012677 si_applet_cant_put(si);
12678 return 0;
12679 }
12680 goto next;
12681 }
12682
12683 /* OK, ptr >= 0, so we have to dump the current line */
12684 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12685 int newptr;
12686 int newline;
12687
12688 newline = appctx->ctx.errors.bol;
12689 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12690 if (newptr == appctx->ctx.errors.ptr)
12691 return 0;
12692
Willy Tarreau06d80a92017-10-19 14:32:15 +020012693 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012694 /* Socket buffer full. Let's try again later from the same point */
12695 si_applet_cant_put(si);
12696 return 0;
12697 }
12698 appctx->ctx.errors.ptr = newptr;
12699 appctx->ctx.errors.bol = newline;
12700 };
12701 next:
12702 appctx->ctx.errors.bol = 0;
12703 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012704 appctx->ctx.errors.flag ^= 1;
12705 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012706 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012707 }
12708
12709 /* dump complete */
12710 return 1;
12711}
12712
12713/* register cli keywords */
12714static struct cli_kw_list cli_kws = {{ },{
12715 { { "show", "errors", NULL },
12716 "show errors : report last request and response errors for each proxy",
12717 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12718 },
12719 {{},}
12720}};
12721
Willy Tarreau4a568972010-05-12 08:08:50 +020012722/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012723/* All supported ACL keywords must be declared here. */
12724/************************************************************************/
12725
12726/* Note: must not be declared <const> as its list will be overwritten.
12727 * Please take care of keeping this list alphabetically sorted.
12728 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012729static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012730 { "base", "base", PAT_MATCH_STR },
12731 { "base_beg", "base", PAT_MATCH_BEG },
12732 { "base_dir", "base", PAT_MATCH_DIR },
12733 { "base_dom", "base", PAT_MATCH_DOM },
12734 { "base_end", "base", PAT_MATCH_END },
12735 { "base_len", "base", PAT_MATCH_LEN },
12736 { "base_reg", "base", PAT_MATCH_REG },
12737 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012738
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012739 { "cook", "req.cook", PAT_MATCH_STR },
12740 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12741 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12742 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12743 { "cook_end", "req.cook", PAT_MATCH_END },
12744 { "cook_len", "req.cook", PAT_MATCH_LEN },
12745 { "cook_reg", "req.cook", PAT_MATCH_REG },
12746 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012747
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012748 { "hdr", "req.hdr", PAT_MATCH_STR },
12749 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12750 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12751 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12752 { "hdr_end", "req.hdr", PAT_MATCH_END },
12753 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12754 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12755 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012756
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012757 /* these two declarations uses strings with list storage (in place
12758 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12759 * and delete functions are relative to the list management. The parse
12760 * and match method are related to the corresponding fetch methods. This
12761 * is very particular ACL declaration mode.
12762 */
12763 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12764 { "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 +020012765
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012766 { "path", "path", PAT_MATCH_STR },
12767 { "path_beg", "path", PAT_MATCH_BEG },
12768 { "path_dir", "path", PAT_MATCH_DIR },
12769 { "path_dom", "path", PAT_MATCH_DOM },
12770 { "path_end", "path", PAT_MATCH_END },
12771 { "path_len", "path", PAT_MATCH_LEN },
12772 { "path_reg", "path", PAT_MATCH_REG },
12773 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012774
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012775 { "req_ver", "req.ver", PAT_MATCH_STR },
12776 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012777
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012778 { "scook", "res.cook", PAT_MATCH_STR },
12779 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12780 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12781 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12782 { "scook_end", "res.cook", PAT_MATCH_END },
12783 { "scook_len", "res.cook", PAT_MATCH_LEN },
12784 { "scook_reg", "res.cook", PAT_MATCH_REG },
12785 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012786
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012787 { "shdr", "res.hdr", PAT_MATCH_STR },
12788 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12789 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12790 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12791 { "shdr_end", "res.hdr", PAT_MATCH_END },
12792 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12793 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12794 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012795
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012796 { "url", "url", PAT_MATCH_STR },
12797 { "url_beg", "url", PAT_MATCH_BEG },
12798 { "url_dir", "url", PAT_MATCH_DIR },
12799 { "url_dom", "url", PAT_MATCH_DOM },
12800 { "url_end", "url", PAT_MATCH_END },
12801 { "url_len", "url", PAT_MATCH_LEN },
12802 { "url_reg", "url", PAT_MATCH_REG },
12803 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012804
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012805 { "urlp", "urlp", PAT_MATCH_STR },
12806 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12807 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12808 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12809 { "urlp_end", "urlp", PAT_MATCH_END },
12810 { "urlp_len", "urlp", PAT_MATCH_LEN },
12811 { "urlp_reg", "urlp", PAT_MATCH_REG },
12812 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012813
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012814 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012815}};
12816
12817/************************************************************************/
12818/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012819/************************************************************************/
12820/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012821static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012822 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012823 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012824 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12825
Willy Tarreau87b09662015-04-03 00:22:06 +020012826 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012827 { "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 +020012828
12829 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012830 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12831 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12832 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012833
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012834 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12835 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012836
Willy Tarreau409bcde2013-01-08 00:31:00 +010012837 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12838 * are only here to match the ACL's name, are request-only and are used
12839 * for ACL compatibility only.
12840 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012841 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12842 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012843 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12844 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012845
12846 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12847 * only here to match the ACL's name, are request-only and are used for
12848 * ACL compatibility only.
12849 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012850 { "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 +020012851 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012852 { "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 +020012853 { "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 +010012854
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012855 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012856 { "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 +010012857 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012858 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012859 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012860 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012861
12862 /* HTTP protocol on the request path */
12863 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012864 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012865
12866 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012867 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12868 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012869
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012870 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012871 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12872 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012873 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012874
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012875 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012876 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12877
Willy Tarreau18ed2562013-01-14 15:56:36 +010012878 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012879 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12880 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012881
Willy Tarreau18ed2562013-01-14 15:56:36 +010012882 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012883 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012884 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12885 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012886
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012887 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012888 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012889 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012890 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012891 { "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 +010012892 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012893 { "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 +010012894
12895 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012896 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012897 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12898 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012899
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012900 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012901 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012902 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012903 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012904 { "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 +010012905 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012906 { "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 +010012907
Willy Tarreau409bcde2013-01-08 00:31:00 +010012908 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012909 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012910 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12911 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012912 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012913
12914 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012915 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012916 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012917 { "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 +020012918 { "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 +010012919
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012920 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012921 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012922 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012923 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012924 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012925 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012926 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012927 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12928 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012929 { "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 +010012930 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012931}};
12932
Willy Tarreau8797c062007-05-07 00:55:35 +020012933
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012934/************************************************************************/
12935/* All supported converter keywords must be declared here. */
12936/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012937/* Note: must not be declared <const> as its list will be overwritten */
12938static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012939 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012940 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012941 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12942 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012943 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012944 { NULL, NULL, 0, 0, 0 },
12945}};
12946
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012947
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012948/************************************************************************/
12949/* All supported http-request action keywords must be declared here. */
12950/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012951struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012952 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012953 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012954 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012955 { "set-method", parse_set_req_line },
12956 { "set-path", parse_set_req_line },
12957 { "set-query", parse_set_req_line },
12958 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012959 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012960 }
12961};
12962
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012963struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012964 .kw = {
12965 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012966 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012967 { NULL, NULL }
12968 }
12969};
12970
Willy Tarreau8797c062007-05-07 00:55:35 +020012971__attribute__((constructor))
12972static void __http_protocol_init(void)
12973{
12974 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012975 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012976 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012977 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012978 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012979 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012980}
12981
12982
Willy Tarreau58f10d72006-12-04 02:26:12 +010012983/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012984 * Local variables:
12985 * c-indent-level: 8
12986 * c-basic-offset: 8
12987 * End:
12988 */