blob: 88cb6360356a4f84ffc12bdf5547e61b66de153e [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 Tarreaubcbd3932018-06-06 07:13:22 +02001111 c_rew(&s->req, 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 Tarreau188e2302018-06-15 11:11:53 +02001114 len = buffer_count(s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001115
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001116 c_adv(&s->req, 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. */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001634 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001635 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 Tarreaud760eec2018-07-10 09:50:25 +02001642 if (c_data(req) && 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 Tarreau188e2302018-06-15 11:11:53 +02001653 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02001654 ci_tail(req) > req->buf->data + req->buf->size - global.tune.maxrewrite))
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02001655 channel_slow_realign(req, trash.str);
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 Tarreau23752332018-06-15 14:54:53 +02001720 if (unlikely(channel_full(req, 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
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002634 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len) < 0) {
2635 static unsigned char rate_limit = 0;
2636
2637 if ((rate_limit++ & 255) == 0) {
2638 replace->str[rule->arg.hdr_add.name_len] = 0;
2639 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, replace->str, s->uniq_id);
2640 }
2641
2642 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2643 if (sess->fe != s->be)
2644 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2645 if (sess->listener->counters)
2646 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2647 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002648
2649 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002650 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002651 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002652
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002653 case ACT_HTTP_DEL_ACL:
2654 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002655 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002656 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002657
2658 /* collect reference */
2659 ref = pat_ref_lookup(rule->arg.map.ref);
2660 if (!ref)
2661 continue;
2662
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002663 /* allocate key */
2664 key = alloc_trash_chunk();
2665 if (!key)
2666 return HTTP_RULE_RES_BADREQ;
2667
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002668 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002669 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2670 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002671
2672 /* perform update */
2673 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002674 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002675 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002676 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002677
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002678 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002679 break;
2680 }
2681
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002682 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002683 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002684 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002685
2686 /* collect reference */
2687 ref = pat_ref_lookup(rule->arg.map.ref);
2688 if (!ref)
2689 continue;
2690
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002691 /* allocate key */
2692 key = alloc_trash_chunk();
2693 if (!key)
2694 return HTTP_RULE_RES_BADREQ;
2695
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002696 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002697 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2698 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002699
2700 /* perform update */
2701 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002702 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002703 if (pat_ref_find_elt(ref, key->str) == NULL)
2704 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002705 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002706
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002707 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002708 break;
2709 }
2710
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002711 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002712 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002713 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002714
2715 /* collect reference */
2716 ref = pat_ref_lookup(rule->arg.map.ref);
2717 if (!ref)
2718 continue;
2719
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002720 /* allocate key */
2721 key = alloc_trash_chunk();
2722 if (!key)
2723 return HTTP_RULE_RES_BADREQ;
2724
2725 /* allocate value */
2726 value = alloc_trash_chunk();
2727 if (!value) {
2728 free_trash_chunk(key);
2729 return HTTP_RULE_RES_BADREQ;
2730 }
2731
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002732 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002733 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2734 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002735
2736 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002737 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2738 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002739
2740 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002741 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002742 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002743 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002744 else
2745 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002746 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002747
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002748 free_trash_chunk(key);
2749 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002750 break;
2751 }
William Lallemand73025dd2014-04-24 14:38:37 +02002752
Thierry FOURNIER42148732015-09-02 17:17:33 +02002753 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002754 if ((s->req.flags & CF_READ_ERROR) ||
2755 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2756 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2757 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002758 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002759
Willy Tarreauacc98002015-09-27 23:34:39 +02002760 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002761 case ACT_RET_ERR:
2762 case ACT_RET_CONT:
2763 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002764 case ACT_RET_STOP:
2765 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002766 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002767 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002768 return HTTP_RULE_RES_YIELD;
2769 }
William Lallemand73025dd2014-04-24 14:38:37 +02002770 break;
2771
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002772 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002773 /* Note: only the first valid tracking parameter of each
2774 * applies.
2775 */
2776
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002777 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002778 struct stktable *t;
2779 struct stksess *ts;
2780 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002781 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002782
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002783 t = rule->arg.trk_ctr.table.t;
2784 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 +02002785
2786 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002787 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002788
2789 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002790 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2791 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2792 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002793 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002794
Emeric Brun819fc6f2017-06-13 19:37:32 +02002795 if (ptr1)
2796 stktable_data_cast(ptr1, http_req_cnt)++;
2797
2798 if (ptr2)
2799 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2800 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2801
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002802 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002803
2804 /* If data was modified, we need to touch to re-schedule sync */
2805 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002806 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002807
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002808 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002809 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002810 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002811 }
2812 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002813 break;
2814
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002815 /* other flags exists, but normaly, they never be matched. */
2816 default:
2817 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002818 }
2819 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002820
2821 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002822 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002823}
2824
Willy Tarreau71241ab2012-12-27 11:30:54 +01002825
Willy Tarreau51d861a2015-05-22 17:30:48 +02002826/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2827 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2828 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2829 * is returned, the process can continue the evaluation of next rule list. If
2830 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2831 * is returned, it means the operation could not be processed and a server error
2832 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2833 * deny rule. If *YIELD is returned, the caller must call again the function
2834 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002835 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002836static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002837http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002838{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002839 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002840 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002841 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002842 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002843 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002844 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002845
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002846 /* If "the current_rule_list" match the executed rule list, we are in
2847 * resume condition. If a resume is needed it is always in the action
2848 * and never in the ACL or converters. In this case, we initialise the
2849 * current rule, and go to the action execution point.
2850 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002851 if (s->current_rule) {
2852 rule = s->current_rule;
2853 s->current_rule = NULL;
2854 if (s->current_rule_list == rules)
2855 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002856 }
2857 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002858
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002859 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002860
2861 /* check optional condition */
2862 if (rule->cond) {
2863 int ret;
2864
Willy Tarreau192252e2015-04-04 01:47:55 +02002865 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002866 ret = acl_pass(ret);
2867
2868 if (rule->cond->pol == ACL_COND_UNLESS)
2869 ret = !ret;
2870
2871 if (!ret) /* condition not matched */
2872 continue;
2873 }
2874
Willy Tarreauacc98002015-09-27 23:34:39 +02002875 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002876resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002877 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002878 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002879 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002880
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002881 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002882 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002883 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002885 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002886 s->task->nice = rule->arg.nice;
2887 break;
2888
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002889 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002890 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002891 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002892 break;
2893
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002894 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002895#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002896 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002897 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002898#endif
2899 break;
2900
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002901 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002902 s->logs.level = rule->arg.loglevel;
2903 break;
2904
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002905 case ACT_HTTP_REPLACE_HDR:
2906 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002907 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2908 rule->arg.hdr_add.name_len,
2909 &rule->arg.hdr_add.fmt,
2910 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002911 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002912 break;
2913
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002914 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002915 ctx.idx = 0;
2916 /* remove all occurrences of the header */
2917 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2918 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2919 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2920 }
Willy Tarreau85603282015-01-21 20:39:27 +01002921 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002922
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002923 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002924 case ACT_HTTP_ADD_HDR: {
2925 struct chunk *replace;
2926
2927 replace = alloc_trash_chunk();
2928 if (!replace)
2929 return HTTP_RULE_RES_BADREQ;
2930
2931 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2932 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2933 replace->len = rule->arg.hdr_add.name_len;
2934 replace->str[replace->len++] = ':';
2935 replace->str[replace->len++] = ' ';
2936 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2937 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002938
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002939 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002940 /* remove all occurrences of the header */
2941 ctx.idx = 0;
2942 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2943 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2944 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2945 }
2946 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002947
2948 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len) < 0) {
2949 static unsigned char rate_limit = 0;
2950
2951 if ((rate_limit++ & 255) == 0) {
2952 replace->str[rule->arg.hdr_add.name_len] = 0;
2953 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, replace->str, s->uniq_id);
2954 }
2955
2956 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2957 if (sess->fe != s->be)
2958 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2959 if (sess->listener->counters)
2960 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2961 if (objt_server(s->target))
2962 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2963 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002964
2965 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002966 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002967 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002968
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002969 case ACT_HTTP_DEL_ACL:
2970 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002971 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002972 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002973
2974 /* collect reference */
2975 ref = pat_ref_lookup(rule->arg.map.ref);
2976 if (!ref)
2977 continue;
2978
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002979 /* allocate key */
2980 key = alloc_trash_chunk();
2981 if (!key)
2982 return HTTP_RULE_RES_BADREQ;
2983
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002984 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002985 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2986 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002987
2988 /* perform update */
2989 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002990 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002991 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002992 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002993
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002994 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002995 break;
2996 }
2997
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002998 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002999 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003000 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003001
3002 /* collect reference */
3003 ref = pat_ref_lookup(rule->arg.map.ref);
3004 if (!ref)
3005 continue;
3006
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003007 /* allocate key */
3008 key = alloc_trash_chunk();
3009 if (!key)
3010 return HTTP_RULE_RES_BADREQ;
3011
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003012 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003013 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3014 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003015
3016 /* perform update */
3017 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003018 if (pat_ref_find_elt(ref, key->str) == NULL)
3019 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003020
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003021 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003022 break;
3023 }
3024
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003025 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003026 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003027 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003028
3029 /* collect reference */
3030 ref = pat_ref_lookup(rule->arg.map.ref);
3031 if (!ref)
3032 continue;
3033
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003034 /* allocate key */
3035 key = alloc_trash_chunk();
3036 if (!key)
3037 return HTTP_RULE_RES_BADREQ;
3038
3039 /* allocate value */
3040 value = alloc_trash_chunk();
3041 if (!value) {
3042 free_trash_chunk(key);
3043 return HTTP_RULE_RES_BADREQ;
3044 }
3045
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003046 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003047 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3048 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003049
3050 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003051 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
3052 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003053
3054 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003055 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003056 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003057 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003058 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003059 else
3060 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003061 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003062 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003063 free_trash_chunk(key);
3064 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003065 break;
3066 }
William Lallemand73025dd2014-04-24 14:38:37 +02003067
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003068 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003069 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3070 return HTTP_RULE_RES_BADREQ;
3071 return HTTP_RULE_RES_DONE;
3072
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003073 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3074 /* Note: only the first valid tracking parameter of each
3075 * applies.
3076 */
3077
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003078 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003079 struct stktable *t;
3080 struct stksess *ts;
3081 struct stktable_key *key;
3082 void *ptr;
3083
3084 t = rule->arg.trk_ctr.table.t;
3085 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3086
3087 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003088 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003089
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003090 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003091
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003092 /* let's count a new HTTP request as it's the first time we do it */
3093 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3094 if (ptr)
3095 stktable_data_cast(ptr, http_req_cnt)++;
3096
3097 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3098 if (ptr)
3099 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3100 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3101
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003102 /* When the client triggers a 4xx from the server, it's most often due
3103 * to a missing object or permission. These events should be tracked
3104 * because if they happen often, it may indicate a brute force or a
3105 * vulnerability scan. Normally this is done when receiving the response
3106 * but here we're tracking after this ought to have been done so we have
3107 * to do it on purpose.
3108 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003109 if ((unsigned)(txn->status - 400) < 100) {
3110 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3111 if (ptr)
3112 stktable_data_cast(ptr, http_err_cnt)++;
3113
3114 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3115 if (ptr)
3116 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3117 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3118 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003119
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003120 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003121
Emeric Brun0fed0b02017-11-29 16:15:07 +01003122 /* If data was modified, we need to touch to re-schedule sync */
3123 stktable_touch_local(t, ts, 0);
3124
Emeric Brun819fc6f2017-06-13 19:37:32 +02003125 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3126 if (sess->fe != s->be)
3127 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3128
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003129 }
3130 }
3131 break;
3132
Thierry FOURNIER42148732015-09-02 17:17:33 +02003133 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003134 if ((s->req.flags & CF_READ_ERROR) ||
3135 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3136 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3137 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003138 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003139
Willy Tarreauacc98002015-09-27 23:34:39 +02003140 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003141 case ACT_RET_ERR:
3142 case ACT_RET_CONT:
3143 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003144 case ACT_RET_STOP:
3145 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003146 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003147 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003148 return HTTP_RULE_RES_YIELD;
3149 }
William Lallemand73025dd2014-04-24 14:38:37 +02003150 break;
3151
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003152 /* other flags exists, but normaly, they never be matched. */
3153 default:
3154 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003155 }
3156 }
3157
3158 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003159 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003160}
3161
3162
Willy Tarreau71241ab2012-12-27 11:30:54 +01003163/* Perform an HTTP redirect based on the information in <rule>. The function
3164 * returns non-zero on success, or zero in case of a, irrecoverable error such
3165 * as too large a request to build a valid response.
3166 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003167static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003168{
Willy Tarreaub329a312015-05-22 16:27:37 +02003169 struct http_msg *req = &txn->req;
3170 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003171 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003172 struct chunk *chunk;
3173 int ret = 0;
3174
3175 chunk = alloc_trash_chunk();
3176 if (!chunk)
3177 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003178
3179 /* build redirect message */
3180 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003181 case 308:
3182 msg_fmt = HTTP_308;
3183 break;
3184 case 307:
3185 msg_fmt = HTTP_307;
3186 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003187 case 303:
3188 msg_fmt = HTTP_303;
3189 break;
3190 case 301:
3191 msg_fmt = HTTP_301;
3192 break;
3193 case 302:
3194 default:
3195 msg_fmt = HTTP_302;
3196 break;
3197 }
3198
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003199 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3200 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003201
3202 switch(rule->type) {
3203 case REDIRECT_TYPE_SCHEME: {
3204 const char *path;
3205 const char *host;
3206 struct hdr_ctx ctx;
3207 int pathlen;
3208 int hostlen;
3209
3210 host = "";
3211 hostlen = 0;
3212 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003213 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003214 host = ctx.line + ctx.val;
3215 hostlen = ctx.vlen;
3216 }
3217
3218 path = http_get_path(txn);
3219 /* build message using path */
3220 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003221 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003222 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3223 int qs = 0;
3224 while (qs < pathlen) {
3225 if (path[qs] == '?') {
3226 pathlen = qs;
3227 break;
3228 }
3229 qs++;
3230 }
3231 }
3232 } else {
3233 path = "/";
3234 pathlen = 1;
3235 }
3236
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003237 if (rule->rdr_str) { /* this is an old "redirect" rule */
3238 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003239 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3240 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003241
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003242 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003243 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3244 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003245 }
3246 else {
3247 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003248 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003249
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003250 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003251 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3252 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003253 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003254 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003255 memcpy(chunk->str + chunk->len, "://", 3);
3256 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003257
3258 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003259 memcpy(chunk->str + chunk->len, host, hostlen);
3260 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003261
3262 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003263 memcpy(chunk->str + chunk->len, path, pathlen);
3264 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003265
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003266 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003267 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003268 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003269 if (chunk->len > chunk->size - 5)
3270 goto leave;
3271 chunk->str[chunk->len] = '/';
3272 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003273 }
3274
3275 break;
3276 }
3277 case REDIRECT_TYPE_PREFIX: {
3278 const char *path;
3279 int pathlen;
3280
3281 path = http_get_path(txn);
3282 /* build message using path */
3283 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003284 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003285 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3286 int qs = 0;
3287 while (qs < pathlen) {
3288 if (path[qs] == '?') {
3289 pathlen = qs;
3290 break;
3291 }
3292 qs++;
3293 }
3294 }
3295 } else {
3296 path = "/";
3297 pathlen = 1;
3298 }
3299
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003300 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003301 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3302 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003303
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003304 /* add prefix. Note that if prefix == "/", we don't want to
3305 * add anything, otherwise it makes it hard for the user to
3306 * configure a self-redirection.
3307 */
3308 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003309 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3310 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003311 }
3312 }
3313 else {
3314 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003315 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003316
3317 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003318 if (chunk->len + pathlen > chunk->size - 4)
3319 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003320 }
3321
3322 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003323 memcpy(chunk->str + chunk->len, path, pathlen);
3324 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003325
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003326 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003327 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003328 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003329 if (chunk->len > chunk->size - 5)
3330 goto leave;
3331 chunk->str[chunk->len] = '/';
3332 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003333 }
3334
3335 break;
3336 }
3337 case REDIRECT_TYPE_LOCATION:
3338 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003339 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003340 if (chunk->len + rule->rdr_len > chunk->size - 4)
3341 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003342
3343 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003344 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3345 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003346 }
3347 else {
3348 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003349 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003350
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003351 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003352 if (chunk->len > chunk->size - 4)
3353 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003354 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003355 break;
3356 }
3357
3358 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003359 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3360 chunk->len += 14;
3361 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3362 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003363 }
3364
Willy Tarreau19b14122017-02-28 09:48:11 +01003365 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003366 txn->status = rule->code;
3367 /* let's log the request time */
3368 s->logs.tv_request = now;
3369
Christopher Fauletbe821b92017-03-30 11:21:53 +02003370 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003371 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3372 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3373 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003374 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003375 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003376 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3377 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003378 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003379 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3380 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003381 }
3382 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003383 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3384 chunk->len += 4;
3385 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003386 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003387 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003388 bi_fast_delete(req->chn->buf, req->sov);
3389 req->next -= req->sov;
3390 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003391 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003392 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003393 req->msg_state = HTTP_MSG_CLOSED;
3394 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003395 /* Trim any possible response */
3396 res->chn->buf->i = 0;
3397 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003398 /* let the server side turn to SI_ST_CLO */
3399 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003400 } else {
3401 /* keep-alive not possible */
3402 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003403 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3404 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003405 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003406 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3407 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003408 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003409 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003410 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003411 }
3412
Willy Tarreaue7dff022015-04-03 01:14:29 +02003413 if (!(s->flags & SF_ERR_MASK))
3414 s->flags |= SF_ERR_LOCAL;
3415 if (!(s->flags & SF_FINST_MASK))
3416 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003417
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003418 ret = 1;
3419 leave:
3420 free_trash_chunk(chunk);
3421 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003422}
3423
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003424/* This stream analyser runs all HTTP request processing which is common to
3425 * frontends and backends, which means blocking ACLs, filters, connection-close,
3426 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003427 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003428 * either needs more data or wants to immediately abort the request (eg: deny,
3429 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003430 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003431int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003432{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003433 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003434 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003435 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003436 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003437 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003438 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003439 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003440 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003441
Willy Tarreau655dce92009-11-08 13:10:58 +01003442 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003443 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003444 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003445 }
3446
Willy Tarreau87b09662015-04-03 00:22:06 +02003447 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 +02003448 now_ms, __FUNCTION__,
3449 s,
3450 req,
3451 req->rex, req->wex,
3452 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003453 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003454 req->analysers);
3455
Willy Tarreau65410832014-04-28 21:25:43 +02003456 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003457 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003458
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003459 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003460 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003461 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003462
Willy Tarreau0b748332014-04-29 00:13:29 +02003463 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003464 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3465 goto return_prx_yield;
3466
Willy Tarreau0b748332014-04-29 00:13:29 +02003467 case HTTP_RULE_RES_CONT:
3468 case HTTP_RULE_RES_STOP: /* nothing to do */
3469 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003470
Willy Tarreau0b748332014-04-29 00:13:29 +02003471 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3472 if (txn->flags & TX_CLTARPIT)
3473 goto tarpit;
3474 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003475
Willy Tarreau0b748332014-04-29 00:13:29 +02003476 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3477 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003478
Willy Tarreau0b748332014-04-29 00:13:29 +02003479 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003480 goto done;
3481
Willy Tarreau0b748332014-04-29 00:13:29 +02003482 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3483 goto return_bad_req;
3484 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003485 }
3486
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003487 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3488 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003489 struct hdr_ctx ctx;
3490
3491 ctx.idx = 0;
3492 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3493 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3494 if (unlikely(http_header_add_tail2(&txn->req,
3495 &txn->hdr_idx, "Early-Data: 1",
3496 strlen("Early-Data: 1"))) < 0) {
3497 goto return_bad_req;
3498 }
3499 }
3500
3501 }
3502
Willy Tarreau52542592014-04-28 18:33:26 +02003503 /* OK at this stage, we know that the request was accepted according to
3504 * the http-request rules, we can check for the stats. Note that the
3505 * URI is detected *before* the req* rules in order not to be affected
3506 * by a possible reqrep, while they are processed *after* so that a
3507 * reqdeny can still block them. This clearly needs to change in 1.6!
3508 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003509 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003510 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003511 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003512 txn->status = 500;
3513 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003514 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003515
Willy Tarreaue7dff022015-04-03 01:14:29 +02003516 if (!(s->flags & SF_ERR_MASK))
3517 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003518 goto return_prx_cond;
3519 }
3520
3521 /* parse the whole stats request and extract the relevant information */
3522 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003523 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003524 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003525
Willy Tarreau0b748332014-04-29 00:13:29 +02003526 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3527 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003528
Willy Tarreau0b748332014-04-29 00:13:29 +02003529 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3530 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003531 }
3532
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003533 /* evaluate the req* rules except reqadd */
3534 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003535 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003536 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003537
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003538 if (txn->flags & TX_CLDENY)
3539 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003540
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003541 if (txn->flags & TX_CLTARPIT) {
3542 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003543 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003544 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003545 }
Willy Tarreau06619262006-12-17 08:37:22 +01003546
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003547 /* add request headers from the rule sets in the same order */
3548 list_for_each_entry(wl, &px->req_add, list) {
3549 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003550 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003551 ret = acl_pass(ret);
3552 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3553 ret = !ret;
3554 if (!ret)
3555 continue;
3556 }
3557
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003558 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003559 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003560 }
3561
Willy Tarreau52542592014-04-28 18:33:26 +02003562
3563 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003564 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3565 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003566 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003567 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003568 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003569
Willy Tarreaue7dff022015-04-03 01:14:29 +02003570 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3571 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3572 if (!(s->flags & SF_FINST_MASK))
3573 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003574
Willy Tarreau70730dd2014-04-24 18:06:27 +02003575 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003576 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3577 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003578 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003579 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003580 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003581
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003582 /* check whether we have some ACLs set to redirect this request */
3583 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003584 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003585 int ret;
3586
Willy Tarreau192252e2015-04-04 01:47:55 +02003587 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003588 ret = acl_pass(ret);
3589 if (rule->cond->pol == ACL_COND_UNLESS)
3590 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003591 if (!ret)
3592 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003593 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003594 if (!http_apply_redirect_rule(rule, s, txn))
3595 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003596 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003597 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003598
Willy Tarreau2be39392010-01-03 17:24:51 +01003599 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3600 * If this happens, then the data will not come immediately, so we must
3601 * send all what we have without waiting. Note that due to the small gain
3602 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003603 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003604 * itself once used.
3605 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003606 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003607
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003608 done: /* done with this analyser, continue with next ones that the calling
3609 * points will have set, if any.
3610 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003611 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003612 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3613 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003614 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003615
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003616 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003617 /* Allow cookie logging
3618 */
3619 if (s->be->cookie_name || sess->fe->capture_name)
3620 manage_client_side_cookies(s, req);
3621
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003622 /* When a connection is tarpitted, we use the tarpit timeout,
3623 * which may be the same as the connect timeout if unspecified.
3624 * If unset, then set it to zero because we really want it to
3625 * eventually expire. We build the tarpit as an analyser.
3626 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003627 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003628
3629 /* wipe the request out so that we can drop the connection early
3630 * if the client closes first.
3631 */
3632 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003633
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003634 txn->status = http_err_codes[deny_status];
3635
Christopher Faulet0184ea72017-01-05 14:06:34 +01003636 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003637 req->analysers |= AN_REQ_HTTP_TARPIT;
3638 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3639 if (!req->analyse_exp)
3640 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003641 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003642 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003643 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003644 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003645 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003646 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003647 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003648
3649 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003650
3651 /* Allow cookie logging
3652 */
3653 if (s->be->cookie_name || sess->fe->capture_name)
3654 manage_client_side_cookies(s, req);
3655
Willy Tarreau0b748332014-04-29 00:13:29 +02003656 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003657 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003658 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003659 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003660 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003661 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003662 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003663 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003664 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003665 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003666 goto return_prx_cond;
3667
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003668 return_bad_req:
3669 /* We centralize bad requests processing here */
3670 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3671 /* we detected a parsing error. We want to archive this request
3672 * in the dedicated proxy area for later troubleshooting.
3673 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003674 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003675 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003676
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003677 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003678 txn->req.msg_state = HTTP_MSG_ERROR;
3679 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003680 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003681
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003682 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003683 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003684 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003685
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003686 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003687 if (!(s->flags & SF_ERR_MASK))
3688 s->flags |= SF_ERR_PRXCOND;
3689 if (!(s->flags & SF_FINST_MASK))
3690 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003691
Christopher Faulet0184ea72017-01-05 14:06:34 +01003692 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003693 req->analyse_exp = TICK_ETERNITY;
3694 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003695
3696 return_prx_yield:
3697 channel_dont_connect(req);
3698 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003699}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003700
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003701/* This function performs all the processing enabled for the current request.
3702 * It returns 1 if the processing can continue on next analysers, or zero if it
3703 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003704 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003705 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003706int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003707{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003708 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003709 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003710 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003711 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003712
Willy Tarreau655dce92009-11-08 13:10:58 +01003713 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003714 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003715 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003716 return 0;
3717 }
3718
Willy Tarreau87b09662015-04-03 00:22:06 +02003719 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 +02003720 now_ms, __FUNCTION__,
3721 s,
3722 req,
3723 req->rex, req->wex,
3724 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003725 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003726 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003727
Willy Tarreau59234e92008-11-30 23:51:27 +01003728 /*
3729 * Right now, we know that we have processed the entire headers
3730 * and that unwanted requests have been filtered out. We can do
3731 * whatever we want with the remaining request. Also, now we
3732 * may have separate values for ->fe, ->be.
3733 */
Willy Tarreau06619262006-12-17 08:37:22 +01003734
Willy Tarreau59234e92008-11-30 23:51:27 +01003735 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003736 * If HTTP PROXY is set we simply get remote server address parsing
3737 * incoming request. Note that this requires that a connection is
3738 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003739 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003740 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003741 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003742 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003743
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003744 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003745 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003746 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003747 txn->req.msg_state = HTTP_MSG_ERROR;
3748 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003749 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003750 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003751
Willy Tarreaue7dff022015-04-03 01:14:29 +02003752 if (!(s->flags & SF_ERR_MASK))
3753 s->flags |= SF_ERR_RESOURCE;
3754 if (!(s->flags & SF_FINST_MASK))
3755 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003756
3757 return 0;
3758 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003759
3760 path = http_get_path(txn);
Christopher Faulet11ebb202018-04-13 15:53:12 +02003761 if (url2sa(req->buf->p + msg->sl.rq.u,
3762 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3763 &conn->addr.to, NULL) == -1)
3764 goto return_bad_req;
3765
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003766 /* if the path was found, we have to remove everything between
3767 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3768 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3769 * u_l characters by a single "/".
3770 */
3771 if (path) {
3772 char *cur_ptr = req->buf->p;
3773 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3774 int delta;
3775
3776 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3777 http_msg_move_end(&txn->req, delta);
3778 cur_end += delta;
3779 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3780 goto return_bad_req;
3781 }
3782 else {
3783 char *cur_ptr = req->buf->p;
3784 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3785 int delta;
3786
3787 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3788 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3789 http_msg_move_end(&txn->req, delta);
3790 cur_end += delta;
3791 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3792 goto return_bad_req;
3793 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003794 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003795
Willy Tarreau59234e92008-11-30 23:51:27 +01003796 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003797 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003798 * Note that doing so might move headers in the request, but
3799 * the fields will stay coherent and the URI will not move.
3800 * This should only be performed in the backend.
3801 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003802 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003804
William Lallemanda73203e2012-03-12 12:48:57 +01003805 /* add unique-id if "header-unique-id" is specified */
3806
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003807 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003808 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003809 goto return_bad_req;
3810 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003811 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003812 }
William Lallemanda73203e2012-03-12 12:48:57 +01003813
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003814 if (sess->fe->header_unique_id && s->unique_id) {
3815 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003816 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003817 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003818 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003819 goto return_bad_req;
3820 }
3821
Cyril Bontéb21570a2009-11-29 20:04:48 +01003822 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003823 * 9: add X-Forwarded-For if either the frontend or the backend
3824 * asks for it.
3825 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003826 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003827 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003828 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3829 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3830 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003831 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003832 /* The header is set to be added only if none is present
3833 * and we found it, so don't do anything.
3834 */
3835 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003836 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003837 /* Add an X-Forwarded-For header unless the source IP is
3838 * in the 'except' network range.
3839 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003840 if ((!sess->fe->except_mask.s_addr ||
3841 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3842 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003843 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003844 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003845 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003846 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003847 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003848 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003849
3850 /* Note: we rely on the backend to get the header name to be used for
3851 * x-forwarded-for, because the header is really meant for the backends.
3852 * However, if the backend did not specify any option, we have to rely
3853 * on the frontend's header name.
3854 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003855 if (s->be->fwdfor_hdr_len) {
3856 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003857 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003858 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003859 len = sess->fe->fwdfor_hdr_len;
3860 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003861 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003862 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 +01003863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003864 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003865 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003866 }
3867 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003868 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003869 /* FIXME: for the sake of completeness, we should also support
3870 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003871 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003872 int len;
3873 char pn[INET6_ADDRSTRLEN];
3874 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003875 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003876 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003877
Willy Tarreau59234e92008-11-30 23:51:27 +01003878 /* Note: we rely on the backend to get the header name to be used for
3879 * x-forwarded-for, because the header is really meant for the backends.
3880 * However, if the backend did not specify any option, we have to rely
3881 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003882 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003883 if (s->be->fwdfor_hdr_len) {
3884 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003885 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003886 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003887 len = sess->fe->fwdfor_hdr_len;
3888 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003889 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003890 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003891
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003892 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003893 goto return_bad_req;
3894 }
3895 }
3896
3897 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003898 * 10: add X-Original-To if either the frontend or the backend
3899 * asks for it.
3900 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003901 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003902
3903 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003904 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003905 /* Add an X-Original-To header unless the destination IP is
3906 * in the 'except' network range.
3907 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003908 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003909
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003910 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003911 ((!sess->fe->except_mask_to.s_addr ||
3912 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3913 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003914 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003915 (((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 +02003916 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003917 int len;
3918 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003919 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003920
3921 /* Note: we rely on the backend to get the header name to be used for
3922 * x-original-to, because the header is really meant for the backends.
3923 * However, if the backend did not specify any option, we have to rely
3924 * on the frontend's header name.
3925 */
3926 if (s->be->orgto_hdr_len) {
3927 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003928 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003929 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003930 len = sess->fe->orgto_hdr_len;
3931 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003932 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003933 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 +02003934
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003935 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003936 goto return_bad_req;
3937 }
3938 }
3939 }
3940
Willy Tarreau50fc7772012-11-11 22:19:57 +01003941 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3942 * If an "Upgrade" token is found, the header is left untouched in order not to have
3943 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3944 * "Upgrade" is present in the Connection header.
3945 */
3946 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3947 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003948 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003949 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003950 unsigned int want_flags = 0;
3951
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003952 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003953 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003954 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003955 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003956 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003957 want_flags |= TX_CON_CLO_SET;
3958 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003959 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003960 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003961 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003962 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003963 want_flags |= TX_CON_KAL_SET;
3964 }
3965
3966 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003967 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003968 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003969
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003970
Willy Tarreau522d6c02009-12-06 18:49:18 +01003971 /* If we have no server assigned yet and we're balancing on url_param
3972 * with a POST request, we may be interested in checking the body for
3973 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003974 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003975 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003976 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003977 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003978 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003979 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003980 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003981
Christopher Fauletbe821b92017-03-30 11:21:53 +02003982 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3983 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003984#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003985 /* We expect some data from the client. Unless we know for sure
3986 * we already have a full request, we have to re-enable quick-ack
3987 * in case we previously disabled it, otherwise we might cause
3988 * the client to delay further data.
3989 */
3990 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3991 cli_conn && conn_ctrl_ready(cli_conn) &&
3992 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3993 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003994 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003995#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003996
Willy Tarreau59234e92008-11-30 23:51:27 +01003997 /*************************************************************
3998 * OK, that's finished for the headers. We have done what we *
3999 * could. Let's switch to the DATA state. *
4000 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004001 req->analyse_exp = TICK_ETERNITY;
4002 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004003
Willy Tarreau59234e92008-11-30 23:51:27 +01004004 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004005 /* OK let's go on with the BODY now */
4006 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004007
Willy Tarreau59234e92008-11-30 23:51:27 +01004008 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004009 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004010 /* we detected a parsing error. We want to archive this request
4011 * in the dedicated proxy area for later troubleshooting.
4012 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004013 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004014 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004015
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004016 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004017 txn->req.msg_state = HTTP_MSG_ERROR;
4018 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004019 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004020 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004021
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004022 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004023 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004024 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004025
Willy Tarreaue7dff022015-04-03 01:14:29 +02004026 if (!(s->flags & SF_ERR_MASK))
4027 s->flags |= SF_ERR_PRXCOND;
4028 if (!(s->flags & SF_FINST_MASK))
4029 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004030 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004031}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004032
Willy Tarreau60b85b02008-11-30 23:28:40 +01004033/* This function is an analyser which processes the HTTP tarpit. It always
4034 * returns zero, at the beginning because it prevents any other processing
4035 * from occurring, and at the end because it terminates the request.
4036 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004037int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004038{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004039 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004040
4041 /* This connection is being tarpitted. The CLIENT side has
4042 * already set the connect expiration date to the right
4043 * timeout. We just have to check that the client is still
4044 * there and that the timeout has not expired.
4045 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004046 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004047 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004048 !tick_is_expired(req->analyse_exp, now_ms))
4049 return 0;
4050
4051 /* We will set the queue timer to the time spent, just for
4052 * logging purposes. We fake a 500 server error, so that the
4053 * attacker will not suspect his connection has been tarpitted.
4054 * It will not cause trouble to the logs because we can exclude
4055 * the tarpitted connections by filtering on the 'PT' status flags.
4056 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004057 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4058
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004059 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004060 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004061
Christopher Faulet0184ea72017-01-05 14:06:34 +01004062 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004063 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004064
Willy Tarreaue7dff022015-04-03 01:14:29 +02004065 if (!(s->flags & SF_ERR_MASK))
4066 s->flags |= SF_ERR_PRXCOND;
4067 if (!(s->flags & SF_FINST_MASK))
4068 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004069 return 0;
4070}
4071
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004072/* This function is an analyser which waits for the HTTP request body. It waits
4073 * for either the buffer to be full, or the full advertised contents to have
4074 * reached the buffer. It must only be called after the standard HTTP request
4075 * processing has occurred, because it expects the request to be parsed and will
4076 * look for the Expect header. It may send a 100-Continue interim response. It
4077 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4078 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4079 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004080 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004081int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004082{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004083 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004084 struct http_txn *txn = s->txn;
4085 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004086
4087 /* We have to parse the HTTP request body to find any required data.
4088 * "balance url_param check_post" should have been the only way to get
4089 * into this. We were brought here after HTTP header analysis, so all
4090 * related structures are ready.
4091 */
4092
Willy Tarreau890988f2014-04-10 11:59:33 +02004093 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4094 /* This is the first call */
4095 if (msg->msg_state < HTTP_MSG_BODY)
4096 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004097
Willy Tarreau890988f2014-04-10 11:59:33 +02004098 if (msg->msg_state < HTTP_MSG_100_SENT) {
4099 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4100 * send an HTTP/1.1 100 Continue intermediate response.
4101 */
4102 if (msg->flags & HTTP_MSGF_VER_11) {
4103 struct hdr_ctx ctx;
4104 ctx.idx = 0;
4105 /* Expect is allowed in 1.1, look for it */
4106 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4107 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004108 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004109 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004110 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004111 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004112 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004113 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004114
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004115 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004116 * req->buf->p still points to the beginning of the message. We
4117 * must save the body in msg->next because it survives buffer
4118 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004119 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004120 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004121
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004122 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004123 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4124 else
4125 msg->msg_state = HTTP_MSG_DATA;
4126 }
4127
Willy Tarreau890988f2014-04-10 11:59:33 +02004128 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4129 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004130 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004131 goto missing_data;
4132
4133 /* OK we have everything we need now */
4134 goto http_end;
4135 }
4136
4137 /* OK here we're parsing a chunked-encoded message */
4138
Willy Tarreau522d6c02009-12-06 18:49:18 +01004139 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004140 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004141 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004142 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004143 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004144 unsigned int chunk;
Willy Tarreaud760eec2018-07-10 09:50:25 +02004145 int ret = h1_parse_chunk_size(req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004146
Willy Tarreau115acb92009-12-26 13:56:06 +01004147 if (!ret)
4148 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004149 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004150 msg->err_pos = req->buf->i + ret;
4151 if (msg->err_pos < 0)
4152 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004153 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004154 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004155 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004156
4157 msg->chunk_len = chunk;
4158 msg->body_len += chunk;
4159
4160 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004161 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004162 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004163 }
4164
Willy Tarreaud98cf932009-12-27 22:54:55 +01004165 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004166 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4167 * for at least a whole chunk or the whole content length bytes after
4168 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004169 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004170 if (msg->msg_state == HTTP_MSG_TRAILERS)
4171 goto http_end;
4172
Willy Tarreaue115b492015-05-01 23:05:14 +02004173 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004174 goto http_end;
4175
4176 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004177 /* we get here if we need to wait for more data. If the buffer is full,
4178 * we have the maximum we can expect.
4179 */
Willy Tarreau23752332018-06-15 14:54:53 +02004180 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02004181 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004182
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004183 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004185 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004186
Willy Tarreaue7dff022015-04-03 01:14:29 +02004187 if (!(s->flags & SF_ERR_MASK))
4188 s->flags |= SF_ERR_CLITO;
4189 if (!(s->flags & SF_FINST_MASK))
4190 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004191 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004192 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004193
4194 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004195 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004196 /* Not enough data. We'll re-use the http-request
4197 * timeout here. Ideally, we should set the timeout
4198 * relative to the accept() date. We just set the
4199 * request timeout once at the beginning of the
4200 * request.
4201 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004202 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004203 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004204 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004205 return 0;
4206 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004207
4208 http_end:
4209 /* The situation will not evolve, so let's give up on the analysis. */
4210 s->logs.tv_request = now; /* update the request timer to reflect full request */
4211 req->analysers &= ~an_bit;
4212 req->analyse_exp = TICK_ETERNITY;
4213 return 1;
4214
4215 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004216 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004217 txn->req.msg_state = HTTP_MSG_ERROR;
4218 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004219 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004220
Willy Tarreaue7dff022015-04-03 01:14:29 +02004221 if (!(s->flags & SF_ERR_MASK))
4222 s->flags |= SF_ERR_PRXCOND;
4223 if (!(s->flags & SF_FINST_MASK))
4224 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004225
Willy Tarreau522d6c02009-12-06 18:49:18 +01004226 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004227 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004228 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004229 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004230 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004231 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004232}
4233
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004234/* send a server's name with an outgoing request over an established connection.
4235 * Note: this function is designed to be called once the request has been scheduled
4236 * for being forwarded. This is the reason why it rewinds the buffer before
4237 * proceeding.
4238 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004239int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004240
4241 struct hdr_ctx ctx;
4242
Mark Lamourinec2247f02012-01-04 13:02:01 -05004243 char *hdr_name = be->server_id_hdr_name;
4244 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004245 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004246 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004247 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004248
William Lallemandd9e90662012-01-30 17:27:17 +01004249 ctx.idx = 0;
4250
Willy Tarreau211cdec2014-04-17 20:18:08 +02004251 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004252 if (old_o) {
4253 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004254 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004255 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004256 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004257 }
4258
Willy Tarreau9b28e032012-10-12 23:49:43 +02004259 old_i = chn->buf->i;
4260 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 -05004261 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004262 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004263 }
4264
4265 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004266 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004267 memcpy(hdr_val, hdr_name, hdr_name_len);
4268 hdr_val += hdr_name_len;
4269 *hdr_val++ = ':';
4270 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004271 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4272 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004273
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004274 if (old_o) {
4275 /* If this was a forwarded request, we must readjust the amount of
4276 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004277 * variations. Note that the current state is >= HTTP_MSG_BODY,
4278 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004279 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004280 old_o += chn->buf->i - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004281 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004282 txn->req.next -= old_o;
4283 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004284 }
4285
Mark Lamourinec2247f02012-01-04 13:02:01 -05004286 return 0;
4287}
4288
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289/* Terminate current transaction and prepare a new one. This is very tricky
4290 * right now but it works.
4291 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004292void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004294 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004295 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004296 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004297 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004298 struct connection *srv_conn;
4299 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004300 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004301
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302 /* FIXME: We need a more portable way of releasing a backend's and a
4303 * server's connections. We need a safer way to reinitialize buffer
4304 * flags. We also need a more accurate method for computing per-request
4305 * data.
4306 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004307 /*
4308 * XXX cognet: This is probably wrong, this is killing a whole
4309 * connection, in the new world order, we probably want to just kill
4310 * the stream, this is to be revisited the day we handle multiple
4311 * streams in one server connection.
4312 */
4313 cs = objt_cs(s->si[1].end);
4314 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315
Willy Tarreau4213a112013-12-15 10:25:42 +01004316 /* unless we're doing keep-alive, we want to quickly close the connection
4317 * to the server.
4318 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004319 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004320 !si_conn_ready(&s->si[1])) {
4321 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4322 si_shutr(&s->si[1]);
4323 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004324 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325
Willy Tarreaue7dff022015-04-03 01:14:29 +02004326 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004327 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004328 if (unlikely(s->srv_conn))
4329 sess_change_server(s, NULL);
4330 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331
4332 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004333 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004334
Willy Tarreaueee5b512015-04-03 23:46:31 +02004335 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336 int n;
4337
Willy Tarreaueee5b512015-04-03 23:46:31 +02004338 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339 if (n < 1 || n > 5)
4340 n = 0;
4341
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004342 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004343 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004344 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004345 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004346 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004347 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4348 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004349 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004350 }
4351
4352 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004353 s->logs.bytes_in -= s->req.buf->i;
4354 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004355
4356 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004357 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004358 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004359 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360 s->do_log(s);
4361 }
4362
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004363 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004364 stream_stop_content_counters(s);
4365 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004366
Willy Tarreau610ecce2010-01-04 21:15:02 +01004367 s->logs.accept_date = date; /* user-visible date for logging */
4368 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004369 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4370 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004371 tv_zero(&s->logs.tv_request);
4372 s->logs.t_queue = -1;
4373 s->logs.t_connect = -1;
4374 s->logs.t_data = -1;
4375 s->logs.t_close = 0;
4376 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4377 s->logs.srv_queue_size = 0; /* we will get this number soon */
4378
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004379 s->logs.bytes_in = s->req.total = s->req.buf->i;
4380 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004381
4382 if (s->pend_pos)
4383 pendconn_free(s->pend_pos);
4384
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004385 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004386 if (s->flags & SF_CURR_SESS) {
4387 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004388 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004390 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004391 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 }
4393
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004394 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004395
Willy Tarreau4213a112013-12-15 10:25:42 +01004396 /* only release our endpoint if we don't intend to reuse the
4397 * connection.
4398 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004399 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004400 !si_conn_ready(&s->si[1])) {
4401 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004402 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004403 }
4404
Willy Tarreau350f4872014-11-28 14:42:25 +01004405 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4406 s->si[1].err_type = SI_ET_NONE;
4407 s->si[1].conn_retries = 0; /* used for logging too */
4408 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004409 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 +01004410 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 +01004411 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 +02004412 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4413 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4414 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004415
Willy Tarreaueee5b512015-04-03 23:46:31 +02004416 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004418 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004419
4420 if (prev_status == 401 || prev_status == 407) {
4421 /* In HTTP keep-alive mode, if we receive a 401, we still have
4422 * a chance of being able to send the visitor again to the same
4423 * server over the same connection. This is required by some
4424 * broken protocols such as NTLM, and anyway whenever there is
4425 * an opportunity for sending the challenge to the proper place,
4426 * it's better to do it (at least it helps with debugging).
4427 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004428 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004429 if (srv_conn)
4430 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004431 }
4432
Willy Tarreau53f96852016-02-02 18:50:47 +01004433 /* Never ever allow to reuse a connection from a non-reuse backend */
4434 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4435 srv_conn->flags |= CO_FL_PRIVATE;
4436
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004437 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004438 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004439
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004440 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004441 s->req.flags |= CF_NEVER_WAIT;
4442 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004443 }
4444
Willy Tarreau714ea782015-11-25 20:11:11 +01004445 /* we're removing the analysers, we MUST re-enable events detection.
4446 * We don't enable close on the response channel since it's either
4447 * already closed, or in keep-alive with an idle connection handler.
4448 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004449 channel_auto_read(&s->req);
4450 channel_auto_close(&s->req);
4451 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004452
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004453 /* we're in keep-alive with an idle connection, monitor it if not already done */
4454 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004455 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004456 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004457 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004458 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004459 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004460 else if (prev_flags & TX_NOT_FIRST)
4461 /* note: we check the request, not the connection, but
4462 * this is valid for strategies SAFE and AGGR, and in
4463 * case of ALWS, we don't care anyway.
4464 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004465 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004466 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004467 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004468 }
Christopher Faulete6006242017-03-10 11:52:44 +01004469 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4470 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004471}
4472
4473
4474/* This function updates the request state machine according to the response
4475 * state machine and buffer flags. It returns 1 if it changes anything (flag
4476 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4477 * it is only used to find when a request/response couple is complete. Both
4478 * this function and its equivalent should loop until both return zero. It
4479 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4480 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004481int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004483 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004484 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004485 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486 unsigned int old_state = txn->req.msg_state;
4487
Christopher Faulet4be98032017-07-18 10:48:24 +02004488 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004489 return 0;
4490
4491 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004492 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004493 * We can shut the read side unless we want to abort_on_close,
4494 * or we have a POST request. The issue with POST requests is
4495 * that some browsers still send a CRLF after the request, and
4496 * this CRLF must be read so that it does not remain in the kernel
4497 * buffers, otherwise a close could cause an RST on some systems
4498 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004499 * Note that if we're using keep-alive on the client side, we'd
4500 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004501 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004502 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004503 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004504 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4505 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004506 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4507 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004508 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004509 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004510
Willy Tarreau40f151a2012-12-20 12:10:09 +01004511 /* if the server closes the connection, we want to immediately react
4512 * and close the socket to save packets and syscalls.
4513 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004514 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004515
Willy Tarreau7f876a12015-11-18 11:59:55 +01004516 /* In any case we've finished parsing the request so we must
4517 * disable Nagle when sending data because 1) we're not going
4518 * to shut this side, and 2) the server is waiting for us to
4519 * send pending data.
4520 */
4521 chn->flags |= CF_NEVER_WAIT;
4522
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4524 goto wait_other_side;
4525
4526 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4527 /* The server has not finished to respond, so we
4528 * don't want to move in order not to upset it.
4529 */
4530 goto wait_other_side;
4531 }
4532
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 /* When we get here, it means that both the request and the
4534 * response have finished receiving. Depending on the connection
4535 * mode, we'll have to wait for the last bytes to leave in either
4536 * direction, and sometimes for a close to be effective.
4537 */
4538
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004539 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4540 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004541 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4542 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004543 }
4544 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4545 /* Option forceclose is set, or either side wants to close,
4546 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004547 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004548 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004549 *
4550 * However, there is an exception if the response
4551 * length is undefined. In this case, we need to wait
4552 * the close from the server. The response will be
4553 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004554 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004555 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4556 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4557 goto check_channel_flags;
4558
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004559 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4560 channel_shutr_now(chn);
4561 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004563 }
4564 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004565 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4566 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004567 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004568 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4569 channel_auto_read(chn);
4570 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004571 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004572 }
4573
Christopher Faulet4be98032017-07-18 10:48:24 +02004574 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004575 }
4576
4577 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4578 http_msg_closing:
4579 /* nothing else to forward, just waiting for the output buffer
4580 * to be empty and for the shutw_now to take effect.
4581 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004582 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004583 txn->req.msg_state = HTTP_MSG_CLOSED;
4584 goto http_msg_closed;
4585 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004586 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004587 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004590 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004591 }
4592
4593 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4594 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004595 /* if we don't know whether the server will close, we need to hard close */
4596 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4597 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4598
Willy Tarreau3988d932013-12-27 23:03:08 +01004599 /* see above in MSG_DONE why we only do this in these states */
4600 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4601 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004602 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4603 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004604 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605 goto wait_other_side;
4606 }
4607
Christopher Faulet4be98032017-07-18 10:48:24 +02004608 check_channel_flags:
4609 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4610 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4611 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004612 txn->req.msg_state = HTTP_MSG_CLOSING;
4613 goto http_msg_closing;
4614 }
4615
4616
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004618 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619}
4620
4621
4622/* This function updates the response state machine according to the request
4623 * state machine and buffer flags. It returns 1 if it changes anything (flag
4624 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4625 * it is only used to find when a request/response couple is complete. Both
4626 * this function and its equivalent should loop until both return zero. It
4627 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4628 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004629int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004630{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004631 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004632 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004633 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004634 unsigned int old_state = txn->rsp.msg_state;
4635
Christopher Faulet4be98032017-07-18 10:48:24 +02004636 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637 return 0;
4638
4639 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4640 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004641 * still monitor the server connection for a possible close
4642 * while the request is being uploaded, so we don't disable
4643 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004644 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004645 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004646
4647 if (txn->req.msg_state == HTTP_MSG_ERROR)
4648 goto wait_other_side;
4649
4650 if (txn->req.msg_state < HTTP_MSG_DONE) {
4651 /* The client seems to still be sending data, probably
4652 * because we got an error response during an upload.
4653 * We have the choice of either breaking the connection
4654 * or letting it pass through. Let's do the later.
4655 */
4656 goto wait_other_side;
4657 }
4658
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 /* When we get here, it means that both the request and the
4660 * response have finished receiving. Depending on the connection
4661 * mode, we'll have to wait for the last bytes to leave in either
4662 * direction, and sometimes for a close to be effective.
4663 */
4664
4665 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4666 /* Server-close mode : shut read and wait for the request
4667 * side to close its output buffer. The caller will detect
4668 * when we're in DONE and the other is in CLOSED and will
4669 * catch that for the final cleanup.
4670 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004671 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4672 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004673 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004674 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4675 /* Option forceclose is set, or either side wants to close,
4676 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004677 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004678 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004679 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004680 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004681 channel_shutr_now(chn);
4682 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004683 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684 }
4685 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004686 /* The last possible modes are keep-alive and tunnel. Tunnel will
4687 * need to forward remaining data. Keep-alive will need to monitor
4688 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004689 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004690 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004691 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004692 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4693 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004694 }
4695
Christopher Faulet4be98032017-07-18 10:48:24 +02004696 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697 }
4698
4699 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4700 http_msg_closing:
4701 /* nothing else to forward, just waiting for the output buffer
4702 * to be empty and for the shutw_now to take effect.
4703 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004704 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004705 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4706 goto http_msg_closed;
4707 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004708 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004709 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004711 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004712 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004713 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004715 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 }
4717
4718 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4719 http_msg_closed:
4720 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004721 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004722 channel_auto_close(chn);
4723 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004724 goto wait_other_side;
4725 }
4726
Christopher Faulet4be98032017-07-18 10:48:24 +02004727 check_channel_flags:
4728 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4729 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4730 /* if we've just closed an output, let's switch */
4731 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4732 goto http_msg_closing;
4733 }
4734
Willy Tarreau610ecce2010-01-04 21:15:02 +01004735 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004736 /* We force the response to leave immediately if we're waiting for the
4737 * other side, since there is no pending shutdown to push it out.
4738 */
4739 if (!channel_is_empty(chn))
4740 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004741 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742}
4743
4744
Christopher Faulet894da4c2017-07-18 11:29:07 +02004745/* Resync the request and response state machines. */
4746void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004747{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004748 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004749#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004750 int old_req_state = txn->req.msg_state;
4751 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004752#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004753
Willy Tarreau610ecce2010-01-04 21:15:02 +01004754 http_sync_req_state(s);
4755 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004756 if (!http_sync_res_state(s))
4757 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004758 if (!http_sync_req_state(s))
4759 break;
4760 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004761
Christopher Faulet894da4c2017-07-18 11:29:07 +02004762 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4763 "req->analysers=0x%08x res->analysers=0x%08x\n",
4764 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004765 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4766 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004767 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004768
4769
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770 /* OK, both state machines agree on a compatible state.
4771 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004772 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4773 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004774 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4775 * means we must abort the request.
4776 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4777 * corresponding channel.
4778 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4779 * on the response with server-close mode means we've completed one
4780 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004781 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004782 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4783 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004784 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004785 channel_auto_close(&s->req);
4786 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004787 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004788 channel_auto_close(&s->res);
4789 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004790 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004791 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4792 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004793 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004794 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004795 channel_auto_close(&s->res);
4796 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004797 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004798 channel_abort(&s->req);
4799 channel_auto_close(&s->req);
4800 channel_auto_read(&s->req);
4801 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004803 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4804 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4805 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4806 s->req.analysers &= AN_REQ_FLT_END;
4807 if (HAS_REQ_DATA_FILTERS(s))
4808 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4809 }
4810 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4811 s->res.analysers &= AN_RES_FLT_END;
4812 if (HAS_RSP_DATA_FILTERS(s))
4813 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4814 }
4815 channel_auto_close(&s->req);
4816 channel_auto_read(&s->req);
4817 channel_auto_close(&s->res);
4818 channel_auto_read(&s->res);
4819 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004820 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4821 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004822 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004823 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4824 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4825 /* server-close/keep-alive: terminate this transaction,
4826 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004827 * a fresh-new transaction, but only once we're sure there's
4828 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004829 * another request. They must not hold any pending output data
4830 * and the response buffer must realigned
4831 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004832 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004833 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004834 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004835 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004836 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004837 else {
4838 s->req.analysers = AN_REQ_FLT_END;
4839 s->res.analysers = AN_RES_FLT_END;
4840 txn->flags |= TX_WAIT_CLEANUP;
4841 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004842 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004843}
4844
Willy Tarreaud98cf932009-12-27 22:54:55 +01004845/* This function is an analyser which forwards request body (including chunk
4846 * sizes if any). It is called as soon as we must forward, even if we forward
4847 * zero byte. The only situation where it must not be called is when we're in
4848 * tunnel mode and we want to forward till the close. It's used both to forward
4849 * remaining data and to resync after end of body. It expects the msg_state to
4850 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004851 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004852 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004853 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004854 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004855int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004857 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004858 struct http_txn *txn = s->txn;
4859 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004860 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004861
Christopher Faulet814d2702017-03-30 11:33:44 +02004862 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4863 now_ms, __FUNCTION__,
4864 s,
4865 req,
4866 req->rex, req->wex,
4867 req->flags,
4868 req->buf->i,
4869 req->analysers);
4870
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004871 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4872 return 0;
4873
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004874 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004875 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004876 /* Output closed while we were sending data. We must abort and
4877 * wake the other side up.
4878 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004879 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004880 msg->msg_state = HTTP_MSG_ERROR;
4881 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004882 return 1;
4883 }
4884
Willy Tarreaud98cf932009-12-27 22:54:55 +01004885 /* Note that we don't have to send 100-continue back because we don't
4886 * need the data to complete our job, and it's up to the server to
4887 * decide whether to return 100, 417 or anything else in return of
4888 * an "Expect: 100-continue" header.
4889 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004890 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004891 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4892 ? HTTP_MSG_CHUNK_SIZE
4893 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004894
4895 /* TODO/filters: when http-buffer-request option is set or if a
4896 * rule on url_param exists, the first chunk size could be
4897 * already parsed. In that case, msg->next is after the chunk
4898 * size (including the CRLF after the size). So this case should
4899 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004900 }
4901
Willy Tarreau7ba23542014-04-17 21:50:00 +02004902 /* Some post-connect processing might want us to refrain from starting to
4903 * forward data. Currently, the only reason for this is "balance url_param"
4904 * whichs need to parse/process the request after we've enabled forwarding.
4905 */
4906 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004907 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004908 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004909 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004910 channel_dont_close(req); /* don't fail on early shutr */
4911 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004912 }
4913 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4914 }
4915
Willy Tarreau80a92c02014-03-12 10:41:13 +01004916 /* in most states, we should abort in case of early close */
4917 channel_auto_close(req);
4918
Willy Tarreauefdf0942014-04-24 20:08:57 +02004919 if (req->to_forward) {
4920 /* We can't process the buffer's contents yet */
4921 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004922 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004923 }
4924
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004925 if (msg->msg_state < HTTP_MSG_DONE) {
4926 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4927 ? http_msg_forward_chunked_body(s, msg)
4928 : http_msg_forward_body(s, msg));
4929 if (!ret)
4930 goto missing_data_or_waiting;
4931 if (ret < 0)
4932 goto return_bad_req;
4933 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004934
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004935 /* other states, DONE...TUNNEL */
4936 /* we don't want to forward closes on DONE except in tunnel mode. */
4937 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4938 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004939
Christopher Faulet894da4c2017-07-18 11:29:07 +02004940 http_resync_states(s);
4941 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004942 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4943 if (req->flags & CF_SHUTW) {
4944 /* request errors are most likely due to the
4945 * server aborting the transfer. */
4946 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004947 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004948 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004949 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004950 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004952 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004953 }
4954
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004955 /* If "option abortonclose" is set on the backend, we want to monitor
4956 * the client's connection and forward any shutdown notification to the
4957 * server, which will decide whether to close or to go on processing the
4958 * request. We only do that in tunnel mode, and not in other modes since
4959 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004960 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004961 channel_auto_read(req);
4962 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4963 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4964 s->si[1].flags |= SI_FL_NOLINGER;
4965 channel_auto_close(req);
4966 }
4967 else if (s->txn->meth == HTTP_METH_POST) {
4968 /* POST requests may require to read extra CRLF sent by broken
4969 * browsers and which could cause an RST to be sent upon close
4970 * on some systems (eg: Linux). */
4971 channel_auto_read(req);
4972 }
4973 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004974
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004975 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004976 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004977 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004978 if (!(s->flags & SF_ERR_MASK))
4979 s->flags |= SF_ERR_CLICL;
4980 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004981 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004982 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004983 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004984 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004985 }
4986
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004987 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4988 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004989 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004990 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004991
4992 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004993 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004994
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004995 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004996 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004997 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004998 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005000 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005001 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01005002 * And when content-length is used, we never want to let the possible
5003 * shutdown be forwarded to the other side, as the state machine will
5004 * take care of it once the client responds. It's also important to
5005 * prevent TIME_WAITs from accumulating on the backend side, and for
5006 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005007 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01005008 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005009 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005010
Willy Tarreau5c620922011-05-11 19:56:11 +02005011 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005012 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005013 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005014 * modes are already handled by the stream sock layer. We must not do
5015 * this in content-length mode because it could present the MSG_MORE
5016 * flag with the last block of forwarded data, which would cause an
5017 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005018 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005019 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005020 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005021
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005022 return 0;
5023
Willy Tarreaud98cf932009-12-27 22:54:55 +01005024 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005025 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005026 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005027 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02005028
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005029 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005030 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005031 txn->req.msg_state = HTTP_MSG_ERROR;
5032 if (txn->status) {
5033 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005034 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005035 } else {
5036 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005037 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005038 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005039 req->analysers &= AN_REQ_FLT_END;
5040 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 +01005041
Willy Tarreaue7dff022015-04-03 01:14:29 +02005042 if (!(s->flags & SF_ERR_MASK))
5043 s->flags |= SF_ERR_PRXCOND;
5044 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005045 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005046 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005047 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005048 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005049 }
5050 return 0;
5051
5052 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005053 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005054 txn->req.msg_state = HTTP_MSG_ERROR;
5055 if (txn->status) {
5056 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005057 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005058 } else {
5059 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005060 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005061 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005062 req->analysers &= AN_REQ_FLT_END;
5063 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 +01005064
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005065 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5066 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005067 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005068 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005069
Willy Tarreaue7dff022015-04-03 01:14:29 +02005070 if (!(s->flags & SF_ERR_MASK))
5071 s->flags |= SF_ERR_SRVCL;
5072 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005073 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005074 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005075 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005076 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005077 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005078 return 0;
5079}
5080
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005081/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5082 * processing can continue on next analysers, or zero if it either needs more
5083 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005084 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005085 * when it has nothing left to do, and may remove any analyser when it wants to
5086 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005087 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005088int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005089{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005090 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005091 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005093 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005094 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005095 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005096 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005097
Willy Tarreau87b09662015-04-03 00:22:06 +02005098 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 +02005099 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005100 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005101 rep,
5102 rep->rex, rep->wex,
5103 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005104 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005105 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005106
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005107 /*
5108 * Now parse the partial (or complete) lines.
5109 * We will check the response syntax, and also join multi-line
5110 * headers. An index of all the lines will be elaborated while
5111 * parsing.
5112 *
5113 * For the parsing, we use a 28 states FSM.
5114 *
5115 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005116 * rep->buf->p = beginning of response
5117 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5118 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005119 * msg->eol = end of current header or line (LF or CRLF)
5120 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 */
5122
Willy Tarreau628c40c2014-04-24 19:11:26 +02005123 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005124 /* There's a protected area at the end of the buffer for rewriting
5125 * purposes. We don't want to start to parse the request if the
5126 * protected area is affected, because we may have to move processed
5127 * data later, which is much more complicated.
5128 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02005129 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005130 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005131 /* some data has still not left the buffer, wake us once that's done */
5132 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5133 goto abort_response;
5134 channel_dont_close(rep);
5135 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005136 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005137 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005138 }
5139
Willy Tarreau188e2302018-06-15 11:11:53 +02005140 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02005141 ci_tail(rep) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02005142 channel_slow_realign(rep, trash.str);
Willy Tarreau379357a2013-06-08 12:55:46 +02005143
Willy Tarreau9b28e032012-10-12 23:49:43 +02005144 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005145 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005146 }
5147
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 /* 1: we might have to print this header in debug mode */
5149 if (unlikely((global.mode & MODE_DEBUG) &&
5150 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005151 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005152 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005153
Willy Tarreau9b28e032012-10-12 23:49:43 +02005154 sol = rep->buf->p;
5155 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005156 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005157
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005158 sol += hdr_idx_first_pos(&txn->hdr_idx);
5159 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005160
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005161 while (cur_idx) {
5162 eol = sol + txn->hdr_idx.v[cur_idx].len;
5163 debug_hdr("srvhdr", s, sol, eol);
5164 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5165 cur_idx = txn->hdr_idx.v[cur_idx].next;
5166 }
5167 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005169 /*
5170 * Now we quickly check if we have found a full valid response.
5171 * If not so, we check the FD and buffer states before leaving.
5172 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005173 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005174 * responses are checked first.
5175 *
5176 * Depending on whether the client is still there or not, we
5177 * may send an error response back or not. Note that normally
5178 * we should only check for HTTP status there, and check I/O
5179 * errors somewhere else.
5180 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005181
Willy Tarreau655dce92009-11-08 13:10:58 +01005182 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005183 /* Invalid response */
5184 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5185 /* we detected a parsing error. We want to archive this response
5186 * in the dedicated proxy area for later troubleshooting.
5187 */
5188 hdr_response_bad:
5189 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005190 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005192 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005193 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005194 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005196 }
Willy Tarreau64648412010-03-05 10:41:54 +01005197 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005198 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005199 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005200 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005201 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005202 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005203 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005204
Willy Tarreaue7dff022015-04-03 01:14:29 +02005205 if (!(s->flags & SF_ERR_MASK))
5206 s->flags |= SF_ERR_PRXCOND;
5207 if (!(s->flags & SF_FINST_MASK))
5208 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209
5210 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005211 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005212
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005213 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02005214 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005215 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005216 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005217 goto hdr_response_bad;
5218 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005219
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005221 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005222 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005223 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005224 else if (txn->flags & TX_NOT_FIRST)
5225 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005226
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005227 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005228 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005229 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005230 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005231 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005232
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005233 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005234 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005235 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005236
5237 /* Check to see if the server refused the early data.
5238 * If so, just send a 425
5239 */
5240 if (objt_cs(s->si[1].end)) {
5241 struct connection *conn = objt_cs(s->si[1].end)->conn;
5242
5243 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5244 txn->status = 425;
5245 }
5246
Willy Tarreau350f4872014-11-28 14:42:25 +01005247 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005248 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005249 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005250
Willy Tarreaue7dff022015-04-03 01:14:29 +02005251 if (!(s->flags & SF_ERR_MASK))
5252 s->flags |= SF_ERR_SRVCL;
5253 if (!(s->flags & SF_FINST_MASK))
5254 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005255 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005256 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005257
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005258 /* read timeout : return a 504 to the client. */
5259 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005260 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005261 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005262
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005263 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005264 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005265 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005266 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005267 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005268
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005269 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005270 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005271 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005272 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005273 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005274 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005275
Willy Tarreaue7dff022015-04-03 01:14:29 +02005276 if (!(s->flags & SF_ERR_MASK))
5277 s->flags |= SF_ERR_SRVTO;
5278 if (!(s->flags & SF_FINST_MASK))
5279 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005280 return 0;
5281 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005282
Willy Tarreauf003d372012-11-26 13:35:37 +01005283 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005284 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005285 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5286 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005287 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005288 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005289
Christopher Faulet0184ea72017-01-05 14:06:34 +01005290 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005291 channel_auto_close(rep);
5292
5293 txn->status = 400;
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 Tarreauf003d372012-11-26 13:35:37 +01005296
Willy Tarreaue7dff022015-04-03 01:14:29 +02005297 if (!(s->flags & SF_ERR_MASK))
5298 s->flags |= SF_ERR_CLICL;
5299 if (!(s->flags & SF_FINST_MASK))
5300 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005301
Willy Tarreau87b09662015-04-03 00:22:06 +02005302 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005303 return 0;
5304 }
5305
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005306 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005307 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005308 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005309 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005310 else if (txn->flags & TX_NOT_FIRST)
5311 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005312
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005313 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005314 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005315 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005316 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005317 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005318
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005319 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005320 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005321 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005322 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005323 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005324 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005325
Willy Tarreaue7dff022015-04-03 01:14:29 +02005326 if (!(s->flags & SF_ERR_MASK))
5327 s->flags |= SF_ERR_SRVCL;
5328 if (!(s->flags & SF_FINST_MASK))
5329 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 return 0;
5331 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005332
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005333 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005334 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005336 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005337 else if (txn->flags & TX_NOT_FIRST)
5338 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005339
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005340 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005341 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005342 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005343
Willy Tarreaue7dff022015-04-03 01:14:29 +02005344 if (!(s->flags & SF_ERR_MASK))
5345 s->flags |= SF_ERR_CLICL;
5346 if (!(s->flags & SF_FINST_MASK))
5347 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005348
Willy Tarreau87b09662015-04-03 00:22:06 +02005349 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005350 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005351 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005352
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005353 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005354 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005355 return 0;
5356 }
5357
5358 /* More interesting part now : we know that we have a complete
5359 * response which at least looks like HTTP. We have an indicator
5360 * of each header's length, so we can parse them quickly.
5361 */
5362
5363 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005364 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005365
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005366 /*
5367 * 1: get the status code
5368 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005369 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005370 if (n < 1 || n > 5)
5371 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005372 /* when the client triggers a 4xx from the server, it's most often due
5373 * to a missing object or permission. These events should be tracked
5374 * because if they happen often, it may indicate a brute force or a
5375 * vulnerability scan.
5376 */
5377 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005378 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005379
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005380 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005381 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005382
Willy Tarreau91852eb2015-05-01 13:26:00 +02005383 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5384 * exactly one digit "." one digit. This check may be disabled using
5385 * option accept-invalid-http-response.
5386 */
5387 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5388 if (msg->sl.st.v_l != 8) {
5389 msg->err_pos = 0;
5390 goto hdr_response_bad;
5391 }
5392
5393 if (rep->buf->p[4] != '/' ||
5394 !isdigit((unsigned char)rep->buf->p[5]) ||
5395 rep->buf->p[6] != '.' ||
5396 !isdigit((unsigned char)rep->buf->p[7])) {
5397 msg->err_pos = 4;
5398 goto hdr_response_bad;
5399 }
5400 }
5401
Willy Tarreau5b154472009-12-21 20:11:07 +01005402 /* check if the response is HTTP/1.1 or above */
5403 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005404 ((rep->buf->p[5] > '1') ||
5405 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005406 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005407
5408 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005409 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 +01005410
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005411 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005412 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005413
Willy Tarreau9b28e032012-10-12 23:49:43 +02005414 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005415
Willy Tarreau39650402010-03-15 19:44:39 +01005416 /* Adjust server's health based on status code. Note: status codes 501
5417 * and 505 are triggered on demand by client request, so we must not
5418 * count them as server failures.
5419 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005420 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005421 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005422 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005423 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005424 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005425 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005426
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005427 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005428 * We may be facing a 100-continue response, or any other informational
5429 * 1xx response which is non-final, in which case this is not the right
5430 * response, and we're waiting for the next one. Let's allow this response
5431 * to go to the client and wait for the next one. There's an exception for
5432 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005434 if (txn->status < 200 &&
5435 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005436 hdr_idx_init(&txn->hdr_idx);
5437 msg->next -= channel_forward(rep, msg->next);
5438 msg->msg_state = HTTP_MSG_RPBEFORE;
5439 txn->status = 0;
5440 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005441 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005442 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005443 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005444
Willy Tarreaua14ad722017-07-07 11:36:32 +02005445 /*
5446 * 2: check for cacheability.
5447 */
5448
5449 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005450 case 200:
5451 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005452 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005453 case 206:
5454 case 300:
5455 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005456 case 404:
5457 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005458 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005459 case 414:
5460 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005461 break;
5462 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005463 /* RFC7231#6.1:
5464 * Responses with status codes that are defined as
5465 * cacheable by default (e.g., 200, 203, 204, 206,
5466 * 300, 301, 404, 405, 410, 414, and 501 in this
5467 * specification) can be reused by a cache with
5468 * heuristic expiration unless otherwise indicated
5469 * by the method definition or explicit cache
5470 * controls [RFC7234]; all other status codes are
5471 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005472 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005473 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005474 break;
5475 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005476
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005477 /*
5478 * 3: we may need to capture headers
5479 */
5480 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005481 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005482 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005483 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005484
Willy Tarreau557f1992015-05-01 10:05:17 +02005485 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5486 * by RFC7230#3.3.3 :
5487 *
5488 * The length of a message body is determined by one of the following
5489 * (in order of precedence):
5490 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005491 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5492 * the connection will become a tunnel immediately after the empty
5493 * line that concludes the header fields. A client MUST ignore
5494 * any Content-Length or Transfer-Encoding header fields received
5495 * in such a message. Any 101 response (Switching Protocols) is
5496 * managed in the same manner.
5497 *
5498 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005499 * (Informational), 204 (No Content), or 304 (Not Modified) status
5500 * code is always terminated by the first empty line after the
5501 * header fields, regardless of the header fields present in the
5502 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005503 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005504 * 3. If a Transfer-Encoding header field is present and the chunked
5505 * transfer coding (Section 4.1) is the final encoding, the message
5506 * body length is determined by reading and decoding the chunked
5507 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005508 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005509 * If a Transfer-Encoding header field is present in a response and
5510 * the chunked transfer coding is not the final encoding, the
5511 * message body length is determined by reading the connection until
5512 * it is closed by the server. If a Transfer-Encoding header field
5513 * is present in a request and the chunked transfer coding is not
5514 * the final encoding, the message body length cannot be determined
5515 * reliably; the server MUST respond with the 400 (Bad Request)
5516 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005517 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005518 * If a message is received with both a Transfer-Encoding and a
5519 * Content-Length header field, the Transfer-Encoding overrides the
5520 * Content-Length. Such a message might indicate an attempt to
5521 * perform request smuggling (Section 9.5) or response splitting
5522 * (Section 9.4) and ought to be handled as an error. A sender MUST
5523 * remove the received Content-Length field prior to forwarding such
5524 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005525 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005526 * 4. If a message is received without Transfer-Encoding and with
5527 * either multiple Content-Length header fields having differing
5528 * field-values or a single Content-Length header field having an
5529 * invalid value, then the message framing is invalid and the
5530 * recipient MUST treat it as an unrecoverable error. If this is a
5531 * request message, the server MUST respond with a 400 (Bad Request)
5532 * status code and then close the connection. If this is a response
5533 * message received by a proxy, the proxy MUST close the connection
5534 * to the server, discard the received response, and send a 502 (Bad
5535 * Gateway) response to the client. If this is a response message
5536 * received by a user agent, the user agent MUST close the
5537 * connection to the server and discard the received response.
5538 *
5539 * 5. If a valid Content-Length header field is present without
5540 * Transfer-Encoding, its decimal value defines the expected message
5541 * body length in octets. If the sender closes the connection or
5542 * the recipient times out before the indicated number of octets are
5543 * received, the recipient MUST consider the message to be
5544 * incomplete and close the connection.
5545 *
5546 * 6. If this is a request message and none of the above are true, then
5547 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005548 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005549 * 7. Otherwise, this is a response message without a declared message
5550 * body length, so the message body length is determined by the
5551 * number of octets received prior to the server closing the
5552 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005553 */
5554
5555 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005556 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005557 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005558 * FIXME: should we parse anyway and return an error on chunked encoding ?
5559 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005560 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5561 txn->status == 101)) {
5562 /* Either we've established an explicit tunnel, or we're
5563 * switching the protocol. In both cases, we're very unlikely
5564 * to understand the next protocols. We have to switch to tunnel
5565 * mode, so that we transfer the request and responses then let
5566 * this protocol pass unmodified. When we later implement specific
5567 * parsers for such protocols, we'll want to check the Upgrade
5568 * header which contains information about that protocol for
5569 * responses with status 101 (eg: see RFC2817 about TLS).
5570 */
5571 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5572 msg->flags |= HTTP_MSGF_XFER_LEN;
5573 goto end;
5574 }
5575
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005576 if (txn->meth == HTTP_METH_HEAD ||
5577 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005578 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005579 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005580 goto skip_content_length;
5581 }
5582
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005583 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005584 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005585 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005586 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005587 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5588 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005589 /* bad transfer-encoding (chunked followed by something else) */
5590 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005591 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005592 break;
5593 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005594 }
5595
Willy Tarreau1c913912015-04-30 10:57:51 +02005596 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005597 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005598 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005599 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5600 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5601 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005602 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005603 signed long long cl;
5604
Willy Tarreauad14f752011-09-02 20:33:27 +02005605 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005606 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005607 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005608 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005609
Willy Tarreauad14f752011-09-02 20:33:27 +02005610 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005611 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005612 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005613 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005614
Willy Tarreauad14f752011-09-02 20:33:27 +02005615 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005616 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005617 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005618 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005619
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005620 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005621 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005622 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005623 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005624
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005625 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005626 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005627 }
5628
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005629 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005630 /* Now we have to check if we need to modify the Connection header.
5631 * This is more difficult on the response than it is on the request,
5632 * because we can have two different HTTP versions and we don't know
5633 * how the client will interprete a response. For instance, let's say
5634 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5635 * HTTP/1.1 response without any header. Maybe it will bound itself to
5636 * HTTP/1.0 because it only knows about it, and will consider the lack
5637 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5638 * the lack of header as a keep-alive. Thus we will use two flags
5639 * indicating how a request MAY be understood by the client. In case
5640 * of multiple possibilities, we'll fix the header to be explicit. If
5641 * ambiguous cases such as both close and keepalive are seen, then we
5642 * will fall back to explicit close. Note that we won't take risks with
5643 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005644 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005645 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005646 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5647 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5648 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5649 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005650 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005651
Willy Tarreau70dffda2014-01-30 03:07:23 +01005652 /* this situation happens when combining pretend-keepalive with httpclose. */
5653 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005654 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005655 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005656 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5657
Willy Tarreau60466522010-01-18 19:08:45 +01005658 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005659 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005660 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5661 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005662
Willy Tarreau60466522010-01-18 19:08:45 +01005663 /* now adjust header transformations depending on current state */
5664 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5665 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5666 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005667 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005668 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005669 }
Willy Tarreau60466522010-01-18 19:08:45 +01005670 else { /* SCL / KAL */
5671 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005672 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005673 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005674 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005675
Willy Tarreau60466522010-01-18 19:08:45 +01005676 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005677 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005678
Willy Tarreau60466522010-01-18 19:08:45 +01005679 /* Some keep-alive responses are converted to Server-close if
5680 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005681 */
Willy Tarreau60466522010-01-18 19:08:45 +01005682 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5683 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005684 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005685 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005686 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005687 }
5688
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005689 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005690 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005691 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005692
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005693 /* end of job, return OK */
5694 rep->analysers &= ~an_bit;
5695 rep->analyse_exp = TICK_ETERNITY;
5696 channel_auto_close(rep);
5697 return 1;
5698
5699 abort_keep_alive:
5700 /* A keep-alive request to the server failed on a network error.
5701 * The client is required to retry. We need to close without returning
5702 * any other information so that the client retries.
5703 */
5704 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005705 rep->analysers &= AN_RES_FLT_END;
5706 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005707 channel_auto_close(rep);
5708 s->logs.logwait = 0;
5709 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005710 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005711 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005712 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005713 return 0;
5714}
5715
5716/* This function performs all the processing enabled for the current response.
5717 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005718 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005719 * other functions. It works like process_request (see indications above).
5720 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005721int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005722{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005723 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005724 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005725 struct http_msg *msg = &txn->rsp;
5726 struct proxy *cur_proxy;
5727 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005728 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005729
Willy Tarreau87b09662015-04-03 00:22:06 +02005730 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 +02005731 now_ms, __FUNCTION__,
5732 s,
5733 rep,
5734 rep->rex, rep->wex,
5735 rep->flags,
5736 rep->buf->i,
5737 rep->analysers);
5738
5739 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5740 return 0;
5741
Willy Tarreau70730dd2014-04-24 18:06:27 +02005742 /* The stats applet needs to adjust the Connection header but we don't
5743 * apply any filter there.
5744 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005745 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5746 rep->analysers &= ~an_bit;
5747 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005748 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005749 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005750
Willy Tarreau58975672014-04-24 21:13:57 +02005751 /*
5752 * We will have to evaluate the filters.
5753 * As opposed to version 1.2, now they will be evaluated in the
5754 * filters order and not in the header order. This means that
5755 * each filter has to be validated among all headers.
5756 *
5757 * Filters are tried with ->be first, then with ->fe if it is
5758 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005759 *
5760 * Maybe we are in resume condiion. In this case I choose the
5761 * "struct proxy" which contains the rule list matching the resume
5762 * pointer. If none of theses "struct proxy" match, I initialise
5763 * the process with the first one.
5764 *
5765 * In fact, I check only correspondance betwwen the current list
5766 * pointer and the ->fe rule list. If it doesn't match, I initialize
5767 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005768 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005769 if (s->current_rule_list == &sess->fe->http_res_rules)
5770 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005771 else
5772 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005773 while (1) {
5774 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005775
Willy Tarreau58975672014-04-24 21:13:57 +02005776 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005777 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005778 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005779
Willy Tarreau51d861a2015-05-22 17:30:48 +02005780 if (ret == HTTP_RULE_RES_BADREQ)
5781 goto return_srv_prx_502;
5782
5783 if (ret == HTTP_RULE_RES_DONE) {
5784 rep->analysers &= ~an_bit;
5785 rep->analyse_exp = TICK_ETERNITY;
5786 return 1;
5787 }
5788 }
5789
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005790 /* we need to be called again. */
5791 if (ret == HTTP_RULE_RES_YIELD) {
5792 channel_dont_close(rep);
5793 return 0;
5794 }
5795
Willy Tarreau58975672014-04-24 21:13:57 +02005796 /* try headers filters */
5797 if (rule_set->rsp_exp != NULL) {
5798 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5799 return_bad_resp:
5800 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005801 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005802 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005803 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005804 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005805 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005806 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005807 txn->status = 502;
5808 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005809 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005810 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005811 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005812 if (!(s->flags & SF_ERR_MASK))
5813 s->flags |= SF_ERR_PRXCOND;
5814 if (!(s->flags & SF_FINST_MASK))
5815 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005816 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005817 }
Willy Tarreau58975672014-04-24 21:13:57 +02005818 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005819
Willy Tarreau58975672014-04-24 21:13:57 +02005820 /* has the response been denied ? */
5821 if (txn->flags & TX_SVDENY) {
5822 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005823 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005824
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005825 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5826 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005827 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005828 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005829
Willy Tarreau58975672014-04-24 21:13:57 +02005830 goto return_srv_prx_502;
5831 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005832
Willy Tarreau58975672014-04-24 21:13:57 +02005833 /* add response headers from the rule sets in the same order */
5834 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005835 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005836 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005837 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005838 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005839 ret = acl_pass(ret);
5840 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5841 ret = !ret;
5842 if (!ret)
5843 continue;
5844 }
5845 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5846 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005847 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005848
Willy Tarreau58975672014-04-24 21:13:57 +02005849 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005850 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005851 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005852 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005853 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005854
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005855 /* After this point, this anayzer can't return yield, so we can
5856 * remove the bit corresponding to this analyzer from the list.
5857 *
5858 * Note that the intermediate returns and goto found previously
5859 * reset the analyzers.
5860 */
5861 rep->analysers &= ~an_bit;
5862 rep->analyse_exp = TICK_ETERNITY;
5863
Willy Tarreau58975672014-04-24 21:13:57 +02005864 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005865 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005866 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005867
Willy Tarreau58975672014-04-24 21:13:57 +02005868 /*
5869 * Now check for a server cookie.
5870 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005871 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005872 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005873
Willy Tarreau58975672014-04-24 21:13:57 +02005874 /*
5875 * Check for cache-control or pragma headers if required.
5876 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005877 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005878 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005879
Willy Tarreau58975672014-04-24 21:13:57 +02005880 /*
5881 * Add server cookie in the response if needed
5882 */
5883 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5884 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005885 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005886 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5887 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5888 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5889 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5890 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005891 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005892 /* the server is known, it's not the one the client requested, or the
5893 * cookie's last seen date needs to be refreshed. We have to
5894 * insert a set-cookie here, except if we want to insert only on POST
5895 * requests and this one isn't. Note that servers which don't have cookies
5896 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005897 */
Willy Tarreau58975672014-04-24 21:13:57 +02005898 if (!objt_server(s->target)->cookie) {
5899 chunk_printf(&trash,
5900 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5901 s->be->cookie_name);
5902 }
5903 else {
5904 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005905
Willy Tarreau58975672014-04-24 21:13:57 +02005906 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5907 /* emit last_date, which is mandatory */
5908 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5909 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5910 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005911
Willy Tarreau58975672014-04-24 21:13:57 +02005912 if (s->be->cookie_maxlife) {
5913 /* emit first_date, which is either the original one or
5914 * the current date.
5915 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005916 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005917 s30tob64(txn->cookie_first_date ?
5918 txn->cookie_first_date >> 2 :
5919 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005920 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005921 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005922 }
Willy Tarreau58975672014-04-24 21:13:57 +02005923 chunk_appendf(&trash, "; path=/");
5924 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005925
Willy Tarreau58975672014-04-24 21:13:57 +02005926 if (s->be->cookie_domain)
5927 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005928
Willy Tarreau58975672014-04-24 21:13:57 +02005929 if (s->be->ck_opts & PR_CK_HTTPONLY)
5930 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005931
Willy Tarreau58975672014-04-24 21:13:57 +02005932 if (s->be->ck_opts & PR_CK_SECURE)
5933 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005934
Willy Tarreau58975672014-04-24 21:13:57 +02005935 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5936 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937
Willy Tarreau58975672014-04-24 21:13:57 +02005938 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005939 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005940 /* the server did not change, only the date was updated */
5941 txn->flags |= TX_SCK_UPDATED;
5942 else
5943 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005944
Willy Tarreau58975672014-04-24 21:13:57 +02005945 /* Here, we will tell an eventual cache on the client side that we don't
5946 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5947 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5948 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005949 */
Willy Tarreau58975672014-04-24 21:13:57 +02005950 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005951
Willy Tarreau58975672014-04-24 21:13:57 +02005952 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005953
Willy Tarreau58975672014-04-24 21:13:57 +02005954 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5955 "Cache-control: private", 22) < 0))
5956 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005957 }
Willy Tarreau58975672014-04-24 21:13:57 +02005958 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959
Willy Tarreau58975672014-04-24 21:13:57 +02005960 /*
5961 * Check if result will be cacheable with a cookie.
5962 * We'll block the response if security checks have caught
5963 * nasty things such as a cacheable cookie.
5964 */
5965 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5966 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5967 (s->be->options & PR_O_CHK_CACHE)) {
5968 /* we're in presence of a cacheable response containing
5969 * a set-cookie header. We'll block it as requested by
5970 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971 */
Willy Tarreau58975672014-04-24 21:13:57 +02005972 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005973 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005974
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005975 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5976 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005977 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005978 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005979
Christopher Faulet767a84b2017-11-24 16:50:31 +01005980 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5981 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005982 send_log(s->be, LOG_ALERT,
5983 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5984 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5985 goto return_srv_prx_502;
5986 }
Willy Tarreau03945942009-12-22 16:50:27 +01005987
Willy Tarreau70730dd2014-04-24 18:06:27 +02005988 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005989 /*
5990 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5991 * If an "Upgrade" token is found, the header is left untouched in order
5992 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005993 * if anything but "Upgrade" is present in the Connection header. We don't
5994 * want to touch any 101 response either since it's switching to another
5995 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005996 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005997 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005998 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005999 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006000 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6001 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006002
Willy Tarreau58975672014-04-24 21:13:57 +02006003 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6004 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6005 /* we want a keep-alive response here. Keep-alive header
6006 * required if either side is not 1.1.
6007 */
6008 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6009 want_flags |= TX_CON_KAL_SET;
6010 }
6011 else {
6012 /* we want a close response here. Close header required if
6013 * the server is 1.1, regardless of the client.
6014 */
6015 if (msg->flags & HTTP_MSGF_VER_11)
6016 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006017 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006018
Willy Tarreau58975672014-04-24 21:13:57 +02006019 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6020 http_change_connection_header(txn, msg, want_flags);
6021 }
6022
6023 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006024 /* Always enter in the body analyzer */
6025 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6026 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006027
Willy Tarreau58975672014-04-24 21:13:57 +02006028 /* if the user wants to log as soon as possible, without counting
6029 * bytes from the server, then this is the right moment. We have
6030 * to temporarily assign bytes_out to log what we currently have.
6031 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006032 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006033 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6034 s->logs.bytes_out = txn->rsp.eoh;
6035 s->do_log(s);
6036 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006037 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006038 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006039}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006040
Willy Tarreaud98cf932009-12-27 22:54:55 +01006041/* This function is an analyser which forwards response body (including chunk
6042 * sizes if any). It is called as soon as we must forward, even if we forward
6043 * zero byte. The only situation where it must not be called is when we're in
6044 * tunnel mode and we want to forward till the close. It's used both to forward
6045 * remaining data and to resync after end of body. It expects the msg_state to
6046 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006047 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006048 *
6049 * It is capable of compressing response data both in content-length mode and
6050 * in chunked mode. The state machines follows different flows depending on
6051 * whether content-length and chunked modes are used, since there are no
6052 * trailers in content-length :
6053 *
6054 * chk-mode cl-mode
6055 * ,----- BODY -----.
6056 * / \
6057 * V size > 0 V chk-mode
6058 * .--> SIZE -------------> DATA -------------> CRLF
6059 * | | size == 0 | last byte |
6060 * | v final crlf v inspected |
6061 * | TRAILERS -----------> DONE |
6062 * | |
6063 * `----------------------------------------------'
6064 *
6065 * Compression only happens in the DATA state, and must be flushed in final
6066 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6067 * is performed at once on final states for all bytes parsed, or when leaving
6068 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006069 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006070int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006071{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006072 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006073 struct http_txn *txn = s->txn;
6074 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006075 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006076
Christopher Faulet814d2702017-03-30 11:33:44 +02006077 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6078 now_ms, __FUNCTION__,
6079 s,
6080 res,
6081 res->rex, res->wex,
6082 res->flags,
6083 res->buf->i,
6084 res->analysers);
6085
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006086 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6087 return 0;
6088
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006089 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006090 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006091 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006092 /* Output closed while we were sending data. We must abort and
6093 * wake the other side up.
6094 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006095 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006096 msg->msg_state = HTTP_MSG_ERROR;
6097 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006098 return 1;
6099 }
6100
Willy Tarreau4fe41902010-06-07 22:27:41 +02006101 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006102 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006103
Christopher Fauletd7c91962015-04-30 11:48:27 +02006104 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006105 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6106 ? HTTP_MSG_CHUNK_SIZE
6107 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006108 }
6109
Willy Tarreauefdf0942014-04-24 20:08:57 +02006110 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006111 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006112 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006113 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006114 }
6115
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006116 if (msg->msg_state < HTTP_MSG_DONE) {
6117 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6118 ? http_msg_forward_chunked_body(s, msg)
6119 : http_msg_forward_body(s, msg));
6120 if (!ret)
6121 goto missing_data_or_waiting;
6122 if (ret < 0)
6123 goto return_bad_res;
6124 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006125
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006126 /* other states, DONE...TUNNEL */
6127 /* for keep-alive we don't want to forward closes on DONE */
6128 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6129 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6130 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006131
Christopher Faulet894da4c2017-07-18 11:29:07 +02006132 http_resync_states(s);
6133 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006134 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6135 if (res->flags & CF_SHUTW) {
6136 /* response errors are most likely due to the
6137 * client aborting the transfer. */
6138 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006139 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006140 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006141 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 +01006142 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006143 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006144 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006145 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006146 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006147
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006148 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006149 if (res->flags & CF_SHUTW)
6150 goto aborted_xfer;
6151
6152 /* stop waiting for data if the input is closed before the end. If the
6153 * client side was already closed, it means that the client has aborted,
6154 * so we don't want to count this as a server abort. Otherwise it's a
6155 * server abort.
6156 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006157 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006158 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006159 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006160 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02006161 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006162 if (!(s->flags & SF_ERR_MASK))
6163 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006164 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006165 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006166 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006167 goto return_bad_res_stats_ok;
6168 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006169 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006170
Willy Tarreau40dba092010-03-04 18:14:51 +01006171 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006172 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006173 goto return_bad_res;
6174
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006175 /* When TE: chunked is used, we need to get there again to parse
6176 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006177 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6178 * or if there are filters registered on the stream, we don't want to
6179 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006180 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006181 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006182 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006183 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6184 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006185 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006186
Willy Tarreau5c620922011-05-11 19:56:11 +02006187 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006188 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006189 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006190 * modes are already handled by the stream sock layer. We must not do
6191 * this in content-length mode because it could present the MSG_MORE
6192 * flag with the last block of forwarded data, which would cause an
6193 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006194 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006195 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006196 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006197
Willy Tarreau87b09662015-04-03 00:22:06 +02006198 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006199 return 0;
6200
Willy Tarreau40dba092010-03-04 18:14:51 +01006201 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006202 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 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.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006205
6206 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006207 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006208 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006209 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006210 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006211 res->analysers &= AN_RES_FLT_END;
6212 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 +01006213 if (objt_server(s->target))
6214 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006215
Willy Tarreaue7dff022015-04-03 01:14:29 +02006216 if (!(s->flags & SF_ERR_MASK))
6217 s->flags |= SF_ERR_PRXCOND;
6218 if (!(s->flags & SF_FINST_MASK))
6219 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006220 return 0;
6221
6222 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006223 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006224 txn->rsp.msg_state = HTTP_MSG_ERROR;
6225 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006226 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006227 res->analysers &= AN_RES_FLT_END;
6228 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 +01006229
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006230 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6231 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006232 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006233 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006234
Willy Tarreaue7dff022015-04-03 01:14:29 +02006235 if (!(s->flags & SF_ERR_MASK))
6236 s->flags |= SF_ERR_CLICL;
6237 if (!(s->flags & SF_FINST_MASK))
6238 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006239 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006240}
6241
6242
6243static inline int
6244http_msg_forward_body(struct stream *s, struct http_msg *msg)
6245{
6246 struct channel *chn = msg->chn;
6247 int ret;
6248
6249 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6250
6251 if (msg->msg_state == HTTP_MSG_ENDING)
6252 goto ending;
6253
6254 /* Neither content-length, nor transfer-encoding was found, so we must
6255 * read the body until the server connection is closed. In that case, we
6256 * eat data as they come. Of course, this happens for response only. */
6257 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6258 unsigned long long len = (chn->buf->i - msg->next);
6259 msg->chunk_len += len;
6260 msg->body_len += len;
6261 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006262 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6263 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6264 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006265 msg->next += ret;
6266 msg->chunk_len -= ret;
6267 if (msg->chunk_len) {
6268 /* input empty or output full */
6269 if (chn->buf->i > msg->next)
6270 chn->flags |= CF_WAKE_WRITE;
6271 goto missing_data_or_waiting;
6272 }
6273
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006274 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6275 * always set for a request. */
6276 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6277 /* The server still sending data that should be filtered */
6278 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6279 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006280 msg->msg_state = HTTP_MSG_TUNNEL;
6281 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006282 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006283
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006284 msg->msg_state = HTTP_MSG_ENDING;
6285
6286 ending:
6287 /* we may have some pending data starting at res->buf->p such as a last
6288 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006289 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6290 /* default_ret */ msg->next,
6291 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006292 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006293 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006294 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6295 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006296 if (msg->next)
6297 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006298
Christopher Fauletda02e172015-12-04 09:25:05 +01006299 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6300 /* default_ret */ 1,
6301 /* on_error */ goto error,
6302 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006303 if (msg->msg_state == HTTP_MSG_ENDING)
6304 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006305 return 1;
6306
6307 missing_data_or_waiting:
6308 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006309 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6310 /* default_ret */ msg->next,
6311 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006312 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006313 msg->next -= ret;
6314 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6315 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006316 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006317 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006318 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006319 return 0;
6320 error:
6321 return -1;
6322}
6323
6324static inline int
6325http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6326{
6327 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006328 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006329 int ret;
6330
6331 /* Here we have the guarantee to be in one of the following state:
6332 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6333 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6334
6335 switch_states:
6336 switch (msg->msg_state) {
6337 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006338 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6339 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6340 /* on_error */ goto error);
6341 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006342 msg->chunk_len -= ret;
6343 if (msg->chunk_len) {
6344 /* input empty or output full */
6345 if (chn->buf->i > msg->next)
6346 chn->flags |= CF_WAKE_WRITE;
6347 goto missing_data_or_waiting;
6348 }
6349
6350 /* nothing left to forward for this chunk*/
6351 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6352 /* fall through for HTTP_MSG_CHUNK_CRLF */
6353
6354 case HTTP_MSG_CHUNK_CRLF:
6355 /* we want the CRLF after the data */
Willy Tarreaud760eec2018-07-10 09:50:25 +02006356 ret = h1_skip_chunk_crlf(chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006357 if (ret == 0)
6358 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006359 if (ret < 0) {
6360 msg->err_pos = chn->buf->i + ret;
6361 if (msg->err_pos < 0)
6362 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006363 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006364 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006365 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006366 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6367 /* fall through for HTTP_MSG_CHUNK_SIZE */
6368
6369 case HTTP_MSG_CHUNK_SIZE:
6370 /* read the chunk size and assign it to ->chunk_len,
6371 * then set ->next to point to the body and switch to
6372 * DATA or TRAILERS state.
6373 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02006374 ret = h1_parse_chunk_size(chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006375 if (ret == 0)
6376 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006377 if (ret < 0) {
6378 msg->err_pos = chn->buf->i + ret;
6379 if (msg->err_pos < 0)
6380 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006381 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006382 }
6383
6384 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006385 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006386 msg->chunk_len = chunk;
6387 msg->body_len += chunk;
6388
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006389 if (msg->chunk_len) {
6390 msg->msg_state = HTTP_MSG_DATA;
6391 goto switch_states;
6392 }
6393 msg->msg_state = HTTP_MSG_TRAILERS;
6394 /* fall through for HTTP_MSG_TRAILERS */
6395
6396 case HTTP_MSG_TRAILERS:
6397 ret = http_forward_trailers(msg);
6398 if (ret < 0)
6399 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006400 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6401 /* default_ret */ 1,
6402 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006403 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006404 if (!ret)
6405 goto missing_data_or_waiting;
6406 break;
6407
6408 case HTTP_MSG_ENDING:
6409 goto ending;
6410
6411 default:
6412 /* This should no happen in this function */
6413 goto error;
6414 }
6415
6416 msg->msg_state = HTTP_MSG_ENDING;
6417 ending:
6418 /* we may have some pending data starting at res->buf->p such as a last
6419 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006420 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006421 /* default_ret */ msg->next,
6422 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006423 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006424 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006425 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6426 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006427 if (msg->next)
6428 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006429
Christopher Fauletda02e172015-12-04 09:25:05 +01006430 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006431 /* default_ret */ 1,
6432 /* on_error */ goto error,
6433 /* on_wait */ goto waiting);
6434 msg->msg_state = HTTP_MSG_DONE;
6435 return 1;
6436
6437 missing_data_or_waiting:
6438 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006439 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006440 /* default_ret */ msg->next,
6441 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006442 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006443 msg->next -= ret;
6444 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6445 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006446 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006447 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006448 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006449 return 0;
6450
6451 chunk_parsing_error:
6452 if (msg->err_pos >= 0) {
6453 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006454 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006455 msg->msg_state, strm_fe(s));
6456 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006457 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006458 msg, msg->msg_state, s->be);
6459 }
6460 error:
6461 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006462}
6463
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465/* Iterate the same filter through all request headers.
6466 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006467 * Since it can manage the switch to another backend, it updates the per-proxy
6468 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006469 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006470int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006471{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 char *cur_ptr, *cur_end, *cur_next;
6473 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006474 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006476 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 last_hdr = 0;
6479
Willy Tarreau9b28e032012-10-12 23:49:43 +02006480 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006481 old_idx = 0;
6482
6483 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006484 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006486 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006487 (exp->action == ACT_ALLOW ||
6488 exp->action == ACT_DENY ||
6489 exp->action == ACT_TARPIT))
6490 return 0;
6491
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006492 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493 if (!cur_idx)
6494 break;
6495
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006496 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 cur_ptr = cur_next;
6498 cur_end = cur_ptr + cur_hdr->len;
6499 cur_next = cur_end + cur_hdr->cr + 1;
6500
6501 /* Now we have one header between cur_ptr and cur_end,
6502 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503 */
6504
Willy Tarreau15a53a42015-01-21 13:39:42 +01006505 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006508 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006509 last_hdr = 1;
6510 break;
6511
6512 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006513 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006515 break;
6516
6517 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006518 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 break;
6521
6522 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006523 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6524 if (trash.len < 0)
6525 return -1;
6526
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006527 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528 /* FIXME: if the user adds a newline in the replacement, the
6529 * index will not be recalculated for now, and the new line
6530 * will not be counted as a new header.
6531 */
6532
6533 cur_end += delta;
6534 cur_next += delta;
6535 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006536 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006537 break;
6538
6539 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006540 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 cur_next += delta;
6542
Willy Tarreaufa355d42009-11-29 18:12:29 +01006543 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006544 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6545 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546 cur_hdr->len = 0;
6547 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006548 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006549 break;
6550
6551 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552 }
6553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006554 /* keep the link from this header to next one in case of later
6555 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006556 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006557 old_idx = cur_idx;
6558 }
6559 return 0;
6560}
6561
6562
6563/* Apply the filter to the request line.
6564 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6565 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 * Since it can manage the switch to another backend, it updates the per-proxy
6567 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006569int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006570{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 char *cur_ptr, *cur_end;
6572 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006573 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006574 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575
Willy Tarreau3d300592007-03-18 18:34:41 +01006576 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006577 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006578 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 (exp->action == ACT_ALLOW ||
6580 exp->action == ACT_DENY ||
6581 exp->action == ACT_TARPIT))
6582 return 0;
6583 else if (exp->action == ACT_REMOVE)
6584 return 0;
6585
6586 done = 0;
6587
Willy Tarreau9b28e032012-10-12 23:49:43 +02006588 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006589 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006590
6591 /* Now we have the request line between cur_ptr and cur_end */
6592
Willy Tarreau15a53a42015-01-21 13:39:42 +01006593 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006594 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006596 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 done = 1;
6598 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006600 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006601 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006602 done = 1;
6603 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006605 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006606 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006607 done = 1;
6608 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006610 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006611 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6612 if (trash.len < 0)
6613 return -1;
6614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006615 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006616 /* FIXME: if the user adds a newline in the replacement, the
6617 * index will not be recalculated for now, and the new line
6618 * will not be counted as a new header.
6619 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006620
Willy Tarreaufa355d42009-11-29 18:12:29 +01006621 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006622 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006623 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006624 HTTP_MSG_RQMETH,
6625 cur_ptr, cur_end + 1,
6626 NULL, NULL);
6627 if (unlikely(!cur_end))
6628 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006630 /* we have a full request and we know that we have either a CR
6631 * or an LF at <ptr>.
6632 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006633 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6634 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006635 /* there is no point trying this regex on headers */
6636 return 1;
6637 }
6638 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006639 return done;
6640}
Willy Tarreau97de6242006-12-27 17:18:38 +01006641
Willy Tarreau58f10d72006-12-04 02:26:12 +01006642
Willy Tarreau58f10d72006-12-04 02:26:12 +01006643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006644/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006645 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006647 * unparsable request. Since it can manage the switch to another backend, it
6648 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006649 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006650int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006651{
Willy Tarreau192252e2015-04-04 01:47:55 +02006652 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006653 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006654 struct hdr_exp *exp;
6655
6656 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006657 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 /*
6660 * The interleaving of transformations and verdicts
6661 * makes it difficult to decide to continue or stop
6662 * the evaluation.
6663 */
6664
Willy Tarreau6c123b12010-01-28 20:22:06 +01006665 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6666 break;
6667
Willy Tarreau3d300592007-03-18 18:34:41 +01006668 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006669 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006670 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006671 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006672
6673 /* if this filter had a condition, evaluate it now and skip to
6674 * next filter if the condition does not match.
6675 */
6676 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006677 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006678 ret = acl_pass(ret);
6679 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6680 ret = !ret;
6681
6682 if (!ret)
6683 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006684 }
6685
6686 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006687 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006688 if (unlikely(ret < 0))
6689 return -1;
6690
6691 if (likely(ret == 0)) {
6692 /* The filter did not match the request, it can be
6693 * iterated through all headers.
6694 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006695 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6696 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006698 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006699 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006700}
6701
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006702
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006703/* Find the end of a cookie value contained between <s> and <e>. It works the
6704 * same way as with headers above except that the semi-colon also ends a token.
6705 * See RFC2965 for more information. Note that it requires a valid header to
6706 * return a valid result.
6707 */
6708char *find_cookie_value_end(char *s, const char *e)
6709{
6710 int quoted, qdpair;
6711
6712 quoted = qdpair = 0;
6713 for (; s < e; s++) {
6714 if (qdpair) qdpair = 0;
6715 else if (quoted) {
6716 if (*s == '\\') qdpair = 1;
6717 else if (*s == '"') quoted = 0;
6718 }
6719 else if (*s == '"') quoted = 1;
6720 else if (*s == ',' || *s == ';') return s;
6721 }
6722 return s;
6723}
6724
6725/* Delete a value in a header between delimiters <from> and <next> in buffer
6726 * <buf>. The number of characters displaced is returned, and the pointer to
6727 * the first delimiter is updated if required. The function tries as much as
6728 * possible to respect the following principles :
6729 * - replace <from> delimiter by the <next> one unless <from> points to a
6730 * colon, in which case <next> is simply removed
6731 * - set exactly one space character after the new first delimiter, unless
6732 * there are not enough characters in the block being moved to do so.
6733 * - remove unneeded spaces before the previous delimiter and after the new
6734 * one.
6735 *
6736 * It is the caller's responsibility to ensure that :
6737 * - <from> points to a valid delimiter or the colon ;
6738 * - <next> points to a valid delimiter or the final CR/LF ;
6739 * - there are non-space chars before <from> ;
6740 * - there is a CR/LF at or after <next>.
6741 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006742int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743{
6744 char *prev = *from;
6745
6746 if (*prev == ':') {
6747 /* We're removing the first value, preserve the colon and add a
6748 * space if possible.
6749 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006750 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006751 next++;
6752 prev++;
6753 if (prev < next)
6754 *prev++ = ' ';
6755
Willy Tarreau2235b262016-11-05 15:50:20 +01006756 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757 next++;
6758 } else {
6759 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006760 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761 prev--;
6762 *from = prev;
6763
6764 /* copy the delimiter and if possible a space if we're
6765 * not at the end of the line.
6766 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006767 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 *prev++ = *next++;
6769 if (prev + 1 < next)
6770 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006771 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006772 next++;
6773 }
6774 }
6775 return buffer_replace2(buf, prev, next, NULL, 0);
6776}
6777
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006778/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006779 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 * desirable to call it only when needed. This code is quite complex because
6781 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6782 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006783 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006784void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006785{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006786 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006787 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006788 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006789 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006790 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6791 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006792
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006794 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006795 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006796
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006797 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006798 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006799 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006800
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006801 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006802 hdr_beg = hdr_next;
6803 hdr_end = hdr_beg + cur_hdr->len;
6804 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006805
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006806 /* We have one full header between hdr_beg and hdr_end, and the
6807 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006808 * "Cookie:" headers.
6809 */
6810
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006811 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006812 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006813 old_idx = cur_idx;
6814 continue;
6815 }
6816
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006817 del_from = NULL; /* nothing to be deleted */
6818 preserve_hdr = 0; /* assume we may kill the whole header */
6819
Willy Tarreau58f10d72006-12-04 02:26:12 +01006820 /* Now look for cookies. Conforming to RFC2109, we have to support
6821 * attributes whose name begin with a '$', and associate them with
6822 * the right cookie, if we want to delete this cookie.
6823 * So there are 3 cases for each cookie read :
6824 * 1) it's a special attribute, beginning with a '$' : ignore it.
6825 * 2) it's a server id cookie that we *MAY* want to delete : save
6826 * some pointers on it (last semi-colon, beginning of cookie...)
6827 * 3) it's an application cookie : we *MAY* have to delete a previous
6828 * "special" cookie.
6829 * At the end of loop, if a "special" cookie remains, we may have to
6830 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006831 * *MUST* delete it.
6832 *
6833 * Note: RFC2965 is unclear about the processing of spaces around
6834 * the equal sign in the ATTR=VALUE form. A careful inspection of
6835 * the RFC explicitly allows spaces before it, and not within the
6836 * tokens (attrs or values). An inspection of RFC2109 allows that
6837 * too but section 10.1.3 lets one think that spaces may be allowed
6838 * after the equal sign too, resulting in some (rare) buggy
6839 * implementations trying to do that. So let's do what servers do.
6840 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6841 * allowed quoted strings in values, with any possible character
6842 * after a backslash, including control chars and delimitors, which
6843 * causes parsing to become ambiguous. Browsers also allow spaces
6844 * within values even without quotes.
6845 *
6846 * We have to keep multiple pointers in order to support cookie
6847 * removal at the beginning, middle or end of header without
6848 * corrupting the header. All of these headers are valid :
6849 *
6850 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6851 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6852 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6853 * | | | | | | | | |
6854 * | | | | | | | | hdr_end <--+
6855 * | | | | | | | +--> next
6856 * | | | | | | +----> val_end
6857 * | | | | | +-----------> val_beg
6858 * | | | | +--------------> equal
6859 * | | | +----------------> att_end
6860 * | | +---------------------> att_beg
6861 * | +--------------------------> prev
6862 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006863 */
6864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6866 /* Iterate through all cookies on this line */
6867
6868 /* find att_beg */
6869 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006870 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 att_beg++;
6872
6873 /* find att_end : this is the first character after the last non
6874 * space before the equal. It may be equal to hdr_end.
6875 */
6876 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 while (equal < hdr_end) {
6879 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006881 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006882 continue;
6883 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006884 }
6885
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006886 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6887 * is between <att_beg> and <equal>, both may be identical.
6888 */
6889
6890 /* look for end of cookie if there is an equal sign */
6891 if (equal < hdr_end && *equal == '=') {
6892 /* look for the beginning of the value */
6893 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006894 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 val_beg++;
6896
6897 /* find the end of the value, respecting quotes */
6898 next = find_cookie_value_end(val_beg, hdr_end);
6899
6900 /* make val_end point to the first white space or delimitor after the value */
6901 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006902 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 val_end--;
6904 } else {
6905 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006906 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006907
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006908 /* We have nothing to do with attributes beginning with '$'. However,
6909 * they will automatically be removed if a header before them is removed,
6910 * since they're supposed to be linked together.
6911 */
6912 if (*att_beg == '$')
6913 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 /* Ignore cookies with no equal sign */
6916 if (equal == next) {
6917 /* This is not our cookie, so we must preserve it. But if we already
6918 * scheduled another cookie for removal, we cannot remove the
6919 * complete header, but we can remove the previous block itself.
6920 */
6921 preserve_hdr = 1;
6922 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006923 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 val_end += delta;
6925 next += delta;
6926 hdr_end += delta;
6927 hdr_next += delta;
6928 cur_hdr->len += delta;
6929 http_msg_move_end(&txn->req, delta);
6930 prev = del_from;
6931 del_from = NULL;
6932 }
6933 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006934 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006935
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006936 /* if there are spaces around the equal sign, we need to
6937 * strip them otherwise we'll get trouble for cookie captures,
6938 * or even for rewrites. Since this happens extremely rarely,
6939 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6942 int stripped_before = 0;
6943 int stripped_after = 0;
6944
6945 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006946 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006947 equal += stripped_before;
6948 val_beg += stripped_before;
6949 }
6950
6951 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006952 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 val_beg += stripped_after;
6954 stripped_before += stripped_after;
6955 }
6956
6957 val_end += stripped_before;
6958 next += stripped_before;
6959 hdr_end += stripped_before;
6960 hdr_next += stripped_before;
6961 cur_hdr->len += stripped_before;
6962 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006963 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006964 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006966 /* First, let's see if we want to capture this cookie. We check
6967 * that we don't already have a client side cookie, because we
6968 * can only capture one. Also as an optimisation, we ignore
6969 * cookies shorter than the declared name.
6970 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006971 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6972 (val_end - att_beg >= sess->fe->capture_namelen) &&
6973 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006974 int log_len = val_end - att_beg;
6975
Willy Tarreaubafbe012017-11-24 17:34:44 +01006976 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006977 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006978 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006979 if (log_len > sess->fe->capture_len)
6980 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 memcpy(txn->cli_cookie, att_beg, log_len);
6982 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006983 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006984 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985
Willy Tarreaubca99692010-10-06 19:25:55 +02006986 /* Persistence cookies in passive, rewrite or insert mode have the
6987 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006988 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006989 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006990 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006991 * For cookies in prefix mode, the form is :
6992 *
6993 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006995 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6996 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6997 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006998 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007000 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7001 * have the server ID between val_beg and delim, and the original cookie between
7002 * delim+1 and val_end. Otherwise, delim==val_end :
7003 *
7004 * Cookie: NAME=SRV; # in all but prefix modes
7005 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7006 * | || || | |+-> next
7007 * | || || | +--> val_end
7008 * | || || +---------> delim
7009 * | || |+------------> val_beg
7010 * | || +-------------> att_end = equal
7011 * | |+-----------------> att_beg
7012 * | +------------------> prev
7013 * +-------------------------> hdr_beg
7014 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007016 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007017 for (delim = val_beg; delim < val_end; delim++)
7018 if (*delim == COOKIE_DELIM)
7019 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007020 } else {
7021 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007022 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007023 /* Now check if the cookie contains a date field, which would
7024 * appear after a vertical bar ('|') just after the server name
7025 * and before the delimiter.
7026 */
7027 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7028 if (vbar1) {
7029 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007030 * right is the last seen date. It is a base64 encoded
7031 * 30-bit value representing the UNIX date since the
7032 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007033 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007034 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007035 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007036 if (val_end - vbar1 >= 5) {
7037 val = b64tos30(vbar1);
7038 if (val > 0)
7039 txn->cookie_last_date = val << 2;
7040 }
7041 /* look for a second vertical bar */
7042 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7043 if (vbar1 && (val_end - vbar1 > 5)) {
7044 val = b64tos30(vbar1 + 1);
7045 if (val > 0)
7046 txn->cookie_first_date = val << 2;
7047 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007048 }
7049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007050
Willy Tarreauf64d1412010-10-07 20:06:11 +02007051 /* if the cookie has an expiration date and the proxy wants to check
7052 * it, then we do that now. We first check if the cookie is too old,
7053 * then only if it has expired. We detect strict overflow because the
7054 * time resolution here is not great (4 seconds). Cookies with dates
7055 * in the future are ignored if their offset is beyond one day. This
7056 * allows an admin to fix timezone issues without expiring everyone
7057 * and at the same time avoids keeping unwanted side effects for too
7058 * long.
7059 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007060 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7061 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007062 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007063 txn->flags &= ~TX_CK_MASK;
7064 txn->flags |= TX_CK_OLD;
7065 delim = val_beg; // let's pretend we have not found the cookie
7066 txn->cookie_first_date = 0;
7067 txn->cookie_last_date = 0;
7068 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007069 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7070 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007071 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007072 txn->flags &= ~TX_CK_MASK;
7073 txn->flags |= TX_CK_EXPIRED;
7074 delim = val_beg; // let's pretend we have not found the cookie
7075 txn->cookie_first_date = 0;
7076 txn->cookie_last_date = 0;
7077 }
7078
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007079 /* Here, we'll look for the first running server which supports the cookie.
7080 * This allows to share a same cookie between several servers, for example
7081 * to dedicate backup servers to specific servers only.
7082 * However, to prevent clients from sticking to cookie-less backup server
7083 * when they have incidentely learned an empty cookie, we simply ignore
7084 * empty cookies and mark them as invalid.
7085 * The same behaviour is applied when persistence must be ignored.
7086 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007087 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007089
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007090 while (srv) {
7091 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7092 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007093 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007094 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007095 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007096 /* we found the server and we can use it */
7097 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007098 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007099 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007100 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 break;
7102 } else {
7103 /* we found a server, but it's down,
7104 * mark it as such and go on in case
7105 * another one is available.
7106 */
7107 txn->flags &= ~TX_CK_MASK;
7108 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007111 srv = srv->next;
7112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113
Willy Tarreauf64d1412010-10-07 20:06:11 +02007114 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007115 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007117 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007118 txn->flags |= TX_CK_UNUSED;
7119 else
7120 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007123 /* depending on the cookie mode, we may have to either :
7124 * - delete the complete cookie if we're in insert+indirect mode, so that
7125 * the server never sees it ;
7126 * - remove the server id from the cookie value, and tag the cookie as an
7127 * application cookie so that it does not get accidentely removed later,
7128 * if we're in cookie prefix mode
7129 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007130 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007131 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132
Willy Tarreau9b28e032012-10-12 23:49:43 +02007133 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007134 val_end += delta;
7135 next += delta;
7136 hdr_end += delta;
7137 hdr_next += delta;
7138 cur_hdr->len += delta;
7139 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007140
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007141 del_from = NULL;
7142 preserve_hdr = 1; /* we want to keep this cookie */
7143 }
7144 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007145 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 del_from = prev;
7147 }
7148 } else {
7149 /* This is not our cookie, so we must preserve it. But if we already
7150 * scheduled another cookie for removal, we cannot remove the
7151 * complete header, but we can remove the previous block itself.
7152 */
7153 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007155 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007156 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007157 if (att_beg >= del_from)
7158 att_beg += delta;
7159 if (att_end >= del_from)
7160 att_end += delta;
7161 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007162 val_end += delta;
7163 next += delta;
7164 hdr_end += delta;
7165 hdr_next += delta;
7166 cur_hdr->len += delta;
7167 http_msg_move_end(&txn->req, delta);
7168 prev = del_from;
7169 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007171 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007173 /* continue with next cookie on this header line */
7174 att_beg = next;
7175 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007177 /* There are no more cookies on this line.
7178 * We may still have one (or several) marked for deletion at the
7179 * end of the line. We must do this now in two ways :
7180 * - if some cookies must be preserved, we only delete from the
7181 * mark to the end of line ;
7182 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007183 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007184 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007185 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007186 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007187 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007188 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007189 cur_hdr->len += delta;
7190 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007191 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007192
7193 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007194 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7195 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007196 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007197 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007199 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007200 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007201 }
7202
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007203 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007204 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007206}
7207
7208
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209/* Iterate the same filter through all response headers contained in <rtr>.
7210 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7211 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007212int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 char *cur_ptr, *cur_end, *cur_next;
7215 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007216 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007218 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219
7220 last_hdr = 0;
7221
Willy Tarreau9b28e032012-10-12 23:49:43 +02007222 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 old_idx = 0;
7224
7225 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007226 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007228 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 (exp->action == ACT_ALLOW ||
7230 exp->action == ACT_DENY))
7231 return 0;
7232
7233 cur_idx = txn->hdr_idx.v[old_idx].next;
7234 if (!cur_idx)
7235 break;
7236
7237 cur_hdr = &txn->hdr_idx.v[cur_idx];
7238 cur_ptr = cur_next;
7239 cur_end = cur_ptr + cur_hdr->len;
7240 cur_next = cur_end + cur_hdr->cr + 1;
7241
7242 /* Now we have one header between cur_ptr and cur_end,
7243 * and the next header starts at cur_next.
7244 */
7245
Willy Tarreau15a53a42015-01-21 13:39:42 +01007246 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 switch (exp->action) {
7248 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007249 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250 last_hdr = 1;
7251 break;
7252
7253 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007254 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 last_hdr = 1;
7256 break;
7257
7258 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007259 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7260 if (trash.len < 0)
7261 return -1;
7262
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007263 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264 /* FIXME: if the user adds a newline in the replacement, the
7265 * index will not be recalculated for now, and the new line
7266 * will not be counted as a new header.
7267 */
7268
7269 cur_end += delta;
7270 cur_next += delta;
7271 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007272 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 break;
7274
7275 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007276 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007277 cur_next += delta;
7278
Willy Tarreaufa355d42009-11-29 18:12:29 +01007279 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7281 txn->hdr_idx.used--;
7282 cur_hdr->len = 0;
7283 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007284 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 break;
7286
7287 }
7288 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289
7290 /* keep the link from this header to next one in case of later
7291 * removal of next header.
7292 */
7293 old_idx = cur_idx;
7294 }
7295 return 0;
7296}
7297
7298
7299/* Apply the filter to the status line in the response buffer <rtr>.
7300 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7301 * or -1 if a replacement resulted in an invalid status line.
7302 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007303int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305 char *cur_ptr, *cur_end;
7306 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007307 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007308 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309
7310
Willy Tarreau3d300592007-03-18 18:34:41 +01007311 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007313 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 (exp->action == ACT_ALLOW ||
7315 exp->action == ACT_DENY))
7316 return 0;
7317 else if (exp->action == ACT_REMOVE)
7318 return 0;
7319
7320 done = 0;
7321
Willy Tarreau9b28e032012-10-12 23:49:43 +02007322 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007323 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324
7325 /* Now we have the status line between cur_ptr and cur_end */
7326
Willy Tarreau15a53a42015-01-21 13:39:42 +01007327 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007328 switch (exp->action) {
7329 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007330 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331 done = 1;
7332 break;
7333
7334 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007335 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 done = 1;
7337 break;
7338
7339 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007340 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7341 if (trash.len < 0)
7342 return -1;
7343
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007344 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345 /* FIXME: if the user adds a newline in the replacement, the
7346 * index will not be recalculated for now, and the new line
7347 * will not be counted as a new header.
7348 */
7349
Willy Tarreaufa355d42009-11-29 18:12:29 +01007350 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007351 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007352 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007353 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354 cur_ptr, cur_end + 1,
7355 NULL, NULL);
7356 if (unlikely(!cur_end))
7357 return -1;
7358
7359 /* we have a full respnse and we know that we have either a CR
7360 * or an LF at <ptr>.
7361 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007362 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007363 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007364 /* there is no point trying this regex on headers */
7365 return 1;
7366 }
7367 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 return done;
7369}
7370
7371
7372
7373/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007374 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7376 * unparsable response.
7377 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007378int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007379{
Willy Tarreau192252e2015-04-04 01:47:55 +02007380 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007381 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007382 struct hdr_exp *exp;
7383
7384 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385 int ret;
7386
7387 /*
7388 * The interleaving of transformations and verdicts
7389 * makes it difficult to decide to continue or stop
7390 * the evaluation.
7391 */
7392
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007393 if (txn->flags & TX_SVDENY)
7394 break;
7395
Willy Tarreau3d300592007-03-18 18:34:41 +01007396 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007397 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7398 exp->action == ACT_PASS)) {
7399 exp = exp->next;
7400 continue;
7401 }
7402
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007403 /* if this filter had a condition, evaluate it now and skip to
7404 * next filter if the condition does not match.
7405 */
7406 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007407 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007408 ret = acl_pass(ret);
7409 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7410 ret = !ret;
7411 if (!ret)
7412 continue;
7413 }
7414
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007416 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417 if (unlikely(ret < 0))
7418 return -1;
7419
7420 if (likely(ret == 0)) {
7421 /* The filter did not match the response, it can be
7422 * iterated through all headers.
7423 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007424 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7425 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 }
7428 return 0;
7429}
7430
7431
Willy Tarreaua15645d2007-03-18 16:22:39 +01007432/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007433 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007434 * desirable to call it only when needed. This function is also used when we
7435 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007436 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007437void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007438{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007439 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007440 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007441 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007442 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007444 char *hdr_beg, *hdr_end, *hdr_next;
7445 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007446
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 /* Iterate through the headers.
7448 * we start with the start line.
7449 */
7450 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007451 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452
7453 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7454 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007455 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456
7457 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007458 hdr_beg = hdr_next;
7459 hdr_end = hdr_beg + cur_hdr->len;
7460 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007461
Willy Tarreau24581ba2010-08-31 22:39:35 +02007462 /* We have one full header between hdr_beg and hdr_end, and the
7463 * next header starts at hdr_next. We're only interested in
7464 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465 */
7466
Willy Tarreau24581ba2010-08-31 22:39:35 +02007467 is_cookie2 = 0;
7468 prev = hdr_beg + 10;
7469 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007470 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007471 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7472 if (!val) {
7473 old_idx = cur_idx;
7474 continue;
7475 }
7476 is_cookie2 = 1;
7477 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007478 }
7479
Willy Tarreau24581ba2010-08-31 22:39:35 +02007480 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7481 * <prev> points to the colon.
7482 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007483 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007484
Willy Tarreau24581ba2010-08-31 22:39:35 +02007485 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7486 * check-cache is enabled) and we are not interested in checking
7487 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007488 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007489 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490 return;
7491
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 /* OK so now we know we have to process this response cookie.
7493 * The format of the Set-Cookie header is slightly different
7494 * from the format of the Cookie header in that it does not
7495 * support the comma as a cookie delimiter (thus the header
7496 * cannot be folded) because the Expires attribute described in
7497 * the original Netscape's spec may contain an unquoted date
7498 * with a comma inside. We have to live with this because
7499 * many browsers don't support Max-Age and some browsers don't
7500 * support quoted strings. However the Set-Cookie2 header is
7501 * clean.
7502 *
7503 * We have to keep multiple pointers in order to support cookie
7504 * removal at the beginning, middle or end of header without
7505 * corrupting the header (in case of set-cookie2). A special
7506 * pointer, <scav> points to the beginning of the set-cookie-av
7507 * fields after the first semi-colon. The <next> pointer points
7508 * either to the end of line (set-cookie) or next unquoted comma
7509 * (set-cookie2). All of these headers are valid :
7510 *
7511 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7512 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7513 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7514 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7515 * | | | | | | | | | |
7516 * | | | | | | | | +-> next hdr_end <--+
7517 * | | | | | | | +------------> scav
7518 * | | | | | | +--------------> val_end
7519 * | | | | | +--------------------> val_beg
7520 * | | | | +----------------------> equal
7521 * | | | +------------------------> att_end
7522 * | | +----------------------------> att_beg
7523 * | +------------------------------> prev
7524 * +-----------------------------------------> hdr_beg
7525 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007526
Willy Tarreau24581ba2010-08-31 22:39:35 +02007527 for (; prev < hdr_end; prev = next) {
7528 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007529
Willy Tarreau24581ba2010-08-31 22:39:35 +02007530 /* find att_beg */
7531 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007532 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007533 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007534
Willy Tarreau24581ba2010-08-31 22:39:35 +02007535 /* find att_end : this is the first character after the last non
7536 * space before the equal. It may be equal to hdr_end.
7537 */
7538 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007539
Willy Tarreau24581ba2010-08-31 22:39:35 +02007540 while (equal < hdr_end) {
7541 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7542 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007543 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007544 continue;
7545 att_end = equal;
7546 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547
Willy Tarreau24581ba2010-08-31 22:39:35 +02007548 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7549 * is between <att_beg> and <equal>, both may be identical.
7550 */
7551
7552 /* look for end of cookie if there is an equal sign */
7553 if (equal < hdr_end && *equal == '=') {
7554 /* look for the beginning of the value */
7555 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007556 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007558
Willy Tarreau24581ba2010-08-31 22:39:35 +02007559 /* find the end of the value, respecting quotes */
7560 next = find_cookie_value_end(val_beg, hdr_end);
7561
7562 /* make val_end point to the first white space or delimitor after the value */
7563 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007564 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007565 val_end--;
7566 } else {
7567 /* <equal> points to next comma, semi-colon or EOL */
7568 val_beg = val_end = next = equal;
7569 }
7570
7571 if (next < hdr_end) {
7572 /* Set-Cookie2 supports multiple cookies, and <next> points to
7573 * a colon or semi-colon before the end. So skip all attr-value
7574 * pairs and look for the next comma. For Set-Cookie, since
7575 * commas are permitted in values, skip to the end.
7576 */
7577 if (is_cookie2)
7578 next = find_hdr_value_end(next, hdr_end);
7579 else
7580 next = hdr_end;
7581 }
7582
7583 /* Now everything is as on the diagram above */
7584
7585 /* Ignore cookies with no equal sign */
7586 if (equal == val_end)
7587 continue;
7588
7589 /* If there are spaces around the equal sign, we need to
7590 * strip them otherwise we'll get trouble for cookie captures,
7591 * or even for rewrites. Since this happens extremely rarely,
7592 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007593 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007594 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7595 int stripped_before = 0;
7596 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007597
Willy Tarreau24581ba2010-08-31 22:39:35 +02007598 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007599 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007600 equal += stripped_before;
7601 val_beg += stripped_before;
7602 }
7603
7604 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007605 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007606 val_beg += stripped_after;
7607 stripped_before += stripped_after;
7608 }
7609
7610 val_end += stripped_before;
7611 next += stripped_before;
7612 hdr_end += stripped_before;
7613 hdr_next += stripped_before;
7614 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007615 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007616 }
7617
7618 /* First, let's see if we want to capture this cookie. We check
7619 * that we don't already have a server side cookie, because we
7620 * can only capture one. Also as an optimisation, we ignore
7621 * cookies shorter than the declared name.
7622 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007623 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007624 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007625 (val_end - att_beg >= sess->fe->capture_namelen) &&
7626 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007627 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007628 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007629 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007630 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007631 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007632 if (log_len > sess->fe->capture_len)
7633 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007634 memcpy(txn->srv_cookie, att_beg, log_len);
7635 txn->srv_cookie[log_len] = 0;
7636 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007637 }
7638
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007639 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007641 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007642 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7643 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007644 /* assume passive cookie by default */
7645 txn->flags &= ~TX_SCK_MASK;
7646 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647
7648 /* If the cookie is in insert mode on a known server, we'll delete
7649 * this occurrence because we'll insert another one later.
7650 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007651 * a direct access.
7652 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007653 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007654 /* The "preserve" flag was set, we don't want to touch the
7655 * server's cookie.
7656 */
7657 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007658 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007659 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007660 /* this cookie must be deleted */
7661 if (*prev == ':' && next == hdr_end) {
7662 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007663 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007664 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7665 txn->hdr_idx.used--;
7666 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007667 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007668 hdr_next += delta;
7669 http_msg_move_end(&txn->rsp, delta);
7670 /* note: while both invalid now, <next> and <hdr_end>
7671 * are still equal, so the for() will stop as expected.
7672 */
7673 } else {
7674 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007675 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007676 next = prev;
7677 hdr_end += delta;
7678 hdr_next += delta;
7679 cur_hdr->len += delta;
7680 http_msg_move_end(&txn->rsp, delta);
7681 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007682 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007683 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007684 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007686 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007687 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688 * with this server since we know it.
7689 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007690 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007691 next += delta;
7692 hdr_end += delta;
7693 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007695 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007696
Willy Tarreauf1348312010-10-07 15:54:11 +02007697 txn->flags &= ~TX_SCK_MASK;
7698 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007699 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007700 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007702 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007703 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007704 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007705 next += delta;
7706 hdr_end += delta;
7707 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007708 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007709 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007710
Willy Tarreau827aee92011-03-10 16:55:02 +01007711 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007712 txn->flags &= ~TX_SCK_MASK;
7713 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007714 }
7715 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007716 /* that's done for this cookie, check the next one on the same
7717 * line when next != hdr_end (only if is_cookie2).
7718 */
7719 }
7720 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007721 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007722 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723}
7724
7725
Willy Tarreaua15645d2007-03-18 16:22:39 +01007726/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007727 * Parses the Cache-Control and Pragma request header fields to determine if
7728 * the request may be served from the cache and/or if it is cacheable. Updates
7729 * s->txn->flags.
7730 */
7731void check_request_for_cacheability(struct stream *s, struct channel *chn)
7732{
7733 struct http_txn *txn = s->txn;
7734 char *p1, *p2;
7735 char *cur_ptr, *cur_end, *cur_next;
7736 int pragma_found;
7737 int cc_found;
7738 int cur_idx;
7739
7740 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7741 return; /* nothing more to do here */
7742
7743 cur_idx = 0;
7744 pragma_found = cc_found = 0;
7745 cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
7746
7747 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7748 struct hdr_idx_elem *cur_hdr;
7749 int val;
7750
7751 cur_hdr = &txn->hdr_idx.v[cur_idx];
7752 cur_ptr = cur_next;
7753 cur_end = cur_ptr + cur_hdr->len;
7754 cur_next = cur_end + cur_hdr->cr + 1;
7755
7756 /* We have one full header between cur_ptr and cur_end, and the
7757 * next header starts at cur_next.
7758 */
7759
7760 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7761 if (val) {
7762 if ((cur_end - (cur_ptr + val) >= 8) &&
7763 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7764 pragma_found = 1;
7765 continue;
7766 }
7767 }
7768
William Lallemand8a16fe02018-05-22 11:04:33 +02007769 /* Don't use the cache and don't try to store if we found the
7770 * Authorization header */
7771 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7772 if (val) {
7773 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7774 txn->flags |= TX_CACHE_IGNORE;
7775 continue;
7776 }
7777
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007778 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7779 if (!val)
7780 continue;
7781
7782 /* OK, right now we know we have a cache-control header at cur_ptr */
7783 cc_found = 1;
7784 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7785
7786 if (p1 >= cur_end) /* no more info */
7787 continue;
7788
7789 /* p1 is at the beginning of the value */
7790 p2 = p1;
7791 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7792 p2++;
7793
7794 /* we have a complete value between p1 and p2. We don't check the
7795 * values after max-age, max-stale nor min-fresh, we simply don't
7796 * use the cache when they're specified.
7797 */
7798 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7799 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7800 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7801 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7802 txn->flags |= TX_CACHE_IGNORE;
7803 continue;
7804 }
7805
7806 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7807 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7808 continue;
7809 }
7810 }
7811
7812 /* RFC7234#5.4:
7813 * When the Cache-Control header field is also present and
7814 * understood in a request, Pragma is ignored.
7815 * When the Cache-Control header field is not present in a
7816 * request, caches MUST consider the no-cache request
7817 * pragma-directive as having the same effect as if
7818 * "Cache-Control: no-cache" were present.
7819 */
7820 if (!cc_found && pragma_found)
7821 txn->flags |= TX_CACHE_IGNORE;
7822}
7823
7824/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007825 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007827void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007829 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007830 char *p1, *p2;
7831
7832 char *cur_ptr, *cur_end, *cur_next;
7833 int cur_idx;
7834
Willy Tarreau12b32f22017-12-21 16:08:09 +01007835 if (txn->status < 200) {
7836 /* do not try to cache interim responses! */
7837 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007839 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840
7841 /* Iterate through the headers.
7842 * we start with the start line.
7843 */
7844 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007845 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846
7847 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7848 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007849 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850
7851 cur_hdr = &txn->hdr_idx.v[cur_idx];
7852 cur_ptr = cur_next;
7853 cur_end = cur_ptr + cur_hdr->len;
7854 cur_next = cur_end + cur_hdr->cr + 1;
7855
7856 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007857 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 */
7859
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007860 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7861 if (val) {
7862 if ((cur_end - (cur_ptr + val) >= 8) &&
7863 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7864 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7865 return;
7866 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867 }
7868
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007869 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7870 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871 continue;
7872
7873 /* OK, right now we know we have a cache-control header at cur_ptr */
7874
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007875 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876
7877 if (p1 >= cur_end) /* no more info */
7878 continue;
7879
7880 /* p1 is at the beginning of the value */
7881 p2 = p1;
7882
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007883 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884 p2++;
7885
7886 /* we have a complete value between p1 and p2 */
7887 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007888 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7889 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7890 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7891 continue;
7892 }
7893
Willy Tarreaua15645d2007-03-18 16:22:39 +01007894 /* we have something of the form no-cache="set-cookie" */
7895 if ((cur_end - p1 >= 21) &&
7896 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7897 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007898 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 continue;
7900 }
7901
7902 /* OK, so we know that either p2 points to the end of string or to a comma */
7903 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007904 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007905 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007906 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907 return;
7908 }
7909
7910 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007911 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007912 continue;
7913 }
7914 }
7915}
7916
Willy Tarreau58f10d72006-12-04 02:26:12 +01007917
Willy Tarreaub2513902006-12-17 14:52:38 +01007918/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007919 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007920 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007921 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007922 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007923 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007924 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007925 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007926 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007927int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007928{
7929 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007930 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007931 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007932
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007933 if (!uri_auth)
7934 return 0;
7935
Cyril Bonté70be45d2010-10-12 00:14:35 +02007936 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007937 return 0;
7938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007939 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007940 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007941 return 0;
7942
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007943 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007944 return 0;
7945
Willy Tarreaub2513902006-12-17 14:52:38 +01007946 return 1;
7947}
7948
Willy Tarreau4076a152009-04-02 15:18:36 +02007949/*
7950 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007951 * By default it tries to report the error position as msg->err_pos. However if
7952 * this one is not set, it will then report msg->next, which is the last known
7953 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007954 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007955 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007956void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007957 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007958 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007959{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007960 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007961 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007962 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007963
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007964 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007965 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007966 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007967 len1 = MIN(len1, es->len);
7968 len2 = es->len - len1; /* remaining data if buffer wraps */
7969
Willy Tarreauf3764b72016-03-31 13:45:10 +02007970 if (!es->buf)
7971 es->buf = malloc(global.tune.bufsize);
7972
7973 if (es->buf) {
7974 memcpy(es->buf, chn->buf->p, len1);
7975 if (len2)
7976 memcpy(es->buf + len1, chn->buf->data, len2);
7977 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007978
Willy Tarreau4076a152009-04-02 15:18:36 +02007979 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007980 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007981 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007982 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007983
Willy Tarreau4076a152009-04-02 15:18:36 +02007984 es->when = date; // user-visible date
7985 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007986 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007987 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007988 if (objt_conn(sess->origin))
7989 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007990 else
7991 memset(&es->src, 0, sizeof(es->src));
7992
Willy Tarreau078272e2010-12-12 12:46:33 +01007993 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007994 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007995 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007996 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007997 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007998 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007999 es->b_out = chn->buf->o;
8000 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008001 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008002 es->m_clen = msg->chunk_len;
8003 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01008004 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02008005}
Willy Tarreaub2513902006-12-17 14:52:38 +01008006
Willy Tarreau294c4732011-12-16 21:35:50 +01008007/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8008 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8009 * performed over the whole headers. Otherwise it must contain a valid header
8010 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8011 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8012 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8013 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008014 * -1. The value fetch stops at commas, so this function is suited for use with
8015 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008016 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008017 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008018unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008019 struct hdr_idx *idx, int occ,
8020 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008021{
Willy Tarreau294c4732011-12-16 21:35:50 +01008022 struct hdr_ctx local_ctx;
8023 char *ptr_hist[MAX_HDR_HISTORY];
8024 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008025 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008026 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008027
Willy Tarreau294c4732011-12-16 21:35:50 +01008028 if (!ctx) {
8029 local_ctx.idx = 0;
8030 ctx = &local_ctx;
8031 }
8032
Willy Tarreaubce70882009-09-07 11:51:47 +02008033 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008034 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008035 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008036 occ--;
8037 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008038 *vptr = ctx->line + ctx->val;
8039 *vlen = ctx->vlen;
8040 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008041 }
8042 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008043 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008044 }
8045
8046 /* negative occurrence, we scan all the list then walk back */
8047 if (-occ > MAX_HDR_HISTORY)
8048 return 0;
8049
Willy Tarreau294c4732011-12-16 21:35:50 +01008050 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008051 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008052 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8053 len_hist[hist_ptr] = ctx->vlen;
8054 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008055 hist_ptr = 0;
8056 found++;
8057 }
8058 if (-occ > found)
8059 return 0;
8060 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008061 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8062 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8063 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008064 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008065 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008066 if (hist_ptr >= MAX_HDR_HISTORY)
8067 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008068 *vptr = ptr_hist[hist_ptr];
8069 *vlen = len_hist[hist_ptr];
8070 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008071}
8072
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008073/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8074 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8075 * performed over the whole headers. Otherwise it must contain a valid header
8076 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8077 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8078 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8079 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8080 * -1. This function differs from http_get_hdr() in that it only returns full
8081 * line header values and does not stop at commas.
8082 * The return value is 0 if nothing was found, or non-zero otherwise.
8083 */
8084unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8085 struct hdr_idx *idx, int occ,
8086 struct hdr_ctx *ctx, char **vptr, int *vlen)
8087{
8088 struct hdr_ctx local_ctx;
8089 char *ptr_hist[MAX_HDR_HISTORY];
8090 int len_hist[MAX_HDR_HISTORY];
8091 unsigned int hist_ptr;
8092 int found;
8093
8094 if (!ctx) {
8095 local_ctx.idx = 0;
8096 ctx = &local_ctx;
8097 }
8098
8099 if (occ >= 0) {
8100 /* search from the beginning */
8101 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8102 occ--;
8103 if (occ <= 0) {
8104 *vptr = ctx->line + ctx->val;
8105 *vlen = ctx->vlen;
8106 return 1;
8107 }
8108 }
8109 return 0;
8110 }
8111
8112 /* negative occurrence, we scan all the list then walk back */
8113 if (-occ > MAX_HDR_HISTORY)
8114 return 0;
8115
8116 found = hist_ptr = 0;
8117 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8118 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8119 len_hist[hist_ptr] = ctx->vlen;
8120 if (++hist_ptr >= MAX_HDR_HISTORY)
8121 hist_ptr = 0;
8122 found++;
8123 }
8124 if (-occ > found)
8125 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008126
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008127 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008128 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8129 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8130 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008131 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008132 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008133 if (hist_ptr >= MAX_HDR_HISTORY)
8134 hist_ptr -= MAX_HDR_HISTORY;
8135 *vptr = ptr_hist[hist_ptr];
8136 *vlen = len_hist[hist_ptr];
8137 return 1;
8138}
8139
Willy Tarreaubaaee002006-06-26 02:48:02 +02008140/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008141 * Print a debug line with a header. Always stop at the first CR or LF char,
8142 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8143 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008144 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008145void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008146{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008147 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008148 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008149
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008150 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008151 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008152 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008153 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 +02008154
8155 for (max = 0; start + max < end; max++)
8156 if (start[max] == '\r' || start[max] == '\n')
8157 break;
8158
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008159 UBOUND(max, trash.size - trash.len - 3);
8160 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8161 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008162 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008163}
8164
Willy Tarreaueee5b512015-04-03 23:46:31 +02008165
8166/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8167 * The hdr_idx is allocated as well. In case of allocation failure, everything
8168 * allocated is freed and NULL is returned. Otherwise the new transaction is
8169 * assigned to the stream and returned.
8170 */
8171struct http_txn *http_alloc_txn(struct stream *s)
8172{
8173 struct http_txn *txn = s->txn;
8174
8175 if (txn)
8176 return txn;
8177
Willy Tarreaubafbe012017-11-24 17:34:44 +01008178 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008179 if (!txn)
8180 return txn;
8181
8182 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008183 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008184 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008185 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008186 return NULL;
8187 }
8188
8189 s->txn = txn;
8190 return txn;
8191}
8192
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008193void http_txn_reset_req(struct http_txn *txn)
8194{
8195 txn->req.flags = 0;
8196 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8197 txn->req.next = 0;
8198 txn->req.chunk_len = 0LL;
8199 txn->req.body_len = 0LL;
8200 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8201}
8202
8203void http_txn_reset_res(struct http_txn *txn)
8204{
8205 txn->rsp.flags = 0;
8206 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8207 txn->rsp.next = 0;
8208 txn->rsp.chunk_len = 0LL;
8209 txn->rsp.body_len = 0LL;
8210 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8211}
8212
Willy Tarreau0937bc42009-12-22 15:03:09 +01008213/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008214 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008215 * the required fields are properly allocated and that we only need to (re)init
8216 * them. This should be used before processing any new request.
8217 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008218void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008219{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008220 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008221 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008222
8223 txn->flags = 0;
8224 txn->status = -1;
8225
Willy Tarreauf64d1412010-10-07 20:06:11 +02008226 txn->cookie_first_date = 0;
8227 txn->cookie_last_date = 0;
8228
Willy Tarreaueee5b512015-04-03 23:46:31 +02008229 txn->srv_cookie = NULL;
8230 txn->cli_cookie = NULL;
8231 txn->uri = NULL;
8232
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008233 http_txn_reset_req(txn);
8234 http_txn_reset_res(txn);
8235
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008236 txn->req.chn = &s->req;
8237 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008238
8239 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008240
8241 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8242 if (fe->options2 & PR_O2_REQBUG_OK)
8243 txn->req.err_pos = -1; /* let buggy requests pass */
8244
Willy Tarreau0937bc42009-12-22 15:03:09 +01008245 if (txn->hdr_idx.v)
8246 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008247
8248 vars_init(&s->vars_txn, SCOPE_TXN);
8249 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008250}
8251
8252/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008253void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008254{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008255 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008256 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008257
8258 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008259 pool_free(pool_head_requri, txn->uri);
8260 pool_free(pool_head_capture, txn->cli_cookie);
8261 pool_free(pool_head_capture, txn->srv_cookie);
8262 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008263
William Lallemanda73203e2012-03-12 12:48:57 +01008264 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008265 txn->uri = NULL;
8266 txn->srv_cookie = NULL;
8267 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008268
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008269 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008270 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008271 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008272 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008273 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008274 }
8275
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008276 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008277 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008278 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008279 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008280 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008281 }
8282
Willy Tarreau6204cd92016-03-10 16:33:04 +01008283 vars_prune(&s->vars_txn, s->sess, s);
8284 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008285}
8286
8287/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008288void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008289{
8290 http_end_txn(s);
8291 http_init_txn(s);
8292
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008293 /* reinitialise the current rule list pointer to NULL. We are sure that
8294 * any rulelist match the NULL pointer.
8295 */
8296 s->current_rule_list = NULL;
8297
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008298 s->be = strm_fe(s);
8299 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008300 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008301 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008302 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008303 /* re-init store persistence */
8304 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008305 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008306
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008307 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008308
Willy Tarreau739cfba2010-01-25 23:11:14 +01008309 /* We must trim any excess data from the response buffer, because we
8310 * may have blocked an invalid response from a server that we don't
8311 * want to accidentely forward once we disable the analysers, nor do
8312 * we want those data to come along with next response. A typical
8313 * example of such data would be from a buggy server responding to
8314 * a HEAD with some data, or sending more than the advertised
8315 * content-length.
8316 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008317 if (unlikely(s->res.buf->i))
8318 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008319
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008320 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02008321 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008322
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008323 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008324 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008325
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008326 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008327 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008328
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008329 s->req.rex = TICK_ETERNITY;
8330 s->req.wex = TICK_ETERNITY;
8331 s->req.analyse_exp = TICK_ETERNITY;
8332 s->res.rex = TICK_ETERNITY;
8333 s->res.wex = TICK_ETERNITY;
8334 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008335 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008336}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008337
Sasha Pachev218f0642014-06-16 12:05:59 -06008338void free_http_res_rules(struct list *r)
8339{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008340 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008341
8342 list_for_each_entry_safe(pr, tr, r, list) {
8343 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008344 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008345 free(pr);
8346 }
8347}
8348
8349void free_http_req_rules(struct list *r)
8350{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008351 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008352
8353 list_for_each_entry_safe(pr, tr, r, list) {
8354 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008355 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008356 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008357
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008358 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008359 free(pr);
8360 }
8361}
8362
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008363/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008364struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008365{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008366 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008367 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008368 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008369 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008370
Vincent Bernat02779b62016-04-03 13:48:43 +02008371 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008372 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008373 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008374 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008375 }
8376
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008377 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008378 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008379 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008380 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008381 int code;
8382 int hc;
8383
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008384 if (!strcmp(args[0], "tarpit")) {
8385 rule->action = ACT_HTTP_REQ_TARPIT;
8386 rule->deny_status = HTTP_ERR_500;
8387 }
8388 else {
8389 rule->action = ACT_ACTION_DENY;
8390 rule->deny_status = HTTP_ERR_403;
8391 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008392 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008393 if (strcmp(args[cur_arg], "deny_status") == 0) {
8394 cur_arg++;
8395 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008396 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8397 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008398 goto out_err;
8399 }
8400
8401 code = atol(args[cur_arg]);
8402 cur_arg++;
8403 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8404 if (http_err_codes[hc] == code) {
8405 rule->deny_status = hc;
8406 break;
8407 }
8408 }
8409
8410 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008411 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8412 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008413 }
8414 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008415 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008416 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008417 cur_arg = 1;
8418
8419 while(*args[cur_arg]) {
8420 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008421 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008422 cur_arg+=2;
8423 continue;
8424 } else
8425 break;
8426 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008427 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008428 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008429 cur_arg = 1;
8430
8431 if (!*args[cur_arg] ||
8432 (*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 +01008433 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8434 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008435 goto out_err;
8436 }
8437 rule->arg.nice = atoi(args[cur_arg]);
8438 if (rule->arg.nice < -1024)
8439 rule->arg.nice = -1024;
8440 else if (rule->arg.nice > 1024)
8441 rule->arg.nice = 1024;
8442 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008443 } else if (!strcmp(args[0], "set-tos")) {
8444#ifdef IP_TOS
8445 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008446 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008447 cur_arg = 1;
8448
8449 if (!*args[cur_arg] ||
8450 (*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 +01008451 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8452 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008453 goto out_err;
8454 }
8455
8456 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8457 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008458 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8459 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008460 goto out_err;
8461 }
8462 cur_arg++;
8463#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008464 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 +02008465 goto out_err;
8466#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008467 } else if (!strcmp(args[0], "set-mark")) {
8468#ifdef SO_MARK
8469 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008470 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008471 cur_arg = 1;
8472
8473 if (!*args[cur_arg] ||
8474 (*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 +01008475 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8476 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008477 goto out_err;
8478 }
8479
8480 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8481 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008482 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8483 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008484 goto out_err;
8485 }
8486 cur_arg++;
8487 global.last_checks |= LSTCHK_NETADM;
8488#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008489 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 +02008490 goto out_err;
8491#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008492 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008493 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008494 cur_arg = 1;
8495
8496 if (!*args[cur_arg] ||
8497 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8498 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008499 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8500 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008501 goto out_err;
8502 }
8503 if (strcmp(args[cur_arg], "silent") == 0)
8504 rule->arg.loglevel = -1;
8505 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8506 goto bad_log_level;
8507 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008508 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008509 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008510 cur_arg = 1;
8511
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008512 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8513 (*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 +01008514 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8515 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008516 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008517 }
8518
8519 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8520 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8521 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008522
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008523 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008524 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008525 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 +01008526 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008527 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8528 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008529 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008530 goto out_err;
8531 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008532 free(proxy->conf.lfs_file);
8533 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8534 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008535 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008536 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008537 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008538 cur_arg = 1;
8539
8540 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008541 (*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 +01008542 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8543 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008544 goto out_err;
8545 }
8546
8547 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8548 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8549 LIST_INIT(&rule->arg.hdr_add.fmt);
8550
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008551 error = NULL;
8552 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008553 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8554 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008555 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008556 goto out_err;
8557 }
8558
8559 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008560 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008561 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 +01008562 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008563 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8564 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008565 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008566 goto out_err;
8567 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008568
8569 free(proxy->conf.lfs_file);
8570 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8571 proxy->conf.lfs_line = proxy->conf.args.line;
8572 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008573 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008574 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008575 cur_arg = 1;
8576
8577 if (!*args[cur_arg] ||
8578 (*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 +01008579 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8580 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008581 goto out_err;
8582 }
8583
8584 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8585 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8586
8587 proxy->conf.args.ctx = ARGC_HRQ;
8588 free(proxy->conf.lfs_file);
8589 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8590 proxy->conf.lfs_line = proxy->conf.args.line;
8591 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008592 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008593 struct sample_expr *expr;
8594 unsigned int where;
8595 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008596 unsigned int tsc_num;
8597 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008598
8599 cur_arg = 1;
8600 proxy->conf.args.ctx = ARGC_TRK;
8601
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008602 tsc_num_str = &args[0][8];
8603 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8604 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8605 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8606 free(err);
8607 goto out_err;
8608 }
8609
Willy Tarreau09448f72014-06-25 18:12:15 +02008610 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8611 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008612 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8613 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008614 free(err);
8615 goto out_err;
8616 }
8617
8618 where = 0;
8619 if (proxy->cap & PR_CAP_FE)
8620 where |= SMP_VAL_FE_HRQ_HDR;
8621 if (proxy->cap & PR_CAP_BE)
8622 where |= SMP_VAL_BE_HRQ_HDR;
8623
8624 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008625 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8626 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8627 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8628 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008629 free(expr);
8630 goto out_err;
8631 }
8632
8633 if (strcmp(args[cur_arg], "table") == 0) {
8634 cur_arg++;
8635 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008636 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8637 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008638 free(expr);
8639 goto out_err;
8640 }
8641 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008642 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008643 cur_arg++;
8644 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008645 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008646 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008647 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008648 } else if (strcmp(args[0], "redirect") == 0) {
8649 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008650 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008651
Willy Tarreaube4653b2015-05-28 15:26:58 +02008652 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008653 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8654 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008655 goto out_err;
8656 }
8657
8658 /* this redirect rule might already contain a parsed condition which
8659 * we'll pass to the http-request rule.
8660 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008661 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008662 rule->arg.redir = redir;
8663 rule->cond = redir->cond;
8664 redir->cond = NULL;
8665 cur_arg = 2;
8666 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008667 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8668 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008669 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008670 /*
8671 * '+ 8' for 'add-acl('
8672 * '- 9' for 'add-acl(' + trailing ')'
8673 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008674 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008675
8676 cur_arg = 1;
8677
8678 if (!*args[cur_arg] ||
8679 (*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 +01008680 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8681 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008682 goto out_err;
8683 }
8684
8685 LIST_INIT(&rule->arg.map.key);
8686 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008687 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008688 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008689 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008690 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8691 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008692 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008693 goto out_err;
8694 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008695 free(proxy->conf.lfs_file);
8696 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8697 proxy->conf.lfs_line = proxy->conf.args.line;
8698 cur_arg += 1;
8699 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8700 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008701 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008702 /*
8703 * '+ 8' for 'del-acl('
8704 * '- 9' for 'del-acl(' + trailing ')'
8705 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008706 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008707
8708 cur_arg = 1;
8709
8710 if (!*args[cur_arg] ||
8711 (*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 +01008712 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8713 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008714 goto out_err;
8715 }
8716
8717 LIST_INIT(&rule->arg.map.key);
8718 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008719 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008720 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008721 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008722 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8723 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008724 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008725 goto out_err;
8726 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008727 free(proxy->conf.lfs_file);
8728 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8729 proxy->conf.lfs_line = proxy->conf.args.line;
8730 cur_arg += 1;
8731 } else if (strncmp(args[0], "del-map", 7) == 0) {
8732 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008733 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008734 /*
8735 * '+ 8' for 'del-map('
8736 * '- 9' for 'del-map(' + trailing ')'
8737 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008738 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008739
8740 cur_arg = 1;
8741
8742 if (!*args[cur_arg] ||
8743 (*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 +01008744 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8745 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008746 goto out_err;
8747 }
8748
8749 LIST_INIT(&rule->arg.map.key);
8750 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008751 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008752 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008753 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008754 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8755 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008756 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008757 goto out_err;
8758 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008759 free(proxy->conf.lfs_file);
8760 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8761 proxy->conf.lfs_line = proxy->conf.args.line;
8762 cur_arg += 1;
8763 } else if (strncmp(args[0], "set-map", 7) == 0) {
8764 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008765 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008766 /*
8767 * '+ 8' for 'set-map('
8768 * '- 9' for 'set-map(' + trailing ')'
8769 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008770 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008771
8772 cur_arg = 1;
8773
8774 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8775 (*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 +01008776 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8777 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008778 goto out_err;
8779 }
8780
8781 LIST_INIT(&rule->arg.map.key);
8782 LIST_INIT(&rule->arg.map.value);
8783 proxy->conf.args.ctx = ARGC_HRQ;
8784
8785 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008786 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008787 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008788 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008789 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8790 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008791 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008792 goto out_err;
8793 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008794
8795 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008796 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008797 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 +01008798 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008799 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8800 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008801 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008802 goto out_err;
8803 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008804 free(proxy->conf.lfs_file);
8805 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8806 proxy->conf.lfs_line = proxy->conf.args.line;
8807
8808 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008809 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8810 char *errmsg = NULL;
8811 cur_arg = 1;
8812 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008813 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008814 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008815 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008816 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8817 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008818 free(errmsg);
8819 goto out_err;
8820 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008821 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008822 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008823 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8824 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8825 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8826 "%s%s, but got '%s'%s.\n",
8827 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008828 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008829 }
8830
8831 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8832 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008833 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008834
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008835 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008836 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8837 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008838 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008839 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008840 }
8841 rule->cond = cond;
8842 }
8843 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008844 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8845 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8846 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008847 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008848 }
8849
8850 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008851 out_err:
8852 free(rule);
8853 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008854}
8855
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008856/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008857struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008858{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008859 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008860 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008861 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008862 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008863
8864 rule = calloc(1, sizeof(*rule));
8865 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008866 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008867 goto out_err;
8868 }
8869
8870 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008871 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008872 cur_arg = 1;
8873 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008874 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008875 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008876 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008877 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008878 cur_arg = 1;
8879
8880 if (!*args[cur_arg] ||
8881 (*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 +01008882 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8883 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008884 goto out_err;
8885 }
8886 rule->arg.nice = atoi(args[cur_arg]);
8887 if (rule->arg.nice < -1024)
8888 rule->arg.nice = -1024;
8889 else if (rule->arg.nice > 1024)
8890 rule->arg.nice = 1024;
8891 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008892 } else if (!strcmp(args[0], "set-tos")) {
8893#ifdef IP_TOS
8894 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008895 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008896 cur_arg = 1;
8897
8898 if (!*args[cur_arg] ||
8899 (*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 +01008900 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8901 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008902 goto out_err;
8903 }
8904
8905 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8906 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008907 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8908 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008909 goto out_err;
8910 }
8911 cur_arg++;
8912#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008913 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 +02008914 goto out_err;
8915#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008916 } else if (!strcmp(args[0], "set-mark")) {
8917#ifdef SO_MARK
8918 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008919 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008920 cur_arg = 1;
8921
8922 if (!*args[cur_arg] ||
8923 (*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 +01008924 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8925 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008926 goto out_err;
8927 }
8928
8929 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8930 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008931 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8932 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008933 goto out_err;
8934 }
8935 cur_arg++;
8936 global.last_checks |= LSTCHK_NETADM;
8937#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008938 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 +02008939 goto out_err;
8940#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008941 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008942 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008943 cur_arg = 1;
8944
8945 if (!*args[cur_arg] ||
8946 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8947 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008948 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8949 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008950 goto out_err;
8951 }
8952 if (strcmp(args[cur_arg], "silent") == 0)
8953 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008954 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008955 goto bad_log_level;
8956 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008957 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008958 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008959 cur_arg = 1;
8960
8961 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8962 (*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 +01008963 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8964 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008965 goto out_err;
8966 }
8967
8968 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8969 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8970 LIST_INIT(&rule->arg.hdr_add.fmt);
8971
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008972 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008973 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008974 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 +01008975 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008976 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8977 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008978 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008979 goto out_err;
8980 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008981 free(proxy->conf.lfs_file);
8982 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8983 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008984 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008985 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008986 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008987 cur_arg = 1;
8988
8989 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008990 (*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 +01008991 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8992 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008993 goto out_err;
8994 }
8995
8996 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8997 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8998 LIST_INIT(&rule->arg.hdr_add.fmt);
8999
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009000 error = NULL;
9001 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009002 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9003 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009004 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009005 goto out_err;
9006 }
9007
9008 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009009 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009010 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 +01009011 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009012 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9013 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009014 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009015 goto out_err;
9016 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009017
9018 free(proxy->conf.lfs_file);
9019 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9020 proxy->conf.lfs_line = proxy->conf.args.line;
9021 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009022 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009023 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009024 cur_arg = 1;
9025
9026 if (!*args[cur_arg] ||
9027 (*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 +01009028 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9029 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009030 goto out_err;
9031 }
9032
9033 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9034 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9035
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009036 proxy->conf.args.ctx = ARGC_HRS;
9037 free(proxy->conf.lfs_file);
9038 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9039 proxy->conf.lfs_line = proxy->conf.args.line;
9040 cur_arg += 1;
9041 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9042 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009043 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009044 /*
9045 * '+ 8' for 'add-acl('
9046 * '- 9' for 'add-acl(' + trailing ')'
9047 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009048 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009049
9050 cur_arg = 1;
9051
9052 if (!*args[cur_arg] ||
9053 (*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 +01009054 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9055 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009056 goto out_err;
9057 }
9058
9059 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009060 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009061 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009062 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009063 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009064 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9065 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009066 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009067 goto out_err;
9068 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009069 free(proxy->conf.lfs_file);
9070 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9071 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009072
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009073 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009074 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9075 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009076 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009077 /*
9078 * '+ 8' for 'del-acl('
9079 * '- 9' for 'del-acl(' + trailing ')'
9080 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009081 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009082
9083 cur_arg = 1;
9084
9085 if (!*args[cur_arg] ||
9086 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009087 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9088 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009089 goto out_err;
9090 }
9091
9092 LIST_INIT(&rule->arg.map.key);
9093 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009094 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009095 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009096 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009097 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9098 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009099 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009100 goto out_err;
9101 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009102 free(proxy->conf.lfs_file);
9103 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9104 proxy->conf.lfs_line = proxy->conf.args.line;
9105 cur_arg += 1;
9106 } else if (strncmp(args[0], "del-map", 7) == 0) {
9107 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009108 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009109 /*
9110 * '+ 8' for 'del-map('
9111 * '- 9' for 'del-map(' + trailing ')'
9112 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009113 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009114
9115 cur_arg = 1;
9116
9117 if (!*args[cur_arg] ||
9118 (*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 +01009119 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9120 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009121 goto out_err;
9122 }
9123
9124 LIST_INIT(&rule->arg.map.key);
9125 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009126 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009127 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009128 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009129 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9130 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009131 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009132 goto out_err;
9133 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009134 free(proxy->conf.lfs_file);
9135 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9136 proxy->conf.lfs_line = proxy->conf.args.line;
9137 cur_arg += 1;
9138 } else if (strncmp(args[0], "set-map", 7) == 0) {
9139 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009140 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009141 /*
9142 * '+ 8' for 'set-map('
9143 * '- 9' for 'set-map(' + trailing ')'
9144 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009145 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009146
9147 cur_arg = 1;
9148
9149 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9150 (*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 +01009151 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9152 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009153 goto out_err;
9154 }
9155
9156 LIST_INIT(&rule->arg.map.key);
9157 LIST_INIT(&rule->arg.map.value);
9158
9159 proxy->conf.args.ctx = ARGC_HRS;
9160
9161 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009162 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009163 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009164 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009165 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9166 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009167 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009168 goto out_err;
9169 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009170
9171 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009172 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009173 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 +01009174 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009175 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9176 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009177 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009178 goto out_err;
9179 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009180
9181 free(proxy->conf.lfs_file);
9182 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9183 proxy->conf.lfs_line = proxy->conf.args.line;
9184
9185 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009186 } else if (strcmp(args[0], "redirect") == 0) {
9187 struct redirect_rule *redir;
9188 char *errmsg = NULL;
9189
9190 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009191 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9192 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009193 goto out_err;
9194 }
9195
9196 /* this redirect rule might already contain a parsed condition which
9197 * we'll pass to the http-request rule.
9198 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009199 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009200 rule->arg.redir = redir;
9201 rule->cond = redir->cond;
9202 redir->cond = NULL;
9203 cur_arg = 2;
9204 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009205 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009206 struct sample_expr *expr;
9207 unsigned int where;
9208 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009209 unsigned int tsc_num;
9210 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009211
9212 cur_arg = 1;
9213 proxy->conf.args.ctx = ARGC_TRK;
9214
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009215 tsc_num_str = &args[0][8];
9216 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9217 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9218 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9219 free(err);
9220 goto out_err;
9221 }
9222
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009223 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9224 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009225 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9226 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009227 free(err);
9228 goto out_err;
9229 }
9230
9231 where = 0;
9232 if (proxy->cap & PR_CAP_FE)
9233 where |= SMP_VAL_FE_HRS_HDR;
9234 if (proxy->cap & PR_CAP_BE)
9235 where |= SMP_VAL_BE_HRS_HDR;
9236
9237 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009238 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9239 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9240 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9241 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009242 free(expr);
9243 goto out_err;
9244 }
9245
9246 if (strcmp(args[cur_arg], "table") == 0) {
9247 cur_arg++;
9248 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009249 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9250 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009251 free(expr);
9252 goto out_err;
9253 }
9254 /* we copy the table name for now, it will be resolved later */
9255 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9256 cur_arg++;
9257 }
9258 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009259 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009260 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009261 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9262 char *errmsg = NULL;
9263 cur_arg = 1;
9264 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009265 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009266 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009267 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009268 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9269 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009270 free(errmsg);
9271 goto out_err;
9272 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009273 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009274 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009275 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9276 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9277 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9278 "%s%s, but got '%s'%s.\n",
9279 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009280 goto out_err;
9281 }
9282
9283 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9284 struct acl_cond *cond;
9285 char *errmsg = NULL;
9286
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009287 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009288 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9289 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009290 free(errmsg);
9291 goto out_err;
9292 }
9293 rule->cond = cond;
9294 }
9295 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009296 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9297 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9298 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009299 goto out_err;
9300 }
9301
9302 return rule;
9303 out_err:
9304 free(rule);
9305 return NULL;
9306}
9307
Willy Tarreau4baae242012-12-27 12:00:31 +01009308/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009309 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009310 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9311 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009312 */
9313struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009314 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009315{
9316 struct redirect_rule *rule;
9317 int cur_arg;
9318 int type = REDIRECT_TYPE_NONE;
9319 int code = 302;
9320 const char *destination = NULL;
9321 const char *cookie = NULL;
9322 int cookie_set = 0;
9323 unsigned int flags = REDIRECT_FLAG_NONE;
9324 struct acl_cond *cond = NULL;
9325
9326 cur_arg = 0;
9327 while (*(args[cur_arg])) {
9328 if (strcmp(args[cur_arg], "location") == 0) {
9329 if (!*args[cur_arg + 1])
9330 goto missing_arg;
9331
9332 type = REDIRECT_TYPE_LOCATION;
9333 cur_arg++;
9334 destination = args[cur_arg];
9335 }
9336 else if (strcmp(args[cur_arg], "prefix") == 0) {
9337 if (!*args[cur_arg + 1])
9338 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009339 type = REDIRECT_TYPE_PREFIX;
9340 cur_arg++;
9341 destination = args[cur_arg];
9342 }
9343 else if (strcmp(args[cur_arg], "scheme") == 0) {
9344 if (!*args[cur_arg + 1])
9345 goto missing_arg;
9346
9347 type = REDIRECT_TYPE_SCHEME;
9348 cur_arg++;
9349 destination = args[cur_arg];
9350 }
9351 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9352 if (!*args[cur_arg + 1])
9353 goto missing_arg;
9354
9355 cur_arg++;
9356 cookie = args[cur_arg];
9357 cookie_set = 1;
9358 }
9359 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9360 if (!*args[cur_arg + 1])
9361 goto missing_arg;
9362
9363 cur_arg++;
9364 cookie = args[cur_arg];
9365 cookie_set = 0;
9366 }
9367 else if (strcmp(args[cur_arg], "code") == 0) {
9368 if (!*args[cur_arg + 1])
9369 goto missing_arg;
9370
9371 cur_arg++;
9372 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009373 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009374 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009375 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009376 args[cur_arg - 1], args[cur_arg]);
9377 return NULL;
9378 }
9379 }
9380 else if (!strcmp(args[cur_arg],"drop-query")) {
9381 flags |= REDIRECT_FLAG_DROP_QS;
9382 }
9383 else if (!strcmp(args[cur_arg],"append-slash")) {
9384 flags |= REDIRECT_FLAG_APPEND_SLASH;
9385 }
9386 else if (strcmp(args[cur_arg], "if") == 0 ||
9387 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009388 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009389 if (!cond) {
9390 memprintf(errmsg, "error in condition: %s", *errmsg);
9391 return NULL;
9392 }
9393 break;
9394 }
9395 else {
9396 memprintf(errmsg,
9397 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9398 args[cur_arg]);
9399 return NULL;
9400 }
9401 cur_arg++;
9402 }
9403
9404 if (type == REDIRECT_TYPE_NONE) {
9405 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9406 return NULL;
9407 }
9408
Willy Tarreaube4653b2015-05-28 15:26:58 +02009409 if (dir && type != REDIRECT_TYPE_LOCATION) {
9410 memprintf(errmsg, "response only supports redirect type 'location'");
9411 return NULL;
9412 }
9413
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009414 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009415 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009416 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009417
9418 if (!use_fmt) {
9419 /* old-style static redirect rule */
9420 rule->rdr_str = strdup(destination);
9421 rule->rdr_len = strlen(destination);
9422 }
9423 else {
9424 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009425
9426 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9427 * if prefix == "/", we don't want to add anything, otherwise it
9428 * makes it hard for the user to configure a self-redirection.
9429 */
Godbachd9722032014-12-18 15:44:58 +08009430 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009431 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009432 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9433 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 +01009434 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9435 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009436 return NULL;
9437 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009438 free(curproxy->conf.lfs_file);
9439 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9440 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009441 }
9442 }
9443
Willy Tarreau4baae242012-12-27 12:00:31 +01009444 if (cookie) {
9445 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9446 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9447 */
9448 rule->cookie_len = strlen(cookie);
9449 if (cookie_set) {
9450 rule->cookie_str = malloc(rule->cookie_len + 10);
9451 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9452 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9453 rule->cookie_len += 9;
9454 } else {
9455 rule->cookie_str = malloc(rule->cookie_len + 21);
9456 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9457 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9458 rule->cookie_len += 20;
9459 }
9460 }
9461 rule->type = type;
9462 rule->code = code;
9463 rule->flags = flags;
9464 LIST_INIT(&rule->list);
9465 return rule;
9466
9467 missing_arg:
9468 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9469 return NULL;
9470}
9471
Willy Tarreau8797c062007-05-07 00:55:35 +02009472/************************************************************************/
9473/* The code below is dedicated to ACL parsing and matching */
9474/************************************************************************/
9475
9476
Willy Tarreau14174bc2012-04-16 14:34:04 +02009477/* This function ensures that the prerequisites for an L7 fetch are ready,
9478 * which means that a request or response is ready. If some data is missing,
9479 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009480 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9481 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009482 *
9483 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009484 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9485 * decide whether or not an HTTP message is present ;
9486 * 0 if the requested data cannot be fetched or if it is certain that
9487 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009488 * 1 if an HTTP message is ready
9489 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009490int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009491 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009492{
Willy Tarreau192252e2015-04-04 01:47:55 +02009493 struct http_txn *txn;
9494 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009495
Willy Tarreaube508f12016-03-10 11:47:01 +01009496 /* Note: it is possible that <s> is NULL when called before stream
9497 * initialization (eg: tcp-request connection), so this function is the
9498 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009499 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009500 if (!s)
9501 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009502
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009503 if (!s->txn) {
9504 if (unlikely(!http_alloc_txn(s)))
9505 return 0; /* not enough memory */
9506 http_init_txn(s);
9507 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009508 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009509 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009510
9511 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009512 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009513
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009514 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009515 /* If the buffer does not leave enough free space at the end,
9516 * we must first realign it.
9517 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009518 if (s->req.buf->p > s->req.buf->data &&
9519 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
Willy Tarreaufd8d42f2018-07-12 10:57:15 +02009520 channel_slow_realign(&s->req, trash.str);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009521
Willy Tarreau14174bc2012-04-16 14:34:04 +02009522 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009523 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009524 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009525
9526 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009527 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009528 http_msg_analyzer(msg, &txn->hdr_idx);
9529
9530 /* Still no valid request ? */
9531 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009532 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02009533 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009534 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009535 }
9536 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009537 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009538 return 0;
9539 }
9540
9541 /* OK we just got a valid HTTP request. We have some minor
9542 * preparation to perform so that further checks can rely
9543 * on HTTP tests.
9544 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009545
9546 /* If the request was parsed but was too large, we must absolutely
9547 * return an error so that it is not processed. At the moment this
9548 * cannot happen, but if the parsers are to change in the future,
9549 * we want this check to be maintained.
9550 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009551 if (unlikely(s->req.buf->i + s->req.buf->p >
9552 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009553 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009554 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009555 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009556 return 1;
9557 }
9558
Willy Tarreau9b28e032012-10-12 23:49:43 +02009559 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009560 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009561 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009562
Willy Tarreau506d0502013-07-06 13:29:24 +02009563 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9564 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009565 }
9566
Willy Tarreau506d0502013-07-06 13:29:24 +02009567 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009568 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009569 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009570
9571 /* otherwise everything's ready for the request */
9572 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009573 else {
9574 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009575 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9576 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009577 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009578 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009579 }
9580
9581 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009582 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009583 return 1;
9584}
Willy Tarreau8797c062007-05-07 00:55:35 +02009585
Willy Tarreau8797c062007-05-07 00:55:35 +02009586/* 1. Check on METHOD
9587 * We use the pre-parsed method if it is known, and store its number as an
9588 * integer. If it is unknown, we use the pointer and the length.
9589 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009590static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009591{
9592 int len, meth;
9593
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009594 len = strlen(text);
9595 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009596
9597 pattern->val.i = meth;
9598 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009599 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009600 pattern->len = len;
9601 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009602 else {
9603 pattern->ptr.str = NULL;
9604 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009605 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009606 return 1;
9607}
9608
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009609/* This function fetches the method of current HTTP request and stores
9610 * it in the global pattern struct as a chunk. There are two possibilities :
9611 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9612 * in <len> and <ptr> is NULL ;
9613 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9614 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009615 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009616 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009617static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009618smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009619{
9620 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009621 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009622
Willy Tarreau24e32d82012-04-23 23:55:44 +02009623 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009624
Willy Tarreaube508f12016-03-10 11:47:01 +01009625 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009626 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009627 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009628 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009629 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009630 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9631 /* ensure the indexes are not affected */
9632 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009633 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009634 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9635 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009636 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009637 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009638 return 1;
9639}
9640
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009641/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009642static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009643{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009644 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009645 struct pattern_list *lst;
9646 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009647
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009648 list_for_each_entry(lst, &expr->patterns, list) {
9649 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009650
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009651 /* well-known method */
9652 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009653 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009654 return pattern;
9655 else
9656 continue;
9657 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009658
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009659 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009660 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009661 continue;
9662
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009663 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009664 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9665 (!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 +01009666 return pattern;
9667 }
9668 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009669}
9670
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009672smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009673{
Willy Tarreaube508f12016-03-10 11:47:01 +01009674 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009675 char *ptr;
9676 int len;
9677
Willy Tarreauc0239e02012-04-16 14:42:55 +02009678 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009679
Willy Tarreaube508f12016-03-10 11:47:01 +01009680 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009681 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009682 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009683
9684 while ((len-- > 0) && (*ptr++ != '/'));
9685 if (len <= 0)
9686 return 0;
9687
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009688 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009689 smp->data.u.str.str = ptr;
9690 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009691
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009692 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009693 return 1;
9694}
9695
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009696static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009697smp_fetch_stver(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.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009710 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009711
9712 while ((len-- > 0) && (*ptr++ != '/'));
9713 if (len <= 0)
9714 return 0;
9715
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009716 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009717 smp->data.u.str.str = ptr;
9718 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009719
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009720 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009721 return 1;
9722}
9723
9724/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009725static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009726smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009727{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009728 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009729 char *ptr;
9730 int len;
9731
Willy Tarreauc0239e02012-04-16 14:42:55 +02009732 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009733
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009734 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009735 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9736 return 0;
9737
Willy Tarreau8797c062007-05-07 00:55:35 +02009738 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009739 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009740
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009741 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009742 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009743 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009744 return 1;
9745}
9746
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009747static int
9748smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9749{
9750 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9751 return 0;
9752
9753 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009754 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009755 return 0;
9756 smp->strm->unique_id[0] = '\0';
9757 }
9758 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9759 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9760
9761 smp->data.type = SMP_T_STR;
9762 smp->data.u.str.str = smp->strm->unique_id;
9763 smp->flags = SMP_F_CONST;
9764 return 1;
9765}
9766
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009767/* Returns a string block containing all headers including the
9768 * empty line wich separes headers from the body. This is useful
9769 * form some headers analysis.
9770 */
9771static int
9772smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9773{
9774 struct http_msg *msg;
9775 struct hdr_idx *idx;
9776 struct http_txn *txn;
9777
9778 CHECK_HTTP_MESSAGE_FIRST();
9779
9780 txn = smp->strm->txn;
9781 idx = &txn->hdr_idx;
9782 msg = &txn->req;
9783
9784 smp->data.type = SMP_T_STR;
9785 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9786 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9787 (msg->chn->buf->p[msg->eoh] == '\r');
9788
9789 return 1;
9790}
9791
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009792/* Returns the header request in a length/value encoded format.
9793 * This is useful for exchanges with the SPOE.
9794 *
9795 * A "length value" is a multibyte code encoding numbers. It uses the
9796 * SPOE format. The encoding is the following:
9797 *
9798 * Each couple "header name" / "header value" is composed
9799 * like this:
9800 * "length value" "header name bytes"
9801 * "length value" "header value bytes"
9802 * When the last header is reached, the header name and the header
9803 * value are empty. Their length are 0
9804 */
9805static int
9806smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9807{
9808 struct http_msg *msg;
9809 struct chunk *temp;
9810 struct hdr_idx *idx;
9811 const char *cur_ptr, *cur_next, *p;
9812 int old_idx, cur_idx;
9813 struct hdr_idx_elem *cur_hdr;
9814 const char *hn, *hv;
9815 int hnl, hvl;
9816 int ret;
9817 struct http_txn *txn;
9818 char *buf;
9819 char *end;
9820
9821 CHECK_HTTP_MESSAGE_FIRST();
9822
9823 temp = get_trash_chunk();
9824 buf = temp->str;
9825 end = temp->str + temp->size;
9826
9827 txn = smp->strm->txn;
9828 idx = &txn->hdr_idx;
9829 msg = &txn->req;
9830
9831 /* Build array of headers. */
9832 old_idx = 0;
9833 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9834 while (1) {
9835 cur_idx = idx->v[old_idx].next;
9836 if (!cur_idx)
9837 break;
9838 old_idx = cur_idx;
9839
9840 cur_hdr = &idx->v[cur_idx];
9841 cur_ptr = cur_next;
9842 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9843
9844 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9845 * and the next header starts at cur_next. We'll check
9846 * this header in the list as well as against the default
9847 * rule.
9848 */
9849
9850 /* look for ': *'. */
9851 hn = cur_ptr;
9852 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9853 if (p >= cur_ptr+cur_hdr->len)
9854 continue;
9855 hnl = p - hn;
9856 p++;
9857 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9858 p++;
9859 if (p >= cur_ptr + cur_hdr->len)
9860 continue;
9861 hv = p;
9862 hvl = cur_ptr + cur_hdr->len-p;
9863
9864 /* encode the header name. */
9865 ret = encode_varint(hnl, &buf, end);
9866 if (ret == -1)
9867 return 0;
9868 if (buf + hnl > end)
9869 return 0;
9870 memcpy(buf, hn, hnl);
9871 buf += hnl;
9872
9873 /* encode and copy the value. */
9874 ret = encode_varint(hvl, &buf, end);
9875 if (ret == -1)
9876 return 0;
9877 if (buf + hvl > end)
9878 return 0;
9879 memcpy(buf, hv, hvl);
9880 buf += hvl;
9881 }
9882
9883 /* encode the end of the header list with empty
9884 * header name and header value.
9885 */
9886 ret = encode_varint(0, &buf, end);
9887 if (ret == -1)
9888 return 0;
9889 ret = encode_varint(0, &buf, end);
9890 if (ret == -1)
9891 return 0;
9892
9893 /* Initialise sample data which will be filled. */
9894 smp->data.type = SMP_T_BIN;
9895 smp->data.u.str.str = temp->str;
9896 smp->data.u.str.len = buf - temp->str;
9897 smp->data.u.str.size = temp->size;
9898
9899 return 1;
9900}
9901
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009902/* returns the longest available part of the body. This requires that the body
9903 * has been waited for using http-buffer-request.
9904 */
9905static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009906smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009907{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009908 struct http_msg *msg;
9909 unsigned long len;
9910 unsigned long block1;
9911 char *body;
9912 struct chunk *temp;
9913
9914 CHECK_HTTP_MESSAGE_FIRST();
9915
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009916 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009917 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009918 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009919 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009920
9921 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009922 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009923
9924 block1 = len;
9925 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9926 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9927
9928 if (block1 == len) {
9929 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009930 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009931 smp->data.u.str.str = body;
9932 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009933 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9934 }
9935 else {
9936 /* buffer is wrapped, we need to defragment it */
9937 temp = get_trash_chunk();
9938 memcpy(temp->str, body, block1);
9939 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009940 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009941 smp->data.u.str.str = temp->str;
9942 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009943 smp->flags = SMP_F_VOL_TEST;
9944 }
9945 return 1;
9946}
9947
9948
9949/* returns the available length of the body. This requires that the body
9950 * has been waited for using http-buffer-request.
9951 */
9952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009953smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009954{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009955 struct http_msg *msg;
9956
9957 CHECK_HTTP_MESSAGE_FIRST();
9958
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009959 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009960 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009961 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009962 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009963
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009964 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009965 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009966
9967 smp->flags = SMP_F_VOL_TEST;
9968 return 1;
9969}
9970
9971
9972/* returns the advertised length of the body, or the advertised size of the
9973 * chunks available in the buffer. This requires that the body has been waited
9974 * for using http-buffer-request.
9975 */
9976static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009977smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009978{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009979 struct http_msg *msg;
9980
9981 CHECK_HTTP_MESSAGE_FIRST();
9982
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009983 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009984 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009985 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009986 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009987
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009988 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009989 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009990
9991 smp->flags = SMP_F_VOL_TEST;
9992 return 1;
9993}
9994
9995
Willy Tarreau8797c062007-05-07 00:55:35 +02009996/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009997static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009998smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009999{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010000 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010001
Willy Tarreauc0239e02012-04-16 14:42:55 +020010002 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010003
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010004 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010005 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010006 smp->data.u.str.len = txn->req.sl.rq.u_l;
10007 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010008 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010009 return 1;
10010}
10011
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010012static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010013smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010014{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010015 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010016 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010017
Willy Tarreauc0239e02012-04-16 14:42:55 +020010018 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010019
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010020 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010021 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 +020010022 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010023 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010024
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010025 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010026 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010027 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010028 return 1;
10029}
10030
10031static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010032smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010033{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010034 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010035 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010036
Willy Tarreauc0239e02012-04-16 14:42:55 +020010037 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010038
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010039 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010040 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 +020010041 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10042 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010043
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010044 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010045 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010046 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010047 return 1;
10048}
10049
Willy Tarreau185b5c42012-04-26 15:11:51 +020010050/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10051 * Accepts an optional argument of type string containing the header field name,
10052 * and an optional argument of type signed or unsigned integer to request an
10053 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010054 * headers are considered from the first one. It does not stop on commas and
10055 * returns full lines instead (useful for User-Agent or Date for example).
10056 */
10057static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010058smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010059{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010060 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010061 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010062 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010063 int occ = 0;
10064 const char *name_str = NULL;
10065 int name_len = 0;
10066
10067 if (!ctx) {
10068 /* first call */
10069 ctx = &static_hdr_ctx;
10070 ctx->idx = 0;
10071 smp->ctx.a[0] = ctx;
10072 }
10073
10074 if (args) {
10075 if (args[0].type != ARGT_STR)
10076 return 0;
10077 name_str = args[0].data.str.str;
10078 name_len = args[0].data.str.len;
10079
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010080 if (args[1].type == ARGT_SINT)
10081 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010082 }
10083
10084 CHECK_HTTP_MESSAGE_FIRST();
10085
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010086 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010087 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 +020010088
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010089 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10090 /* search for header from the beginning */
10091 ctx->idx = 0;
10092
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010093 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010094 /* no explicit occurrence and single fetch => last header by default */
10095 occ = -1;
10096
10097 if (!occ)
10098 /* prepare to report multiple occurrences for ACL fetches */
10099 smp->flags |= SMP_F_NOT_LAST;
10100
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010101 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010102 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010103 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 +020010104 return 1;
10105
10106 smp->flags &= ~SMP_F_NOT_LAST;
10107 return 0;
10108}
10109
10110/* 6. Check on HTTP header count. The number of occurrences is returned.
10111 * Accepts exactly 1 argument of type string. It does not stop on commas and
10112 * returns full lines instead (useful for User-Agent or Date for example).
10113 */
10114static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010115smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010116{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010117 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010118 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010119 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010120 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010121 const char *name = NULL;
10122 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010123
Willy Tarreau601a4d12015-04-01 19:16:09 +020010124 if (args && args->type == ARGT_STR) {
10125 name = args->data.str.str;
10126 len = args->data.str.len;
10127 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010128
10129 CHECK_HTTP_MESSAGE_FIRST();
10130
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010131 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010132 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 +020010133
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010134 ctx.idx = 0;
10135 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010136 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010137 cnt++;
10138
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010139 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010140 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010141 smp->flags = SMP_F_VOL_HDR;
10142 return 1;
10143}
10144
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010145static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010146smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010147{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010148 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010149 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010150 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010151 struct chunk *temp;
10152 char del = ',';
10153
10154 if (args && args->type == ARGT_STR)
10155 del = *args[0].data.str.str;
10156
10157 CHECK_HTTP_MESSAGE_FIRST();
10158
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010159 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010160 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 +020010161
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010162 temp = get_trash_chunk();
10163
10164 ctx.idx = 0;
10165 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10166 if (temp->len)
10167 temp->str[temp->len++] = del;
10168 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10169 temp->len += ctx.del;
10170 }
10171
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010172 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010173 smp->data.u.str.str = temp->str;
10174 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010175 smp->flags = SMP_F_VOL_HDR;
10176 return 1;
10177}
10178
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010179/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10180 * Accepts an optional argument of type string containing the header field name,
10181 * and an optional argument of type signed or unsigned integer to request an
10182 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010183 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010184 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010185static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010186smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010187{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010188 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010189 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010190 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010191 int occ = 0;
10192 const char *name_str = NULL;
10193 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010194
Willy Tarreaua890d072013-04-02 12:01:06 +020010195 if (!ctx) {
10196 /* first call */
10197 ctx = &static_hdr_ctx;
10198 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010199 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010200 }
10201
Willy Tarreau185b5c42012-04-26 15:11:51 +020010202 if (args) {
10203 if (args[0].type != ARGT_STR)
10204 return 0;
10205 name_str = args[0].data.str.str;
10206 name_len = args[0].data.str.len;
10207
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010208 if (args[1].type == ARGT_SINT)
10209 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010210 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010211
Willy Tarreaue333ec92012-04-16 16:26:40 +020010212 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010213
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010214 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010215 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 +020010216
Willy Tarreau185b5c42012-04-26 15:11:51 +020010217 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010218 /* search for header from the beginning */
10219 ctx->idx = 0;
10220
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010221 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010222 /* no explicit occurrence and single fetch => last header by default */
10223 occ = -1;
10224
10225 if (!occ)
10226 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010227 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010228
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010229 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010230 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010231 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 +020010232 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010233
Willy Tarreau37406352012-04-23 16:16:37 +020010234 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010235 return 0;
10236}
10237
Willy Tarreauc11416f2007-06-17 16:58:38 +020010238/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010239 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010240 */
10241static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010242smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010243{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010244 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010245 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010246 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010247 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010248 const char *name = NULL;
10249 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010250
Willy Tarreau601a4d12015-04-01 19:16:09 +020010251 if (args && args->type == ARGT_STR) {
10252 name = args->data.str.str;
10253 len = args->data.str.len;
10254 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010255
Willy Tarreaue333ec92012-04-16 16:26:40 +020010256 CHECK_HTTP_MESSAGE_FIRST();
10257
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010258 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010259 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 +020010260
Willy Tarreau33a7e692007-06-10 19:45:56 +020010261 ctx.idx = 0;
10262 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010263 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010264 cnt++;
10265
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010266 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010267 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010268 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010269 return 1;
10270}
10271
Willy Tarreau185b5c42012-04-26 15:11:51 +020010272/* Fetch an HTTP header's integer value. The integer value is returned. It
10273 * takes a mandatory argument of type string and an optional one of type int
10274 * to designate a specific occurrence. It returns an unsigned integer, which
10275 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010276 */
10277static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010278smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010279{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010280 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010281
Willy Tarreauf853c462012-04-23 18:53:56 +020010282 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010283 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010284 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010285 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010286
Willy Tarreaud53e2422012-04-16 17:21:11 +020010287 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010288}
10289
Cyril Bonté69fa9922012-10-25 00:01:06 +020010290/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10291 * and an optional one of type int to designate a specific occurrence.
10292 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010293 */
10294static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010295smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010296{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010297 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010298
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010299 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010300 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010301 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010302 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010303 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010304 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010305 if (smp->data.u.str.len < temp->size - 1) {
10306 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10307 temp->str[smp->data.u.str.len] = '\0';
10308 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010309 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010310 break;
10311 }
10312 }
10313 }
10314
Willy Tarreaud53e2422012-04-16 17:21:11 +020010315 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010316 if (!(smp->flags & SMP_F_NOT_LAST))
10317 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010318 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010319 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010320}
10321
Willy Tarreau737b0c12007-06-10 21:28:46 +020010322/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10323 * the first '/' after the possible hostname, and ends before the possible '?'.
10324 */
10325static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010326smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010327{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010328 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010329 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010330
Willy Tarreauc0239e02012-04-16 14:42:55 +020010331 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010332
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010333 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010334 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010335 ptr = http_get_path(txn);
10336 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010337 return 0;
10338
10339 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010340 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010341 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010342
10343 while (ptr < end && *ptr != '?')
10344 ptr++;
10345
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010346 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010347 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010348 return 1;
10349}
10350
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010351/* This produces a concatenation of the first occurrence of the Host header
10352 * followed by the path component if it begins with a slash ('/'). This means
10353 * that '*' will not be added, resulting in exactly the first Host entry.
10354 * If no Host header is found, then the path is returned as-is. The returned
10355 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010356 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010357 */
10358static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010359smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010360{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010361 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010362 char *ptr, *end, *beg;
10363 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010364 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010365
10366 CHECK_HTTP_MESSAGE_FIRST();
10367
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010368 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010369 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010370 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010371 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010372
10373 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010374 temp = get_trash_chunk();
10375 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010376 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010377 smp->data.u.str.str = temp->str;
10378 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010379
10380 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010381 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010382 beg = http_get_path(txn);
10383 if (!beg)
10384 beg = end;
10385
10386 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10387
10388 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010389 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10390 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010391 }
10392
10393 smp->flags = SMP_F_VOL_1ST;
10394 return 1;
10395}
10396
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010397/* This produces a 32-bit hash of the concatenation of the first occurrence of
10398 * the Host header followed by the path component if it begins with a slash ('/').
10399 * This means that '*' will not be added, resulting in exactly the first Host
10400 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010401 * is hashed using the path hash followed by a full avalanche hash and provides a
10402 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010403 * high-traffic sites without having to store whole paths.
10404 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010405int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010406smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010407{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010408 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010409 struct hdr_ctx ctx;
10410 unsigned int hash = 0;
10411 char *ptr, *beg, *end;
10412 int len;
10413
10414 CHECK_HTTP_MESSAGE_FIRST();
10415
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010416 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010417 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010418 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010419 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10420 ptr = ctx.line + ctx.val;
10421 len = ctx.vlen;
10422 while (len--)
10423 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10424 }
10425
10426 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010427 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010428 beg = http_get_path(txn);
10429 if (!beg)
10430 beg = end;
10431
10432 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10433
10434 if (beg < ptr && *beg == '/') {
10435 while (beg < ptr)
10436 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10437 }
10438 hash = full_hash(hash);
10439
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010440 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010441 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010442 smp->flags = SMP_F_VOL_1ST;
10443 return 1;
10444}
10445
Willy Tarreau4a550602012-12-09 14:53:32 +010010446/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010447 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10448 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10449 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010450 * that in environments where IPv6 is insignificant, truncating the output to
10451 * 8 bytes would still work.
10452 */
10453static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010455{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010456 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010457 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010458
10459 if (!cli_conn)
10460 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010461
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010462 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010463 return 0;
10464
Willy Tarreau47ca5452012-12-23 20:22:19 +010010465 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010466 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010467 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010468
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010469 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010470 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010471 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010472 temp->len += 4;
10473 break;
10474 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010475 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010476 temp->len += 16;
10477 break;
10478 default:
10479 return 0;
10480 }
10481
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010482 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010483 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010484 return 1;
10485}
10486
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010487/* Extracts the query string, which comes after the question mark '?'. If no
10488 * question mark is found, nothing is returned. Otherwise it returns a sample
10489 * of type string carrying the whole query string.
10490 */
10491static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010492smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010493{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010494 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010495 char *ptr, *end;
10496
10497 CHECK_HTTP_MESSAGE_FIRST();
10498
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010499 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010500 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10501 end = ptr + txn->req.sl.rq.u_l;
10502
10503 /* look up the '?' */
10504 do {
10505 if (ptr == end)
10506 return 0;
10507 } while (*ptr++ != '?');
10508
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010509 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010510 smp->data.u.str.str = ptr;
10511 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010512 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10513 return 1;
10514}
10515
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010516static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010517smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010518{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010519 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10520 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10521 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010522
Willy Tarreau24e32d82012-04-23 23:55:44 +020010523 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010524
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010525 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010526 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010527 return 1;
10528}
10529
Willy Tarreau7f18e522010-10-22 20:04:13 +020010530/* return a valid test if the current request is the first one on the connection */
10531static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010532smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010533{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010534 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010535 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010536 return 1;
10537}
10538
Willy Tarreau34db1082012-04-19 17:16:54 +020010539/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010540static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010541smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010542{
10543
Willy Tarreau24e32d82012-04-23 23:55:44 +020010544 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010545 return 0;
10546
Willy Tarreauc0239e02012-04-16 14:42:55 +020010547 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010548
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010549 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010550 return 0;
10551
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010552 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010553 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010554 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010555 return 1;
10556}
Willy Tarreau8797c062007-05-07 00:55:35 +020010557
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010558/* Accepts exactly 1 argument of type userlist */
10559static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010560smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010561{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010562 if (!args || args->type != ARGT_USR)
10563 return 0;
10564
10565 CHECK_HTTP_MESSAGE_FIRST();
10566
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010567 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010568 return 0;
10569
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010570 /* if the user does not belong to the userlist or has a wrong password,
10571 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010572 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010573 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010574 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10575 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010576 return 0;
10577
10578 /* pat_match_auth() will need the user list */
10579 smp->ctx.a[0] = args->data.usr;
10580
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010581 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010582 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010583 smp->data.u.str.str = smp->strm->txn->auth.user;
10584 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010585
10586 return 1;
10587}
10588
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010589/* Try to find the next occurrence of a cookie name in a cookie header value.
10590 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10591 * the cookie value is returned into *value and *value_l, and the function
10592 * returns a pointer to the next pointer to search from if the value was found.
10593 * Otherwise if the cookie was not found, NULL is returned and neither value
10594 * nor value_l are touched. The input <hdr> string should first point to the
10595 * header's value, and the <hdr_end> pointer must point to the first character
10596 * not part of the value. <list> must be non-zero if value may represent a list
10597 * of values (cookie headers). This makes it faster to abort parsing when no
10598 * list is expected.
10599 */
David Carlier4686f792015-09-25 14:10:50 +010010600char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010601extract_cookie_value(char *hdr, const char *hdr_end,
10602 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010603 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010604{
10605 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10606 char *next;
10607
10608 /* we search at least a cookie name followed by an equal, and more
10609 * generally something like this :
10610 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10611 */
10612 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10613 /* Iterate through all cookies on this line */
10614
Willy Tarreau2235b262016-11-05 15:50:20 +010010615 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010616 att_beg++;
10617
10618 /* find att_end : this is the first character after the last non
10619 * space before the equal. It may be equal to hdr_end.
10620 */
10621 equal = att_end = att_beg;
10622
10623 while (equal < hdr_end) {
10624 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10625 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010626 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010627 continue;
10628 att_end = equal;
10629 }
10630
10631 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10632 * is between <att_beg> and <equal>, both may be identical.
10633 */
10634
10635 /* look for end of cookie if there is an equal sign */
10636 if (equal < hdr_end && *equal == '=') {
10637 /* look for the beginning of the value */
10638 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010639 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010640 val_beg++;
10641
10642 /* find the end of the value, respecting quotes */
10643 next = find_cookie_value_end(val_beg, hdr_end);
10644
10645 /* make val_end point to the first white space or delimitor after the value */
10646 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010647 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010648 val_end--;
10649 } else {
10650 val_beg = val_end = next = equal;
10651 }
10652
10653 /* We have nothing to do with attributes beginning with '$'. However,
10654 * they will automatically be removed if a header before them is removed,
10655 * since they're supposed to be linked together.
10656 */
10657 if (*att_beg == '$')
10658 continue;
10659
10660 /* Ignore cookies with no equal sign */
10661 if (equal == next)
10662 continue;
10663
10664 /* Now we have the cookie name between att_beg and att_end, and
10665 * its value between val_beg and val_end.
10666 */
10667
10668 if (att_end - att_beg == cookie_name_l &&
10669 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10670 /* let's return this value and indicate where to go on from */
10671 *value = val_beg;
10672 *value_l = val_end - val_beg;
10673 return next + 1;
10674 }
10675
10676 /* Set-Cookie headers only have the name in the first attr=value part */
10677 if (!list)
10678 break;
10679 }
10680
10681 return NULL;
10682}
10683
William Lallemanda43ba4e2014-01-28 18:14:25 +010010684/* Fetch a captured HTTP request header. The index is the position of
10685 * the "capture" option in the configuration file
10686 */
10687static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010688smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010689{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010690 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010691 int idx;
10692
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010693 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010694 return 0;
10695
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010696 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010697
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010698 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 +010010699 return 0;
10700
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010701 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010702 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010703 smp->data.u.str.str = smp->strm->req_cap[idx];
10704 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010705
10706 return 1;
10707}
10708
10709/* Fetch a captured HTTP response header. The index is the position of
10710 * the "capture" option in the configuration file
10711 */
10712static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010713smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010714{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010715 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010716 int idx;
10717
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010718 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010719 return 0;
10720
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010721 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010722
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010723 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 +010010724 return 0;
10725
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010726 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010727 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010728 smp->data.u.str.str = smp->strm->res_cap[idx];
10729 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010730
10731 return 1;
10732}
10733
William Lallemand65ad6e12014-01-31 15:08:02 +010010734/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10735static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010736smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010737{
10738 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010739 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010740 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010741
Willy Tarreau15e91e12015-04-04 00:52:09 +020010742 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010743 return 0;
10744
William Lallemand96a77852014-02-05 00:30:02 +010010745 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010746
William Lallemand96a77852014-02-05 00:30:02 +010010747 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10748 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010749
William Lallemand96a77852014-02-05 00:30:02 +010010750 temp = get_trash_chunk();
10751 temp->str = txn->uri;
10752 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010753 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010754 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010755 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010756
10757 return 1;
10758
10759}
10760
10761/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10762static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010763smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010764{
10765 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010766 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010767 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010768
Willy Tarreau15e91e12015-04-04 00:52:09 +020010769 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010770 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010771
William Lallemand65ad6e12014-01-31 15:08:02 +010010772 ptr = txn->uri;
10773
10774 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10775 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010776
William Lallemand65ad6e12014-01-31 15:08:02 +010010777 if (!*ptr)
10778 return 0;
10779
10780 ptr++; /* skip the space */
10781
10782 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010783 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010784 if (!ptr)
10785 return 0;
10786 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10787 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010788
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010789 smp->data.u.str = *temp;
10790 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010791 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010792 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010793
10794 return 1;
10795}
10796
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010797/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10798 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10799 */
10800static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010801smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010802{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010803 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010804
Willy Tarreau15e91e12015-04-04 00:52:09 +020010805 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010806 return 0;
10807
10808 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010809 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010810 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010811 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010812
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010813 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010814 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010815 smp->flags = SMP_F_CONST;
10816 return 1;
10817
10818}
10819
10820/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10821 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10822 */
10823static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010824smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010825{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010826 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010827
Willy Tarreau15e91e12015-04-04 00:52:09 +020010828 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010829 return 0;
10830
10831 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010832 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010833 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010834 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010835
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010836 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010837 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010838 smp->flags = SMP_F_CONST;
10839 return 1;
10840
10841}
10842
William Lallemand65ad6e12014-01-31 15:08:02 +010010843
Willy Tarreaue333ec92012-04-16 16:26:40 +020010844/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010845 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010846 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010847 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010848 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010849 * Accepts exactly 1 argument of type string. If the input options indicate
10850 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010851 * The returned sample is of type CSTR. Can be used to parse cookies in other
10852 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010853 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010854int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010855{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010856 struct http_txn *txn;
10857 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010858 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010859 const struct http_msg *msg;
10860 const char *hdr_name;
10861 int hdr_name_len;
10862 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010863 int occ = 0;
10864 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010865
Willy Tarreau24e32d82012-04-23 23:55:44 +020010866 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010867 return 0;
10868
Willy Tarreaua890d072013-04-02 12:01:06 +020010869 if (!ctx) {
10870 /* first call */
10871 ctx = &static_hdr_ctx;
10872 ctx->idx = 0;
10873 smp->ctx.a[2] = ctx;
10874 }
10875
Willy Tarreaue333ec92012-04-16 16:26:40 +020010876 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010877
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010878 txn = smp->strm->txn;
10879 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010880
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010881 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010882 msg = &txn->req;
10883 hdr_name = "Cookie";
10884 hdr_name_len = 6;
10885 } else {
10886 msg = &txn->rsp;
10887 hdr_name = "Set-Cookie";
10888 hdr_name_len = 10;
10889 }
10890
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010891 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010892 /* no explicit occurrence and single fetch => last cookie by default */
10893 occ = -1;
10894
10895 /* OK so basically here, either we want only one value and it's the
10896 * last one, or we want to iterate over all of them and we fetch the
10897 * next one.
10898 */
10899
Willy Tarreau9b28e032012-10-12 23:49:43 +020010900 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010901 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010902 /* search for the header from the beginning, we must first initialize
10903 * the search parameters.
10904 */
Willy Tarreau37406352012-04-23 16:16:37 +020010905 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010906 ctx->idx = 0;
10907 }
10908
Willy Tarreau28376d62012-04-26 21:26:10 +020010909 smp->flags |= SMP_F_VOL_HDR;
10910
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010911 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010912 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10913 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10915 goto out;
10916
Willy Tarreau24e32d82012-04-23 23:55:44 +020010917 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010918 continue;
10919
Willy Tarreau37406352012-04-23 16:16:37 +020010920 smp->ctx.a[0] = ctx->line + ctx->val;
10921 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010922 }
10923
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010924 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010925 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010926 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010927 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010928 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010929 &smp->data.u.str.str,
10930 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010931 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010932 found = 1;
10933 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010934 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010935 smp->flags |= SMP_F_NOT_LAST;
10936 return 1;
10937 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010938 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010939 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010940 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010941 /* all cookie headers and values were scanned. If we're looking for the
10942 * last occurrence, we may return it now.
10943 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010944 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010945 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010946 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010947}
10948
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010949/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010950 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010951 * multiple cookies may be parsed on the same line. The returned sample is of
10952 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010953 */
10954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010955smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010956{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010957 struct http_txn *txn;
10958 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010959 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010960 const struct http_msg *msg;
10961 const char *hdr_name;
10962 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010963 int cnt;
10964 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010965 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010966
Willy Tarreau24e32d82012-04-23 23:55:44 +020010967 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010968 return 0;
10969
Willy Tarreaue333ec92012-04-16 16:26:40 +020010970 CHECK_HTTP_MESSAGE_FIRST();
10971
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010972 txn = smp->strm->txn;
10973 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010974
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010975 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010976 msg = &txn->req;
10977 hdr_name = "Cookie";
10978 hdr_name_len = 6;
10979 } else {
10980 msg = &txn->rsp;
10981 hdr_name = "Set-Cookie";
10982 hdr_name_len = 10;
10983 }
10984
Willy Tarreau9b28e032012-10-12 23:49:43 +020010985 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010986 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010987 ctx.idx = 0;
10988 cnt = 0;
10989
10990 while (1) {
10991 /* Note: val_beg == NULL every time we need to fetch a new header */
10992 if (!val_beg) {
10993 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10994 break;
10995
Willy Tarreau24e32d82012-04-23 23:55:44 +020010996 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010997 continue;
10998
10999 val_beg = ctx.line + ctx.val;
11000 val_end = val_beg + ctx.vlen;
11001 }
11002
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011003 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011004 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011005 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011006 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011007 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011008 &smp->data.u.str.str,
11009 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011010 cnt++;
11011 }
11012 }
11013
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011014 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011015 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011016 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011017 return 1;
11018}
11019
Willy Tarreau51539362012-05-08 12:46:28 +020011020/* Fetch an cookie's integer value. The integer value is returned. It
11021 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11022 */
11023static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011024smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011025{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011026 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011027
11028 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011029 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011030 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011031 }
11032
11033 return ret;
11034}
11035
Willy Tarreau8797c062007-05-07 00:55:35 +020011036/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011037/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011038/************************************************************************/
11039
David Cournapeau16023ee2010-12-23 20:55:41 +090011040/*
11041 * Given a path string and its length, find the position of beginning of the
11042 * query string. Returns NULL if no query string is found in the path.
11043 *
11044 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11045 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011046 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011047 */
bedis4c75cca2012-10-05 08:38:24 +020011048static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011049{
11050 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011051
bedis4c75cca2012-10-05 08:38:24 +020011052 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011053 return p ? p + 1 : NULL;
11054}
11055
bedis4c75cca2012-10-05 08:38:24 +020011056static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011057{
bedis4c75cca2012-10-05 08:38:24 +020011058 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011059}
11060
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011061/* after increasing a pointer value, it can exceed the first buffer
11062 * size. This function transform the value of <ptr> according with
11063 * the expected position. <chunks> is an array of the one or two
11064 * avalaible chunks. The first value is the start of the first chunk,
11065 * the second value if the end+1 of the first chunks. The third value
11066 * is NULL or the start of the second chunk and the fourth value is
11067 * the end+1 of the second chunk. The function returns 1 if does a
11068 * wrap, else returns 0.
11069 */
11070static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11071{
11072 if (*ptr < chunks[1])
11073 return 0;
11074 if (!chunks[2])
11075 return 0;
11076 *ptr = chunks[2] + ( *ptr - chunks[1] );
11077 return 1;
11078}
11079
David Cournapeau16023ee2010-12-23 20:55:41 +090011080/*
11081 * Given a url parameter, find the starting position of the first occurence,
11082 * or NULL if the parameter is not found.
11083 *
11084 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11085 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011086 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011087 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011088 * or the second chunk. The caller must be check the position before using the
11089 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011090 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011091static const char *
11092find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011093 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011094 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011095{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011096 const char *pos, *last, *equal;
11097 const char **bufs = chunks;
11098 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011099
David Cournapeau16023ee2010-12-23 20:55:41 +090011100
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011101 pos = bufs[0];
11102 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011103 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011104 /* Check the equal. */
11105 equal = pos + url_param_name_l;
11106 if (fix_pointer_if_wrap(chunks, &equal)) {
11107 if (equal >= chunks[3])
11108 return NULL;
11109 } else {
11110 if (equal >= chunks[1])
11111 return NULL;
11112 }
11113 if (*equal == '=') {
11114 if (pos + url_param_name_l > last) {
11115 /* process wrap case, we detect a wrap. In this case, the
11116 * comparison is performed in two parts.
11117 */
11118
11119 /* This is the end, we dont have any other chunk. */
11120 if (bufs != chunks || !bufs[2])
11121 return NULL;
11122
11123 /* Compute the length of each part of the comparison. */
11124 l1 = last - pos;
11125 l2 = url_param_name_l - l1;
11126
11127 /* The second buffer is too short to contain the compared string. */
11128 if (bufs[2] + l2 > bufs[3])
11129 return NULL;
11130
11131 if (memcmp(pos, url_param_name, l1) == 0 &&
11132 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11133 return pos;
11134
11135 /* Perform wrapping and jump the string who fail the comparison. */
11136 bufs += 2;
11137 pos = bufs[0] + l2;
11138 last = bufs[1];
11139
11140 } else {
11141 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011142 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11143 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011144 pos += url_param_name_l + 1;
11145 if (fix_pointer_if_wrap(chunks, &pos))
11146 last = bufs[2];
11147 }
11148 }
11149
11150 while (1) {
11151 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011152 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011153 pos++;
11154 if (pos < last)
11155 break;
11156 /* process buffer wrapping. */
11157 if (bufs != chunks || !bufs[2])
11158 return NULL;
11159 bufs += 2;
11160 pos = bufs[0];
11161 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011162 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011163 pos++;
11164 }
11165 return NULL;
11166}
11167
11168/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011169 * Given a url parameter name and a query string, find the next value.
11170 * An empty url_param_name matches the first available parameter.
11171 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11172 * respectively provide a pointer to the value and its end.
11173 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011174 */
11175static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011176find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011177 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011178 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011179{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011180 const char *arg_start, *qs_end;
11181 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011182
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011183 arg_start = chunks[0];
11184 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011185 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011186 /* Looks for an argument name. */
11187 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011188 url_param_name, url_param_name_l,
11189 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011190 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011191 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011192 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011193 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011194 if (!arg_start)
11195 return 0;
11196
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011197 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011198 while (1) {
11199 /* looks for the first argument. */
11200 value_start = memchr(arg_start, '=', qs_end - arg_start);
11201 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011202 /* Check for wrapping. */
11203 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011204 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011205 chunks[2]) {
11206 arg_start = chunks[2];
11207 qs_end = chunks[3];
11208 continue;
11209 }
11210 return 0;
11211 }
11212 break;
11213 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011214 value_start++;
11215 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011216 else {
11217 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011218 value_start = arg_start + url_param_name_l + 1;
11219
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011220 /* Check for pointer wrapping. */
11221 if (fix_pointer_if_wrap(chunks, &value_start)) {
11222 /* Update the end pointer. */
11223 qs_end = chunks[3];
11224
11225 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011226 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011227 return 0;
11228 }
11229 }
11230
David Cournapeau16023ee2010-12-23 20:55:41 +090011231 value_end = value_start;
11232
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011233 while (1) {
11234 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11235 value_end++;
11236 if (value_end < qs_end)
11237 break;
11238 /* process buffer wrapping. */
11239 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011240 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011241 chunks[2]) {
11242 value_end = chunks[2];
11243 qs_end = chunks[3];
11244 continue;
11245 }
11246 break;
11247 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011248
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011249 *vstart = value_start;
11250 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011251 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011252}
11253
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011254/* This scans a URL-encoded query string. It takes an optionally wrapping
11255 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11256 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11257 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011258 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011259static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011260smp_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 +090011261{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011262 const char *vstart, *vend;
11263 struct chunk *temp;
11264 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011265
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011266 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011267 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011268 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011269 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011270 return 0;
11271
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011272 /* Create sample. If the value is contiguous, return the pointer as CONST,
11273 * if the value is wrapped, copy-it in a buffer.
11274 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011275 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011276 if (chunks[2] &&
11277 vstart >= chunks[0] && vstart <= chunks[1] &&
11278 vend >= chunks[2] && vend <= chunks[3]) {
11279 /* Wrapped case. */
11280 temp = get_trash_chunk();
11281 memcpy(temp->str, vstart, chunks[1] - vstart);
11282 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011283 smp->data.u.str.str = temp->str;
11284 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011285 } else {
11286 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011287 smp->data.u.str.str = (char *)vstart;
11288 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011289 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11290 }
11291
11292 /* Update context, check wrapping. */
11293 chunks[0] = vend;
11294 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11295 chunks[1] = chunks[3];
11296 chunks[2] = NULL;
11297 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011298
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011299 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011300 smp->flags |= SMP_F_NOT_LAST;
11301
David Cournapeau16023ee2010-12-23 20:55:41 +090011302 return 1;
11303}
11304
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011305/* This function iterates over each parameter of the query string. It uses
11306 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011307 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11308 * An optional parameter name is passed in args[0], otherwise any parameter is
11309 * considered. It supports an optional delimiter argument for the beginning of
11310 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011311 */
11312static int
11313smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11314{
11315 struct http_msg *msg;
11316 char delim = '?';
11317 const char *name;
11318 int name_len;
11319
Dragan Dosen26f77e52015-05-25 10:02:11 +020011320 if (!args ||
11321 (args[0].type && args[0].type != ARGT_STR) ||
11322 (args[1].type && args[1].type != ARGT_STR))
11323 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011324
Dragan Dosen26f77e52015-05-25 10:02:11 +020011325 name = "";
11326 name_len = 0;
11327 if (args->type == ARGT_STR) {
11328 name = args->data.str.str;
11329 name_len = args->data.str.len;
11330 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011331
Dragan Dosen26f77e52015-05-25 10:02:11 +020011332 if (args[1].type)
11333 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011334
Dragan Dosen26f77e52015-05-25 10:02:11 +020011335 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011336 CHECK_HTTP_MESSAGE_FIRST();
11337
11338 msg = &smp->strm->txn->req;
11339
11340 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11341 msg->sl.rq.u_l, delim);
11342 if (!smp->ctx.a[0])
11343 return 0;
11344
11345 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011346
11347 /* Assume that the context is filled with NULL pointer
11348 * before the first call.
11349 * smp->ctx.a[2] = NULL;
11350 * smp->ctx.a[3] = NULL;
11351 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011352 }
11353
11354 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11355}
11356
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011357/* This function iterates over each parameter of the body. This requires
11358 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011359 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11360 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11361 * optional second part if the body wraps at the end of the buffer. An optional
11362 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011363 */
11364static int
11365smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11366{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011367 struct http_msg *msg;
11368 unsigned long len;
11369 unsigned long block1;
11370 char *body;
11371 const char *name;
11372 int name_len;
11373
11374 if (!args || (args[0].type && args[0].type != ARGT_STR))
11375 return 0;
11376
11377 name = "";
11378 name_len = 0;
11379 if (args[0].type == ARGT_STR) {
11380 name = args[0].data.str.str;
11381 name_len = args[0].data.str.len;
11382 }
11383
11384 if (!smp->ctx.a[0]) { // first call, find the query string
11385 CHECK_HTTP_MESSAGE_FIRST();
11386
11387 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011388 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011389 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011390 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011391
11392 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020011393 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011394
11395 block1 = len;
11396 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11397 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11398
11399 if (block1 == len) {
11400 /* buffer is not wrapped (or empty) */
11401 smp->ctx.a[0] = body;
11402 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011403
11404 /* Assume that the context is filled with NULL pointer
11405 * before the first call.
11406 * smp->ctx.a[2] = NULL;
11407 * smp->ctx.a[3] = NULL;
11408 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011409 }
11410 else {
11411 /* buffer is wrapped, we need to defragment it */
11412 smp->ctx.a[0] = body;
11413 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011414 smp->ctx.a[2] = msg->chn->buf->data;
11415 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011416 }
11417 }
11418 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11419}
11420
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011421/* Return the signed integer value for the specified url parameter (see url_param
11422 * above).
11423 */
11424static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011425smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011426{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011427 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011428
11429 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011430 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011431 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011432 }
11433
11434 return ret;
11435}
11436
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011437/* This produces a 32-bit hash of the concatenation of the first occurrence of
11438 * the Host header followed by the path component if it begins with a slash ('/').
11439 * This means that '*' will not be added, resulting in exactly the first Host
11440 * entry. If no Host header is found, then the path is used. The resulting value
11441 * is hashed using the url hash followed by a full avalanche hash and provides a
11442 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11443 * high-traffic sites without having to store whole paths.
11444 * this differs from the base32 functions in that it includes the url parameters
11445 * as well as the path
11446 */
11447static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011448smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011449{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011450 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011451 struct hdr_ctx ctx;
11452 unsigned int hash = 0;
11453 char *ptr, *beg, *end;
11454 int len;
11455
11456 CHECK_HTTP_MESSAGE_FIRST();
11457
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011458 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011459 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011460 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011461 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11462 ptr = ctx.line + ctx.val;
11463 len = ctx.vlen;
11464 while (len--)
11465 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11466 }
11467
11468 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011469 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 +000011470 beg = http_get_path(txn);
11471 if (!beg)
11472 beg = end;
11473
11474 for (ptr = beg; ptr < end ; ptr++);
11475
11476 if (beg < ptr && *beg == '/') {
11477 while (beg < ptr)
11478 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11479 }
11480 hash = full_hash(hash);
11481
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011482 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011483 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011484 smp->flags = SMP_F_VOL_1ST;
11485 return 1;
11486}
11487
11488/* This concatenates the source address with the 32-bit hash of the Host and
11489 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11490 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11491 * on the source address length. The URL hash is stored before the address so
11492 * that in environments where IPv6 is insignificant, truncating the output to
11493 * 8 bytes would still work.
11494 */
11495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011496smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011497{
11498 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011499 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011500
Dragan Dosendb5af612016-06-16 11:23:01 +020011501 if (!cli_conn)
11502 return 0;
11503
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011504 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011505 return 0;
11506
11507 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011508 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11509 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011510
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011511 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011512 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011513 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011514 temp->len += 4;
11515 break;
11516 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011517 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011518 temp->len += 16;
11519 break;
11520 default:
11521 return 0;
11522 }
11523
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011524 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011525 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011526 return 1;
11527}
11528
Willy Tarreau185b5c42012-04-26 15:11:51 +020011529/* This function is used to validate the arguments passed to any "hdr" fetch
11530 * keyword. These keywords support an optional positive or negative occurrence
11531 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11532 * is assumed that the types are already the correct ones. Returns 0 on error,
11533 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11534 * error message in case of error, that the caller is responsible for freeing.
11535 * The initial location must either be freeable or NULL.
11536 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011537int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011538{
11539 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011540 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011541 return 0;
11542 }
11543 return 1;
11544}
11545
Willy Tarreau276fae92013-07-25 14:36:01 +020011546/* takes an UINT value on input supposed to represent the time since EPOCH,
11547 * adds an optional offset found in args[0] and emits a string representing
11548 * the date in RFC-1123/5322 format.
11549 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011550static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011551{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011552 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011553 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11554 struct chunk *temp;
11555 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011556 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011557 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011558
11559 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011560 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011561 curr_date += args[0].data.sint;
11562
11563 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011564 if (!tm)
11565 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011566
11567 temp = get_trash_chunk();
11568 temp->len = snprintf(temp->str, temp->size - temp->len,
11569 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11570 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11571 tm->tm_hour, tm->tm_min, tm->tm_sec);
11572
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011573 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011574 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011575 return 1;
11576}
11577
Thierry FOURNIERad903512014-04-11 17:51:01 +020011578/* Match language range with language tag. RFC2616 14.4:
11579 *
11580 * A language-range matches a language-tag if it exactly equals
11581 * the tag, or if it exactly equals a prefix of the tag such
11582 * that the first tag character following the prefix is "-".
11583 *
11584 * Return 1 if the strings match, else return 0.
11585 */
11586static inline int language_range_match(const char *range, int range_len,
11587 const char *tag, int tag_len)
11588{
11589 const char *end = range + range_len;
11590 const char *tend = tag + tag_len;
11591 while (range < end) {
11592 if (*range == '-' && tag == tend)
11593 return 1;
11594 if (*range != *tag || tag == tend)
11595 return 0;
11596 range++;
11597 tag++;
11598 }
11599 /* Return true only if the last char of the tag is matched. */
11600 return tag == tend;
11601}
11602
11603/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011604static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011605{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011606 const char *al = smp->data.u.str.str;
11607 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011608 const char *token;
11609 int toklen;
11610 int qvalue;
11611 const char *str;
11612 const char *w;
11613 int best_q = 0;
11614
11615 /* Set the constant to the sample, because the output of the
11616 * function will be peek in the constant configuration string.
11617 */
11618 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011619 smp->data.u.str.size = 0;
11620 smp->data.u.str.str = "";
11621 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011622
11623 /* Parse the accept language */
11624 while (1) {
11625
11626 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011627 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011628 al++;
11629 if (al >= end)
11630 break;
11631
11632 /* Start of the fisrt word. */
11633 token = al;
11634
11635 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011636 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011637 al++;
11638 if (al == token)
11639 goto expect_comma;
11640
11641 /* Length of the token. */
11642 toklen = al - token;
11643 qvalue = 1000;
11644
11645 /* Check if the token exists in the list. If the token not exists,
11646 * jump to the next token.
11647 */
11648 str = args[0].data.str.str;
11649 w = str;
11650 while (1) {
11651 if (*str == ';' || *str == '\0') {
11652 if (language_range_match(token, toklen, w, str-w))
11653 goto look_for_q;
11654 if (*str == '\0')
11655 goto expect_comma;
11656 w = str + 1;
11657 }
11658 str++;
11659 }
11660 goto expect_comma;
11661
11662look_for_q:
11663
11664 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011665 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011666 al++;
11667 if (al >= end)
11668 goto process_value;
11669
11670 /* If ',' is found, process the result */
11671 if (*al == ',')
11672 goto process_value;
11673
11674 /* If the character is different from ';', look
11675 * for the end of the header part in best effort.
11676 */
11677 if (*al != ';')
11678 goto expect_comma;
11679
11680 /* Assumes that the char is ';', now expect "q=". */
11681 al++;
11682
11683 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011684 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011685 al++;
11686 if (al >= end)
11687 goto process_value;
11688
11689 /* Expect 'q'. If no 'q', continue in best effort */
11690 if (*al != 'q')
11691 goto process_value;
11692 al++;
11693
11694 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011695 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011696 al++;
11697 if (al >= end)
11698 goto process_value;
11699
11700 /* Expect '='. If no '=', continue in best effort */
11701 if (*al != '=')
11702 goto process_value;
11703 al++;
11704
11705 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011706 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011707 al++;
11708 if (al >= end)
11709 goto process_value;
11710
11711 /* Parse the q value. */
11712 qvalue = parse_qvalue(al, &al);
11713
11714process_value:
11715
11716 /* If the new q value is the best q value, then store the associated
11717 * language in the response. If qvalue is the biggest value (1000),
11718 * break the process.
11719 */
11720 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011721 smp->data.u.str.str = (char *)w;
11722 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011723 if (qvalue >= 1000)
11724 break;
11725 best_q = qvalue;
11726 }
11727
11728expect_comma:
11729
11730 /* Expect comma or end. If the end is detected, quit the loop. */
11731 while (al < end && *al != ',')
11732 al++;
11733 if (al >= end)
11734 break;
11735
11736 /* Comma is found, jump it and restart the analyzer. */
11737 al++;
11738 }
11739
11740 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011741 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11742 smp->data.u.str.str = args[1].data.str.str;
11743 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011744 }
11745
11746 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011747 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011748}
11749
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011750/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011751static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011752{
11753 /* If the constant flag is set or if not size is avalaible at
11754 * the end of the buffer, copy the string in other buffer
11755 * before decoding.
11756 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011757 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011758 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011759 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11760 smp->data.u.str.str = str->str;
11761 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011762 smp->flags &= ~SMP_F_CONST;
11763 }
11764
11765 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011766 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11767 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011768 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011769}
11770
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011771static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11772{
11773 struct proxy *fe = strm_fe(smp->strm);
11774 int idx, i;
11775 struct cap_hdr *hdr;
11776 int len;
11777
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011778 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011779 return 0;
11780
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011781 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011782
11783 /* Check the availibity of the capture id. */
11784 if (idx > fe->nb_req_cap - 1)
11785 return 0;
11786
11787 /* Look for the original configuration. */
11788 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11789 hdr != NULL && i != idx ;
11790 i--, hdr = hdr->next);
11791 if (!hdr)
11792 return 0;
11793
11794 /* check for the memory allocation */
11795 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011796 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011797 if (smp->strm->req_cap[hdr->index] == NULL)
11798 return 0;
11799
11800 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011801 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011802 if (len > hdr->len)
11803 len = hdr->len;
11804
11805 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011806 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011807 smp->strm->req_cap[idx][len] = '\0';
11808
11809 return 1;
11810}
11811
11812static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11813{
11814 struct proxy *fe = strm_fe(smp->strm);
11815 int idx, i;
11816 struct cap_hdr *hdr;
11817 int len;
11818
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011819 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011820 return 0;
11821
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011822 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011823
11824 /* Check the availibity of the capture id. */
11825 if (idx > fe->nb_rsp_cap - 1)
11826 return 0;
11827
11828 /* Look for the original configuration. */
11829 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11830 hdr != NULL && i != idx ;
11831 i--, hdr = hdr->next);
11832 if (!hdr)
11833 return 0;
11834
11835 /* check for the memory allocation */
11836 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011837 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011838 if (smp->strm->res_cap[hdr->index] == NULL)
11839 return 0;
11840
11841 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011842 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011843 if (len > hdr->len)
11844 len = hdr->len;
11845
11846 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011847 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011848 smp->strm->res_cap[idx][len] = '\0';
11849
11850 return 1;
11851}
11852
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011853/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011854 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011855 * the relevant part of the request line accordingly. Then it updates various
11856 * pointers to the next elements which were moved, and the total buffer length.
11857 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011858 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11859 * error, though this can be revisited when this code is finally exploited.
11860 *
11861 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11862 * query string and 3 to replace uri.
11863 *
11864 * In query string case, the mark question '?' must be set at the start of the
11865 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011866 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011867int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011868 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011869{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011870 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011871 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011872 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011873 int delta;
11874
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011875 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011876 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011877 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011878 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11879
11880 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011881 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011882 txn->req.sl.rq.m_l += delta;
11883 txn->req.sl.rq.u += delta;
11884 txn->req.sl.rq.v += delta;
11885 break;
11886
11887 case 1: // path
11888 cur_ptr = http_get_path(txn);
11889 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011890 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011891
11892 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011893 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 +010011894 cur_end++;
11895
11896 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011897 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011898 txn->req.sl.rq.u_l += delta;
11899 txn->req.sl.rq.v += delta;
11900 break;
11901
11902 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011903 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011904 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011905 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11906 while (cur_ptr < cur_end && *cur_ptr != '?')
11907 cur_ptr++;
11908
11909 /* skip the question mark or indicate that we must insert it
11910 * (but only if the format string is not empty then).
11911 */
11912 if (cur_ptr < cur_end)
11913 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011914 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011915 offset = 0;
11916
11917 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011918 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011919 txn->req.sl.rq.u_l += delta;
11920 txn->req.sl.rq.v += delta;
11921 break;
11922
11923 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011924 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011925 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11926
11927 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011928 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011929 txn->req.sl.rq.u_l += delta;
11930 txn->req.sl.rq.v += delta;
11931 break;
11932
11933 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011934 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011935 }
11936
11937 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011938 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011939 txn->req.sl.rq.l += delta;
11940 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011941 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011942 return 0;
11943}
11944
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011945/* This function replace the HTTP status code and the associated message. The
11946 * variable <status> contains the new status code. This function never fails.
11947 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011948void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011949{
11950 struct http_txn *txn = s->txn;
11951 char *cur_ptr, *cur_end;
11952 int delta;
11953 char *res;
11954 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011955 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011956 int msg_len;
11957
11958 chunk_reset(&trash);
11959
11960 res = ultoa_o(status, trash.str, trash.size);
11961 c_l = res - trash.str;
11962
11963 trash.str[c_l] = ' ';
11964 trash.len = c_l + 1;
11965
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011966 /* Do we have a custom reason format string? */
11967 if (msg == NULL)
11968 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011969 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011970 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11971 trash.len += msg_len;
11972
11973 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11974 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11975
11976 /* commit changes and adjust message */
11977 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11978
11979 /* adjust res line offsets and lengths */
11980 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11981 txn->rsp.sl.st.c_l = c_l;
11982 txn->rsp.sl.st.r_l = msg_len;
11983
11984 delta = trash.len - (cur_end - cur_ptr);
11985 txn->rsp.sl.st.l += delta;
11986 txn->hdr_idx.v[0].len += delta;
11987 http_msg_move_end(&txn->rsp, delta);
11988}
11989
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011990/* This function executes one of the set-{method,path,query,uri} actions. It
11991 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011992 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011993 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011994 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11995 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011996 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011997enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011998 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011999{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012000 struct chunk *replace;
12001 enum act_return ret = ACT_RET_ERR;
12002
12003 replace = alloc_trash_chunk();
12004 if (!replace)
12005 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012006
12007 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012008 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012009 replace->str[replace->len++] = '?';
12010 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
12011 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012012
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012013 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
12014
12015 ret = ACT_RET_CONT;
12016
12017leave:
12018 free_trash_chunk(replace);
12019 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012020}
12021
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012022/* This function is just a compliant action wrapper for "set-status". */
12023enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012024 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012025{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012026 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012027 return ACT_RET_CONT;
12028}
12029
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012030/* parse an http-request action among :
12031 * set-method
12032 * set-path
12033 * set-query
12034 * set-uri
12035 *
12036 * All of them accept a single argument of type string representing a log-format.
12037 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12038 * 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 +020012039 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012040 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012041enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12042 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012043{
12044 int cur_arg = *orig_arg;
12045
Thierry FOURNIER42148732015-09-02 17:17:33 +020012046 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012047
12048 switch (args[0][4]) {
12049 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012050 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012051 rule->action_ptr = http_action_set_req_line;
12052 break;
12053 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012054 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012055 rule->action_ptr = http_action_set_req_line;
12056 break;
12057 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012058 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012059 rule->action_ptr = http_action_set_req_line;
12060 break;
12061 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012062 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012063 rule->action_ptr = http_action_set_req_line;
12064 break;
12065 default:
12066 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012067 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012068 }
12069
12070 if (!*args[cur_arg] ||
12071 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12072 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012073 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012074 }
12075
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012076 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012077 px->conf.args.ctx = ARGC_HRQ;
12078 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12079 (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 +010012080 return ACT_RET_PRS_ERR;
12081 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012082
12083 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012084 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012085}
12086
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012087/* parse set-status action:
12088 * This action accepts a single argument of type int representing
12089 * an http status code. It returns ACT_RET_PRS_OK on success,
12090 * ACT_RET_PRS_ERR on error.
12091 */
12092enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12093 struct act_rule *rule, char **err)
12094{
12095 char *error;
12096
Thierry FOURNIER42148732015-09-02 17:17:33 +020012097 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012098 rule->action_ptr = action_http_set_status;
12099
12100 /* Check if an argument is available */
12101 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012102 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012103 return ACT_RET_PRS_ERR;
12104 }
12105
12106 /* convert status code as integer */
12107 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12108 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12109 memprintf(err, "expects an integer status code between 100 and 999");
12110 return ACT_RET_PRS_ERR;
12111 }
12112
12113 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012114
12115 /* set custom reason string */
12116 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12117 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12118 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12119 (*orig_arg)++;
12120 rule->arg.status.reason = strdup(args[*orig_arg]);
12121 (*orig_arg)++;
12122 }
12123
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012124 return ACT_RET_PRS_OK;
12125}
12126
Willy Tarreau53275e82017-11-24 07:52:01 +010012127/* This function executes the "reject" HTTP action. It clears the request and
12128 * response buffer without sending any response. It can be useful as an HTTP
12129 * alternative to the silent-drop action to defend against DoS attacks, and may
12130 * also be used with HTTP/2 to close a connection instead of just a stream.
12131 * The txn status is unchanged, indicating no response was sent. The termination
12132 * flags will indicate "PR". It always returns ACT_RET_STOP.
12133 */
12134enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12135 struct session *sess, struct stream *s, int flags)
12136{
12137 channel_abort(&s->req);
12138 channel_abort(&s->res);
12139 s->req.analysers = 0;
12140 s->res.analysers = 0;
12141
12142 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12143 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12144 if (sess->listener && sess->listener->counters)
12145 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12146
12147 if (!(s->flags & SF_ERR_MASK))
12148 s->flags |= SF_ERR_PRXCOND;
12149 if (!(s->flags & SF_FINST_MASK))
12150 s->flags |= SF_FINST_R;
12151
12152 return ACT_RET_CONT;
12153}
12154
12155/* parse the "reject" action:
12156 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12157 * ACT_RET_PRS_ERR on error.
12158 */
12159enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12160 struct act_rule *rule, char **err)
12161{
12162 rule->action = ACT_CUSTOM;
12163 rule->action_ptr = http_action_reject;
12164 return ACT_RET_PRS_OK;
12165}
12166
Willy Tarreaua9083d02015-05-08 15:27:59 +020012167/* This function executes the "capture" action. It executes a fetch expression,
12168 * turns the result into a string and puts it in a capture slot. It always
12169 * returns 1. If an error occurs the action is cancelled, but the rule
12170 * processing continues.
12171 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012172enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012173 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012174{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012175 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012176 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012177 char **cap = s->req_cap;
12178 int len;
12179
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012180 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 +020012181 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012182 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012183
12184 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012185 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012186
12187 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012188 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012189
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012190 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012191 if (len > h->len)
12192 len = h->len;
12193
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012194 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012195 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012196 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012197}
12198
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012199/* This function executes the "capture" action and store the result in a
12200 * capture slot if exists. It executes a fetch expression, turns the result
12201 * into a string and puts it in a capture slot. It always returns 1. If an
12202 * error occurs the action is cancelled, but the rule processing continues.
12203 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012204enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012205 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012206{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012207 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012208 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012209 char **cap = s->req_cap;
12210 struct proxy *fe = strm_fe(s);
12211 int len;
12212 int i;
12213
12214 /* Look for the original configuration. */
12215 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012216 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012217 i--, h = h->next);
12218 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012219 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012220
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012221 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 +020012222 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012223 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012224
12225 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012226 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012227
12228 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012229 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012230
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012231 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012232 if (len > h->len)
12233 len = h->len;
12234
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012235 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012236 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012237 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012238}
12239
Christopher Faulet29730ba2017-09-18 15:26:32 +020012240/* Check an "http-request capture" action.
12241 *
12242 * The function returns 1 in success case, otherwise, it returns 0 and err is
12243 * filled.
12244 */
12245int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12246{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012247 if (rule->action_ptr != http_action_req_capture_by_id)
12248 return 1;
12249
Christopher Faulet29730ba2017-09-18 15:26:32 +020012250 if (rule->arg.capid.idx >= px->nb_req_cap) {
12251 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12252 rule->arg.capid.idx);
12253 return 0;
12254 }
12255
12256 return 1;
12257}
12258
Willy Tarreaua9083d02015-05-08 15:27:59 +020012259/* parse an "http-request capture" action. It takes a single argument which is
12260 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012261 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012262 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012263 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012264enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12265 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012266{
12267 struct sample_expr *expr;
12268 struct cap_hdr *hdr;
12269 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012270 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012271
12272 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12273 if (strcmp(args[cur_arg], "if") == 0 ||
12274 strcmp(args[cur_arg], "unless") == 0)
12275 break;
12276
12277 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012278 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012279 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012280 }
12281
Willy Tarreaua9083d02015-05-08 15:27:59 +020012282 cur_arg = *orig_arg;
12283 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12284 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012285 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012286
12287 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12288 memprintf(err,
12289 "fetch method '%s' extracts information from '%s', none of which is available here",
12290 args[cur_arg-1], sample_src_names(expr->fetch->use));
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
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012295 if (!args[cur_arg] || !*args[cur_arg]) {
12296 memprintf(err, "expects 'len or 'id'");
12297 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012298 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012299 }
12300
Willy Tarreaua9083d02015-05-08 15:27:59 +020012301 if (strcmp(args[cur_arg], "len") == 0) {
12302 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303
12304 if (!(px->cap & PR_CAP_FE)) {
12305 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012306 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012307 }
12308
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012309 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012310
Willy Tarreaua9083d02015-05-08 15:27:59 +020012311 if (!args[cur_arg]) {
12312 memprintf(err, "missing length value");
12313 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012314 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012315 }
12316 /* we copy the table name for now, it will be resolved later */
12317 len = atoi(args[cur_arg]);
12318 if (len <= 0) {
12319 memprintf(err, "length must be > 0");
12320 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012321 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012322 }
12323 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012324
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012325 if (!len) {
12326 memprintf(err, "a positive 'len' argument is mandatory");
12327 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012328 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012329 }
12330
Vincent Bernat02779b62016-04-03 13:48:43 +020012331 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012332 hdr->next = px->req_cap;
12333 hdr->name = NULL; /* not a header capture */
12334 hdr->namelen = 0;
12335 hdr->len = len;
12336 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12337 hdr->index = px->nb_req_cap++;
12338
12339 px->req_cap = hdr;
12340 px->to_log |= LW_REQHDR;
12341
Thierry FOURNIER42148732015-09-02 17:17:33 +020012342 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012343 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012344 rule->arg.cap.expr = expr;
12345 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012346 }
12347
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012348 else if (strcmp(args[cur_arg], "id") == 0) {
12349 int id;
12350 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012351
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012352 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012353
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012354 if (!args[cur_arg]) {
12355 memprintf(err, "missing id value");
12356 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012357 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012358 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012359
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012360 id = strtol(args[cur_arg], &error, 10);
12361 if (*error != '\0') {
12362 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12363 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012364 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012365 }
12366 cur_arg++;
12367
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012368 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012369
Thierry FOURNIER42148732015-09-02 17:17:33 +020012370 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012371 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012372 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012373 rule->arg.capid.expr = expr;
12374 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012375 }
12376
12377 else {
12378 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12379 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012380 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012381 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012382
12383 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012384 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012385}
12386
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012387/* This function executes the "capture" action and store the result in a
12388 * capture slot if exists. It executes a fetch expression, turns the result
12389 * into a string and puts it in a capture slot. It always returns 1. If an
12390 * error occurs the action is cancelled, but the rule processing continues.
12391 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012392enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012393 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012394{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012395 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012396 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012397 char **cap = s->res_cap;
12398 struct proxy *fe = strm_fe(s);
12399 int len;
12400 int i;
12401
12402 /* Look for the original configuration. */
12403 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012404 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012405 i--, h = h->next);
12406 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012407 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012408
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012409 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 +020012410 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012411 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012412
12413 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012414 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012415
12416 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012417 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012418
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012419 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012420 if (len > h->len)
12421 len = h->len;
12422
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012423 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012424 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012425 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012426}
12427
Christopher Faulet29730ba2017-09-18 15:26:32 +020012428/* Check an "http-response capture" action.
12429 *
12430 * The function returns 1 in success case, otherwise, it returns 0 and err is
12431 * filled.
12432 */
12433int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12434{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012435 if (rule->action_ptr != http_action_res_capture_by_id)
12436 return 1;
12437
Christopher Faulet29730ba2017-09-18 15:26:32 +020012438 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12439 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12440 rule->arg.capid.idx);
12441 return 0;
12442 }
12443
12444 return 1;
12445}
12446
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012447/* parse an "http-response capture" action. It takes a single argument which is
12448 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12449 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012450 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012451 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012452enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12453 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012454{
12455 struct sample_expr *expr;
12456 int cur_arg;
12457 int id;
12458 char *error;
12459
12460 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12461 if (strcmp(args[cur_arg], "if") == 0 ||
12462 strcmp(args[cur_arg], "unless") == 0)
12463 break;
12464
12465 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012466 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012467 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012468 }
12469
12470 cur_arg = *orig_arg;
12471 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12472 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012473 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012474
12475 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12476 memprintf(err,
12477 "fetch method '%s' extracts information from '%s', none of which is available here",
12478 args[cur_arg-1], sample_src_names(expr->fetch->use));
12479 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012480 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012481 }
12482
12483 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012484 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012485 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012486 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012487 }
12488
12489 if (strcmp(args[cur_arg], "id") != 0) {
12490 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12491 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012492 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012493 }
12494
12495 cur_arg++;
12496
12497 if (!args[cur_arg]) {
12498 memprintf(err, "missing id value");
12499 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012500 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012501 }
12502
12503 id = strtol(args[cur_arg], &error, 10);
12504 if (*error != '\0') {
12505 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12506 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012507 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012508 }
12509 cur_arg++;
12510
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012511 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012512
Thierry FOURNIER42148732015-09-02 17:17:33 +020012513 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012514 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012515 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012516 rule->arg.capid.expr = expr;
12517 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012518
12519 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012520 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012521}
12522
William Lallemand73025dd2014-04-24 14:38:37 +020012523/*
12524 * Return the struct http_req_action_kw associated to a keyword.
12525 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012526struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012527{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012528 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012529}
12530
12531/*
12532 * Return the struct http_res_action_kw associated to a keyword.
12533 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012534struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012535{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012536 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012537}
12538
Willy Tarreau12207b32016-11-22 19:48:51 +010012539
12540/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12541 * now.
12542 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012543static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau12207b32016-11-22 19:48:51 +010012544{
12545 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12546 return 1;
12547
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012548 if (*args[2]) {
12549 struct proxy *px;
12550
12551 px = proxy_find_by_name(args[2], 0, 0);
12552 if (px)
12553 appctx->ctx.errors.iid = px->uuid;
12554 else
12555 appctx->ctx.errors.iid = atoi(args[2]);
12556
12557 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012558 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012559 appctx->ctx.cli.msg = "No such proxy.\n";
12560 appctx->st0 = CLI_ST_PRINT;
12561 return 1;
12562 }
12563 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012564 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012565 appctx->ctx.errors.iid = -1; // dump all proxies
12566
Willy Tarreau35069f82016-11-25 09:16:37 +010012567 appctx->ctx.errors.flag = 0;
12568 if (strcmp(args[3], "request") == 0)
12569 appctx->ctx.errors.flag |= 4; // ignore response
12570 else if (strcmp(args[3], "response") == 0)
12571 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012572 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012573 return 0;
12574}
12575
12576/* This function dumps all captured errors onto the stream interface's
12577 * read buffer. It returns 0 if the output buffer is full and it needs
12578 * to be called again, otherwise non-zero.
12579 */
12580static int cli_io_handler_show_errors(struct appctx *appctx)
12581{
12582 struct stream_interface *si = appctx->owner;
12583 extern const char *monthname[12];
12584
12585 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12586 return 1;
12587
12588 chunk_reset(&trash);
12589
12590 if (!appctx->ctx.errors.px) {
12591 /* the function had not been called yet, let's prepare the
12592 * buffer for a response.
12593 */
12594 struct tm tm;
12595
12596 get_localtime(date.tv_sec, &tm);
12597 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12598 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12599 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12600 error_snapshot_id);
12601
Willy Tarreau06d80a92017-10-19 14:32:15 +020012602 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012603 /* Socket buffer full. Let's try again later from the same point */
12604 si_applet_cant_put(si);
12605 return 0;
12606 }
12607
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012608 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012609 appctx->ctx.errors.bol = 0;
12610 appctx->ctx.errors.ptr = -1;
12611 }
12612
12613 /* we have two inner loops here, one for the proxy, the other one for
12614 * the buffer.
12615 */
12616 while (appctx->ctx.errors.px) {
12617 struct error_snapshot *es;
12618
Willy Tarreau35069f82016-11-25 09:16:37 +010012619 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012620 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012621 if (appctx->ctx.errors.flag & 2) // skip req
12622 goto next;
12623 }
12624 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012625 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012626 if (appctx->ctx.errors.flag & 4) // skip resp
12627 goto next;
12628 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012629
12630 if (!es->when.tv_sec)
12631 goto next;
12632
12633 if (appctx->ctx.errors.iid >= 0 &&
12634 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12635 es->oe->uuid != appctx->ctx.errors.iid)
12636 goto next;
12637
12638 if (appctx->ctx.errors.ptr < 0) {
12639 /* just print headers now */
12640
12641 char pn[INET6_ADDRSTRLEN];
12642 struct tm tm;
12643 int port;
12644
12645 get_localtime(es->when.tv_sec, &tm);
12646 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12647 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12648 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12649
12650 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12651 case AF_INET:
12652 case AF_INET6:
12653 port = get_host_port(&es->src);
12654 break;
12655 default:
12656 port = 0;
12657 }
12658
Willy Tarreau35069f82016-11-25 09:16:37 +010012659 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012660 case 0:
12661 chunk_appendf(&trash,
12662 " frontend %s (#%d): invalid request\n"
12663 " backend %s (#%d)",
12664 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12665 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12666 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12667 break;
12668 case 1:
12669 chunk_appendf(&trash,
12670 " backend %s (#%d): invalid response\n"
12671 " frontend %s (#%d)",
12672 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12673 es->oe->id, es->oe->uuid);
12674 break;
12675 }
12676
12677 chunk_appendf(&trash,
12678 ", server %s (#%d), event #%u\n"
12679 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012680 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012681 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12682 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12683 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12684 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12685 es->ev_id,
12686 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012687 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012688 es->m_clen, es->m_blen,
12689 es->b_flags, es->b_out, es->b_tot,
12690 es->len, es->b_wrap, es->pos);
12691
Willy Tarreau06d80a92017-10-19 14:32:15 +020012692 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012693 /* Socket buffer full. Let's try again later from the same point */
12694 si_applet_cant_put(si);
12695 return 0;
12696 }
12697 appctx->ctx.errors.ptr = 0;
12698 appctx->ctx.errors.sid = es->sid;
12699 }
12700
12701 if (appctx->ctx.errors.sid != es->sid) {
12702 /* the snapshot changed while we were dumping it */
12703 chunk_appendf(&trash,
12704 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012705 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012706 si_applet_cant_put(si);
12707 return 0;
12708 }
12709 goto next;
12710 }
12711
12712 /* OK, ptr >= 0, so we have to dump the current line */
12713 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12714 int newptr;
12715 int newline;
12716
12717 newline = appctx->ctx.errors.bol;
12718 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12719 if (newptr == appctx->ctx.errors.ptr)
12720 return 0;
12721
Willy Tarreau06d80a92017-10-19 14:32:15 +020012722 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012723 /* Socket buffer full. Let's try again later from the same point */
12724 si_applet_cant_put(si);
12725 return 0;
12726 }
12727 appctx->ctx.errors.ptr = newptr;
12728 appctx->ctx.errors.bol = newline;
12729 };
12730 next:
12731 appctx->ctx.errors.bol = 0;
12732 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012733 appctx->ctx.errors.flag ^= 1;
12734 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012735 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012736 }
12737
12738 /* dump complete */
12739 return 1;
12740}
12741
12742/* register cli keywords */
12743static struct cli_kw_list cli_kws = {{ },{
12744 { { "show", "errors", NULL },
12745 "show errors : report last request and response errors for each proxy",
12746 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12747 },
12748 {{},}
12749}};
12750
Willy Tarreau4a568972010-05-12 08:08:50 +020012751/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012752/* All supported ACL keywords must be declared here. */
12753/************************************************************************/
12754
12755/* Note: must not be declared <const> as its list will be overwritten.
12756 * Please take care of keeping this list alphabetically sorted.
12757 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012758static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012759 { "base", "base", PAT_MATCH_STR },
12760 { "base_beg", "base", PAT_MATCH_BEG },
12761 { "base_dir", "base", PAT_MATCH_DIR },
12762 { "base_dom", "base", PAT_MATCH_DOM },
12763 { "base_end", "base", PAT_MATCH_END },
12764 { "base_len", "base", PAT_MATCH_LEN },
12765 { "base_reg", "base", PAT_MATCH_REG },
12766 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012767
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012768 { "cook", "req.cook", PAT_MATCH_STR },
12769 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12770 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12771 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12772 { "cook_end", "req.cook", PAT_MATCH_END },
12773 { "cook_len", "req.cook", PAT_MATCH_LEN },
12774 { "cook_reg", "req.cook", PAT_MATCH_REG },
12775 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012776
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012777 { "hdr", "req.hdr", PAT_MATCH_STR },
12778 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12779 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12780 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12781 { "hdr_end", "req.hdr", PAT_MATCH_END },
12782 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12783 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12784 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012785
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012786 /* these two declarations uses strings with list storage (in place
12787 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12788 * and delete functions are relative to the list management. The parse
12789 * and match method are related to the corresponding fetch methods. This
12790 * is very particular ACL declaration mode.
12791 */
12792 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12793 { "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 +020012794
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012795 { "path", "path", PAT_MATCH_STR },
12796 { "path_beg", "path", PAT_MATCH_BEG },
12797 { "path_dir", "path", PAT_MATCH_DIR },
12798 { "path_dom", "path", PAT_MATCH_DOM },
12799 { "path_end", "path", PAT_MATCH_END },
12800 { "path_len", "path", PAT_MATCH_LEN },
12801 { "path_reg", "path", PAT_MATCH_REG },
12802 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012803
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012804 { "req_ver", "req.ver", PAT_MATCH_STR },
12805 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012806
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012807 { "scook", "res.cook", PAT_MATCH_STR },
12808 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12809 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12810 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12811 { "scook_end", "res.cook", PAT_MATCH_END },
12812 { "scook_len", "res.cook", PAT_MATCH_LEN },
12813 { "scook_reg", "res.cook", PAT_MATCH_REG },
12814 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012815
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012816 { "shdr", "res.hdr", PAT_MATCH_STR },
12817 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12818 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12819 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12820 { "shdr_end", "res.hdr", PAT_MATCH_END },
12821 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12822 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12823 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012824
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012825 { "url", "url", PAT_MATCH_STR },
12826 { "url_beg", "url", PAT_MATCH_BEG },
12827 { "url_dir", "url", PAT_MATCH_DIR },
12828 { "url_dom", "url", PAT_MATCH_DOM },
12829 { "url_end", "url", PAT_MATCH_END },
12830 { "url_len", "url", PAT_MATCH_LEN },
12831 { "url_reg", "url", PAT_MATCH_REG },
12832 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012833
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012834 { "urlp", "urlp", PAT_MATCH_STR },
12835 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12836 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12837 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12838 { "urlp_end", "urlp", PAT_MATCH_END },
12839 { "urlp_len", "urlp", PAT_MATCH_LEN },
12840 { "urlp_reg", "urlp", PAT_MATCH_REG },
12841 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012842
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012843 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012844}};
12845
12846/************************************************************************/
12847/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012848/************************************************************************/
12849/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012850static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012851 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012852 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012853 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12854
Willy Tarreau87b09662015-04-03 00:22:06 +020012855 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012856 { "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 +020012857
12858 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012859 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12860 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12861 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012862
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012863 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12864 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012865
Willy Tarreau409bcde2013-01-08 00:31:00 +010012866 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12867 * are only here to match the ACL's name, are request-only and are used
12868 * for ACL compatibility only.
12869 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012870 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12871 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012872 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12873 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012874
12875 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12876 * only here to match the ACL's name, are request-only and are used for
12877 * ACL compatibility only.
12878 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012879 { "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 +020012880 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012881 { "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 +020012882 { "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 +010012883
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012884 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012885 { "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 +010012886 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012887 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012888 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012889 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012890
12891 /* HTTP protocol on the request path */
12892 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012893 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012894
12895 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012896 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12897 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012898
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012899 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012900 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12901 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012902 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012903
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012904 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012905 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12906
Willy Tarreau18ed2562013-01-14 15:56:36 +010012907 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012908 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12909 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012910
Willy Tarreau18ed2562013-01-14 15:56:36 +010012911 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012912 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012913 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12914 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012915
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012916 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012917 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012918 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012919 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012920 { "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 +010012921 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012922 { "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 +010012923
12924 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012925 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012926 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12927 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012928
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012929 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012930 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012931 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012932 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012933 { "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 +010012934 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012935 { "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 +010012936
Willy Tarreau409bcde2013-01-08 00:31:00 +010012937 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012938 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012939 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12940 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012941 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012942
12943 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012944 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012945 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012946 { "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 +020012947 { "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 +010012948
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012949 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012950 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012951 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012952 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012953 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012954 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012955 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012956 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12957 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012958 { "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 +010012959 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012960}};
12961
Willy Tarreau8797c062007-05-07 00:55:35 +020012962
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012963/************************************************************************/
12964/* All supported converter keywords must be declared here. */
12965/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012966/* Note: must not be declared <const> as its list will be overwritten */
12967static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012968 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012969 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012970 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12971 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012972 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012973 { NULL, NULL, 0, 0, 0 },
12974}};
12975
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012976
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012977/************************************************************************/
12978/* All supported http-request action keywords must be declared here. */
12979/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012980struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012981 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012982 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012983 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012984 { "set-method", parse_set_req_line },
12985 { "set-path", parse_set_req_line },
12986 { "set-query", parse_set_req_line },
12987 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012988 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012989 }
12990};
12991
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012992struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012993 .kw = {
12994 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012995 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012996 { NULL, NULL }
12997 }
12998};
12999
Willy Tarreau8797c062007-05-07 00:55:35 +020013000__attribute__((constructor))
13001static void __http_protocol_init(void)
13002{
13003 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013004 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013005 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013006 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013007 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013008 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013009}
13010
13011
Willy Tarreau58f10d72006-12-04 02:26:12 +010013012/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013013 * Local variables:
13014 * c-indent-level: 8
13015 * c-basic-offset: 8
13016 * End:
13017 */