blob: 65f0636e8ac661b66cc8607f8fbbc22a5642e1ad [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>
Patrick Hemmere3faf022018-08-22 10:02:00 -040065#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010066#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020067#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020070#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020071#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010072#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020073#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010074#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020076#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020077#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Willy Tarreau522d6c02009-12-06 18:49:18 +010079const char HTTP_100[] =
80 "HTTP/1.1 100 Continue\r\n\r\n";
81
Willy Tarreau83061a82018-07-13 11:56:34 +020082const struct buffer http_100_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020083 .area = (char *)&HTTP_100,
84 .data = sizeof(HTTP_100)-1
Willy Tarreau522d6c02009-12-06 18:49:18 +010085};
86
Willy Tarreaua9679ac2010-01-03 17:32:57 +010087/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020091 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreau0f772532006-12-23 20:51:41 +010093const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010096 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010097 "Location: "; /* not terminated since it will be concatenated with the URL */
98
99/* same as 302 except that the browser MUST retry with the GET method */
100const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100101 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100102 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100103 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100104 "Location: "; /* not terminated since it will be concatenated with the URL */
105
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400106
107/* same as 302 except that the browser MUST retry with the same method */
108const char *HTTP_307 =
109 "HTTP/1.1 307 Temporary Redirect\r\n"
110 "Cache-Control: no-cache\r\n"
111 "Content-length: 0\r\n"
112 "Location: "; /* not terminated since it will be concatenated with the URL */
113
114/* same as 301 except that the browser MUST retry with the same method */
115const char *HTTP_308 =
116 "HTTP/1.1 308 Permanent Redirect\r\n"
117 "Content-length: 0\r\n"
118 "Location: "; /* not terminated since it will be concatenated with the URL */
119
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
121const char *HTTP_401_fmt =
122 "HTTP/1.0 401 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200125 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
128 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
129
Willy Tarreau844a7e72010-01-31 21:46:18 +0100130const char *HTTP_407_fmt =
131 "HTTP/1.0 407 Unauthorized\r\n"
132 "Cache-Control: no-cache\r\n"
133 "Connection: close\r\n"
134 "Content-Type: text/html\r\n"
135 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
136 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800137 "<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 +0100138
Willy Tarreau0f772532006-12-23 20:51:41 +0100139
140const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100142 [HTTP_ERR_400] = 400,
143 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400144 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100145 [HTTP_ERR_408] = 408,
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200146 [HTTP_ERR_421] = 421,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200147 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400148 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_500] = 500,
150 [HTTP_ERR_502] = 502,
151 [HTTP_ERR_503] = 503,
152 [HTTP_ERR_504] = 504,
153};
154
Willy Tarreau80587432006-12-24 17:47:20 +0100155static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200156 [HTTP_ERR_200] =
157 "HTTP/1.0 200 OK\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
163
Willy Tarreau0f772532006-12-23 20:51:41 +0100164 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100165 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
171
172 [HTTP_ERR_403] =
173 "HTTP/1.0 403 Forbidden\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
179
CJ Ess108b1dd2015-04-07 12:03:37 -0400180 [HTTP_ERR_405] =
181 "HTTP/1.0 405 Method Not Allowed\r\n"
182 "Cache-Control: no-cache\r\n"
183 "Connection: close\r\n"
184 "Content-Type: text/html\r\n"
185 "\r\n"
186 "<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",
187
Willy Tarreau0f772532006-12-23 20:51:41 +0100188 [HTTP_ERR_408] =
189 "HTTP/1.0 408 Request Time-out\r\n"
190 "Cache-Control: no-cache\r\n"
191 "Connection: close\r\n"
192 "Content-Type: text/html\r\n"
193 "\r\n"
194 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
195
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200196 [HTTP_ERR_421] =
197 "HTTP/1.0 421 Misdirected Request\r\n"
198 "Cache-Control: no-cache\r\n"
199 "Connection: close\r\n"
200 "Content-Type: text/html\r\n"
201 "\r\n"
202 "<html><body><h1>421 Misdirected Request</h1>\nRequest sent to a non-authoritative server.\n</body></html>\n",
203
Olivier Houchard51a76d82017-10-02 16:12:07 +0200204 [HTTP_ERR_425] =
205 "HTTP/1.0 425 Too Early\r\n"
206 "Cache-Control: no-cache\r\n"
207 "Connection: close\r\n"
208 "Content-Type: text/html\r\n"
209 "\r\n"
210 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
211
CJ Ess108b1dd2015-04-07 12:03:37 -0400212 [HTTP_ERR_429] =
213 "HTTP/1.0 429 Too Many Requests\r\n"
214 "Cache-Control: no-cache\r\n"
215 "Connection: close\r\n"
216 "Content-Type: text/html\r\n"
217 "\r\n"
218 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
219
Willy Tarreau0f772532006-12-23 20:51:41 +0100220 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200221 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100222 "Cache-Control: no-cache\r\n"
223 "Connection: close\r\n"
224 "Content-Type: text/html\r\n"
225 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200226 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100227
228 [HTTP_ERR_502] =
229 "HTTP/1.0 502 Bad Gateway\r\n"
230 "Cache-Control: no-cache\r\n"
231 "Connection: close\r\n"
232 "Content-Type: text/html\r\n"
233 "\r\n"
234 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
235
236 [HTTP_ERR_503] =
237 "HTTP/1.0 503 Service Unavailable\r\n"
238 "Cache-Control: no-cache\r\n"
239 "Connection: close\r\n"
240 "Content-Type: text/html\r\n"
241 "\r\n"
242 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
243
244 [HTTP_ERR_504] =
245 "HTTP/1.0 504 Gateway Time-out\r\n"
246 "Cache-Control: no-cache\r\n"
247 "Connection: close\r\n"
248 "Content-Type: text/html\r\n"
249 "\r\n"
250 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
251
252};
253
Cyril Bonté19979e12012-04-04 12:57:21 +0200254/* status codes available for the stats admin page (strictly 4 chars length) */
255const char *stat_status_codes[STAT_STATUS_SIZE] = {
256 [STAT_STATUS_DENY] = "DENY",
257 [STAT_STATUS_DONE] = "DONE",
258 [STAT_STATUS_ERRP] = "ERRP",
259 [STAT_STATUS_EXCD] = "EXCD",
260 [STAT_STATUS_NONE] = "NONE",
261 [STAT_STATUS_PART] = "PART",
262 [STAT_STATUS_UNKN] = "UNKN",
263};
264
265
William Lallemand73025dd2014-04-24 14:38:37 +0200266/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200267struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200268 .list = LIST_HEAD_INIT(http_req_keywords.list)
269};
270
271/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200272struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200273 .list = LIST_HEAD_INIT(http_res_keywords.list)
274};
275
Willy Tarreau80587432006-12-24 17:47:20 +0100276/* We must put the messages here since GCC cannot initialize consts depending
277 * on strlen().
278 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200279struct buffer http_err_chunks[HTTP_ERR_SIZE];
Willy Tarreau80587432006-12-24 17:47:20 +0100280
Willy Tarreaua890d072013-04-02 12:01:06 +0200281/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100282static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200283
Willy Tarreau42250582007-04-01 01:30:43 +0200284#define FD_SETS_ARE_BITFIELDS
285#ifdef FD_SETS_ARE_BITFIELDS
286/*
287 * This map is used with all the FD_* macros to check whether a particular bit
288 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
289 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
290 * byte should be encoded. Be careful to always pass bytes from 0 to 255
291 * exclusively to the macros.
292 */
293fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
294fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100295fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200296
297#else
298#error "Check if your OS uses bitfields for fd_sets"
299#endif
300
Willy Tarreau87b09662015-04-03 00:22:06 +0200301static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200302
David Carlier7365f7d2016-04-04 11:54:42 +0100303static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
304static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100305
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200306/* This function returns a reason associated with the HTTP status.
307 * This function never fails, a message is always returned.
308 */
309const char *get_reason(unsigned int status)
310{
311 switch (status) {
312 case 100: return "Continue";
313 case 101: return "Switching Protocols";
314 case 102: return "Processing";
315 case 200: return "OK";
316 case 201: return "Created";
317 case 202: return "Accepted";
318 case 203: return "Non-Authoritative Information";
319 case 204: return "No Content";
320 case 205: return "Reset Content";
321 case 206: return "Partial Content";
322 case 207: return "Multi-Status";
323 case 210: return "Content Different";
324 case 226: return "IM Used";
325 case 300: return "Multiple Choices";
326 case 301: return "Moved Permanently";
327 case 302: return "Moved Temporarily";
328 case 303: return "See Other";
329 case 304: return "Not Modified";
330 case 305: return "Use Proxy";
331 case 307: return "Temporary Redirect";
332 case 308: return "Permanent Redirect";
333 case 310: return "Too many Redirects";
334 case 400: return "Bad Request";
335 case 401: return "Unauthorized";
336 case 402: return "Payment Required";
337 case 403: return "Forbidden";
338 case 404: return "Not Found";
339 case 405: return "Method Not Allowed";
340 case 406: return "Not Acceptable";
341 case 407: return "Proxy Authentication Required";
342 case 408: return "Request Time-out";
343 case 409: return "Conflict";
344 case 410: return "Gone";
345 case 411: return "Length Required";
346 case 412: return "Precondition Failed";
347 case 413: return "Request Entity Too Large";
348 case 414: return "Request-URI Too Long";
349 case 415: return "Unsupported Media Type";
350 case 416: return "Requested range unsatisfiable";
351 case 417: return "Expectation failed";
352 case 418: return "I'm a teapot";
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200353 case 421: return "Misdirected Request";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200354 case 422: return "Unprocessable entity";
355 case 423: return "Locked";
356 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200357 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200358 case 426: return "Upgrade Required";
359 case 428: return "Precondition Required";
360 case 429: return "Too Many Requests";
361 case 431: return "Request Header Fields Too Large";
362 case 449: return "Retry With";
363 case 450: return "Blocked by Windows Parental Controls";
364 case 451: return "Unavailable For Legal Reasons";
365 case 456: return "Unrecoverable Error";
366 case 499: return "client has closed connection";
367 case 500: return "Internal Server Error";
368 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200369 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200370 case 503: return "Service Unavailable";
371 case 504: return "Gateway Time-out";
372 case 505: return "HTTP Version not supported";
373 case 506: return "Variant also negociate";
374 case 507: return "Insufficient storage";
375 case 508: return "Loop detected";
376 case 509: return "Bandwidth Limit Exceeded";
377 case 510: return "Not extended";
378 case 511: return "Network authentication required";
379 case 520: return "Web server is returning an unknown error";
380 default:
381 switch (status) {
382 case 100 ... 199: return "Informational";
383 case 200 ... 299: return "Success";
384 case 300 ... 399: return "Redirection";
385 case 400 ... 499: return "Client Error";
386 case 500 ... 599: return "Server Error";
387 default: return "Other";
388 }
389 }
390}
391
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200392/* This function returns HTTP_ERR_<num> (enum) matching http status code.
393 * Returned value should match codes from http_err_codes.
394 */
395static const int http_get_status_idx(unsigned int status)
396{
397 switch (status) {
398 case 200: return HTTP_ERR_200;
399 case 400: return HTTP_ERR_400;
400 case 403: return HTTP_ERR_403;
401 case 405: return HTTP_ERR_405;
402 case 408: return HTTP_ERR_408;
Tim Duesterhuse2b10bf2018-04-27 21:18:46 +0200403 case 421: return HTTP_ERR_421;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200404 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200405 case 429: return HTTP_ERR_429;
406 case 500: return HTTP_ERR_500;
407 case 502: return HTTP_ERR_502;
408 case 503: return HTTP_ERR_503;
409 case 504: return HTTP_ERR_504;
410 default: return HTTP_ERR_500;
411 }
412}
413
Willy Tarreau80587432006-12-24 17:47:20 +0100414void init_proto_http()
415{
Willy Tarreau42250582007-04-01 01:30:43 +0200416 int i;
417 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100418 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200419
Willy Tarreau80587432006-12-24 17:47:20 +0100420 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
421 if (!http_err_msgs[msg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100422 ha_alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
Willy Tarreau80587432006-12-24 17:47:20 +0100423 abort();
424 }
425
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 http_err_chunks[msg].area = (char *)http_err_msgs[msg];
427 http_err_chunks[msg].data = strlen(http_err_msgs[msg]);
Willy Tarreau80587432006-12-24 17:47:20 +0100428 }
Willy Tarreau42250582007-04-01 01:30:43 +0200429
430 /* initialize the log header encoding map : '{|}"#' should be encoded with
431 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
432 * URL encoding only requires '"', '#' to be encoded as well as non-
433 * printable characters above.
434 */
435 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
436 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100437 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200438 for (i = 0; i < 32; i++) {
439 FD_SET(i, hdr_encode_map);
440 FD_SET(i, url_encode_map);
441 }
442 for (i = 127; i < 256; i++) {
443 FD_SET(i, hdr_encode_map);
444 FD_SET(i, url_encode_map);
445 }
446
447 tmp = "\"#{|}";
448 while (*tmp) {
449 FD_SET(*tmp, hdr_encode_map);
450 tmp++;
451 }
452
453 tmp = "\"#";
454 while (*tmp) {
455 FD_SET(*tmp, url_encode_map);
456 tmp++;
457 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200458
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100459 /* initialize the http header encoding map. The draft httpbis define the
460 * header content as:
461 *
462 * HTTP-message = start-line
463 * *( header-field CRLF )
464 * CRLF
465 * [ message-body ]
466 * header-field = field-name ":" OWS field-value OWS
467 * field-value = *( field-content / obs-fold )
468 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
469 * obs-fold = CRLF 1*( SP / HTAB )
470 * field-vchar = VCHAR / obs-text
471 * VCHAR = %x21-7E
472 * obs-text = %x80-FF
473 *
474 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
475 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
476 * "obs-fold" is volontary forgotten because haproxy remove this.
477 */
478 memset(http_encode_map, 0, sizeof(http_encode_map));
479 for (i = 0x00; i <= 0x08; i++)
480 FD_SET(i, http_encode_map);
481 for (i = 0x0a; i <= 0x1f; i++)
482 FD_SET(i, http_encode_map);
483 FD_SET(0x7f, http_encode_map);
484
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200485 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100486 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
487 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100488}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489
Willy Tarreau53b6c742006-12-17 13:37:46 +0100490/*
491 * We have 26 list of methods (1 per first letter), each of which can have
492 * up to 3 entries (2 valid, 1 null).
493 */
494struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100495 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100496 int len;
497 const char text[8];
498};
499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100500const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100501 ['C' - 'A'] = {
502 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
503 },
504 ['D' - 'A'] = {
505 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
506 },
507 ['G' - 'A'] = {
508 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
509 },
510 ['H' - 'A'] = {
511 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
512 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200513 ['O' - 'A'] = {
514 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
515 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100516 ['P' - 'A'] = {
517 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
518 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
519 },
520 ['T' - 'A'] = {
521 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
522 },
523 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200524 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100525 */
526};
527
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100528const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100529 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
530 [HTTP_METH_GET] = { "GET", 3 },
531 [HTTP_METH_HEAD] = { "HEAD", 4 },
532 [HTTP_METH_POST] = { "POST", 4 },
533 [HTTP_METH_PUT] = { "PUT", 3 },
534 [HTTP_METH_DELETE] = { "DELETE", 6 },
535 [HTTP_METH_TRACE] = { "TRACE", 5 },
536 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
537};
538
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100539/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100540 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200541 * the last CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100542 * The header is also automatically added to the index <hdr_idx>, and the end
543 * of headers is automatically adjusted. The number of bytes added is returned
544 * on success, otherwise <0 is returned indicating an error.
545 */
Willy Tarreau4d893d42018-07-12 15:43:32 +0200546static inline int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100547{
Willy Tarreau4d893d42018-07-12 15:43:32 +0200548 return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100549}
550
551/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100552 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200553 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100554 * The header is also automatically added to the index <hdr_idx>, and the end
555 * of headers is automatically adjusted. The number of bytes added is returned
556 * on success, otherwise <0 is returned indicating an error.
557 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100558int http_header_add_tail2(struct http_msg *msg,
559 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100560{
561 int bytes;
562
Willy Tarreau4d893d42018-07-12 15:43:32 +0200563 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100564 if (!bytes)
565 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100566 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100567 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
568}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569
570/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100571 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
572 * If so, returns the position of the first non-space character relative to
573 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
574 * to return a pointer to the place after the first space. Returns 0 if the
575 * header name does not match. Checks are case-insensitive.
576 */
577int http_header_match2(const char *hdr, const char *end,
578 const char *name, int len)
579{
580 const char *val;
581
582 if (hdr + len >= end)
583 return 0;
584 if (hdr[len] != ':')
585 return 0;
586 if (strncasecmp(hdr, name, len) != 0)
587 return 0;
588 val = hdr + len + 1;
589 while (val < end && HTTP_IS_SPHT(*val))
590 val++;
591 if ((val >= end) && (len + 2 <= end - hdr))
592 return len + 2; /* we may replace starting from second space */
593 return val - hdr;
594}
595
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200596/* Find the first or next occurrence of header <name> in message buffer <sol>
597 * using headers index <idx>, and return it in the <ctx> structure. This
598 * structure holds everything necessary to use the header and find next
599 * occurrence. If its <idx> member is 0, the header is searched from the
600 * beginning. Otherwise, the next occurrence is returned. The function returns
601 * 1 when it finds a value, and 0 when there is no more. It is very similar to
602 * http_find_header2() except that it is designed to work with full-line headers
603 * whose comma is not a delimiter but is part of the syntax. As a special case,
604 * if ctx->val is NULL when searching for a new values of a header, the current
605 * header is rescanned. This allows rescanning after a header deletion.
606 */
607int http_find_full_header2(const char *name, int len,
608 char *sol, struct hdr_idx *idx,
609 struct hdr_ctx *ctx)
610{
611 char *eol, *sov;
612 int cur_idx, old_idx;
613
614 cur_idx = ctx->idx;
615 if (cur_idx) {
616 /* We have previously returned a header, let's search another one */
617 sol = ctx->line;
618 eol = sol + idx->v[cur_idx].len;
619 goto next_hdr;
620 }
621
622 /* first request for this header */
623 sol += hdr_idx_first_pos(idx);
624 old_idx = 0;
625 cur_idx = hdr_idx_first_idx(idx);
626 while (cur_idx) {
627 eol = sol + idx->v[cur_idx].len;
628
629 if (len == 0) {
630 /* No argument was passed, we want any header.
631 * To achieve this, we simply build a fake request. */
632 while (sol + len < eol && sol[len] != ':')
633 len++;
634 name = sol;
635 }
636
637 if ((len < eol - sol) &&
638 (sol[len] == ':') &&
639 (strncasecmp(sol, name, len) == 0)) {
640 ctx->del = len;
641 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100642 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200643 sov++;
644
645 ctx->line = sol;
646 ctx->prev = old_idx;
647 ctx->idx = cur_idx;
648 ctx->val = sov - sol;
649 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100650 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200651 eol--;
652 ctx->tws++;
653 }
654 ctx->vlen = eol - sov;
655 return 1;
656 }
657 next_hdr:
658 sol = eol + idx->v[cur_idx].cr + 1;
659 old_idx = cur_idx;
660 cur_idx = idx->v[cur_idx].next;
661 }
662 return 0;
663}
664
Willy Tarreauc90dc232015-02-20 13:51:36 +0100665/* Find the first or next header field in message buffer <sol> using headers
666 * index <idx>, and return it in the <ctx> structure. This structure holds
667 * everything necessary to use the header and find next occurrence. If its
668 * <idx> member is 0, the first header is retrieved. Otherwise, the next
669 * occurrence is returned. The function returns 1 when it finds a value, and
670 * 0 when there is no more. It is equivalent to http_find_full_header2() with
671 * no header name.
672 */
673int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
674{
675 char *eol, *sov;
676 int cur_idx, old_idx;
677 int len;
678
679 cur_idx = ctx->idx;
680 if (cur_idx) {
681 /* We have previously returned a header, let's search another one */
682 sol = ctx->line;
683 eol = sol + idx->v[cur_idx].len;
684 goto next_hdr;
685 }
686
687 /* first request for this header */
688 sol += hdr_idx_first_pos(idx);
689 old_idx = 0;
690 cur_idx = hdr_idx_first_idx(idx);
691 while (cur_idx) {
692 eol = sol + idx->v[cur_idx].len;
693
694 len = 0;
695 while (1) {
696 if (len >= eol - sol)
697 goto next_hdr;
698 if (sol[len] == ':')
699 break;
700 len++;
701 }
702
703 ctx->del = len;
704 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100705 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100706 sov++;
707
708 ctx->line = sol;
709 ctx->prev = old_idx;
710 ctx->idx = cur_idx;
711 ctx->val = sov - sol;
712 ctx->tws = 0;
713
Willy Tarreau2235b262016-11-05 15:50:20 +0100714 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100715 eol--;
716 ctx->tws++;
717 }
718 ctx->vlen = eol - sov;
719 return 1;
720
721 next_hdr:
722 sol = eol + idx->v[cur_idx].cr + 1;
723 old_idx = cur_idx;
724 cur_idx = idx->v[cur_idx].next;
725 }
726 return 0;
727}
728
Lukas Tribus23953682017-04-28 13:24:30 +0000729/* Find the end of the header value contained between <s> and <e>. See RFC7230,
730 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100731 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200732 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100733char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200734{
735 int quoted, qdpair;
736
737 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100738
739#if defined(__x86_64__) || \
740 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
741 defined(__ARM_ARCH_7A__)
742 /* speedup: skip everything not a comma nor a double quote */
743 for (; s <= e - sizeof(int); s += sizeof(int)) {
744 unsigned int c = *(int *)s; // comma
745 unsigned int q = c; // quote
746
747 c ^= 0x2c2c2c2c; // contains one zero on a comma
748 q ^= 0x22222222; // contains one zero on a quote
749
750 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
751 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
752
753 if ((c | q) & 0x80808080)
754 break; // found a comma or a quote
755 }
756#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 for (; s < e; s++) {
758 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200759 else if (quoted) {
760 if (*s == '\\') qdpair = 1;
761 else if (*s == '"') quoted = 0;
762 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200763 else if (*s == '"') quoted = 1;
764 else if (*s == ',') return s;
765 }
766 return s;
767}
768
769/* Find the first or next occurrence of header <name> in message buffer <sol>
770 * using headers index <idx>, and return it in the <ctx> structure. This
771 * structure holds everything necessary to use the header and find next
772 * occurrence. If its <idx> member is 0, the header is searched from the
773 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100774 * 1 when it finds a value, and 0 when there is no more. It is designed to work
775 * with headers defined as comma-separated lists. As a special case, if ctx->val
776 * is NULL when searching for a new values of a header, the current header is
777 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200778 */
779int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100780 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200781 struct hdr_ctx *ctx)
782{
Willy Tarreau68085d82010-01-18 14:54:04 +0100783 char *eol, *sov;
784 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200785
Willy Tarreau68085d82010-01-18 14:54:04 +0100786 cur_idx = ctx->idx;
787 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200788 /* We have previously returned a value, let's search
789 * another one on the same line.
790 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200792 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200794 eol = sol + idx->v[cur_idx].len;
795
796 if (sov >= eol)
797 /* no more values in this header */
798 goto next_hdr;
799
Willy Tarreau68085d82010-01-18 14:54:04 +0100800 /* values remaining for this header, skip the comma but save it
801 * for later use (eg: for header deletion).
802 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200803 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100804 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200805 sov++;
806
807 goto return_hdr;
808 }
809
810 /* first request for this header */
811 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100812 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200813 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200814 while (cur_idx) {
815 eol = sol + idx->v[cur_idx].len;
816
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200817 if (len == 0) {
818 /* No argument was passed, we want any header.
819 * To achieve this, we simply build a fake request. */
820 while (sol + len < eol && sol[len] != ':')
821 len++;
822 name = sol;
823 }
824
Willy Tarreau33a7e692007-06-10 19:45:56 +0200825 if ((len < eol - sol) &&
826 (sol[len] == ':') &&
827 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100828 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200829 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100830 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200831 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100832
Willy Tarreau33a7e692007-06-10 19:45:56 +0200833 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100834 ctx->prev = old_idx;
835 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200836 ctx->idx = cur_idx;
837 ctx->val = sov - sol;
838
839 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200840 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100841 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200842 eol--;
843 ctx->tws++;
844 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200845 ctx->vlen = eol - sov;
846 return 1;
847 }
848 next_hdr:
849 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100850 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200851 cur_idx = idx->v[cur_idx].next;
852 }
853 return 0;
854}
855
856int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100857 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200858 struct hdr_ctx *ctx)
859{
860 return http_find_header2(name, strlen(name), sol, idx, ctx);
861}
862
Willy Tarreau68085d82010-01-18 14:54:04 +0100863/* Remove one value of a header. This only works on a <ctx> returned by one of
864 * the http_find_header functions. The value is removed, as well as surrounding
865 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100866 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100867 * message <msg>. The new index is returned. If it is zero, it means there is
868 * no more header, so any processing may stop. The ctx is always left in a form
869 * that can be handled by http_find_header2() to find next occurrence.
870 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100871int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100872{
873 int cur_idx = ctx->idx;
874 char *sol = ctx->line;
875 struct hdr_idx_elem *hdr;
876 int delta, skip_comma;
877
878 if (!cur_idx)
879 return 0;
880
881 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200882 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100883 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200884 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100885 http_msg_move_end(msg, delta);
886 idx->used--;
887 hdr->len = 0; /* unused entry */
888 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100889 if (idx->tail == ctx->idx)
890 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100891 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100892 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100893 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200894 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100895 return ctx->idx;
896 }
897
898 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200899 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
900 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100901 */
902
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200903 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200904 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200905 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 NULL, 0);
907 hdr->len += delta;
908 http_msg_move_end(msg, delta);
909 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200910 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100911 return ctx->idx;
912}
913
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100914/* This function handles a server error at the stream interface level. The
915 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100916 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100917 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100918 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200920static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200921 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922{
Willy Tarreau2019f952017-03-14 11:07:31 +0100923 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100924 channel_auto_read(si_oc(si));
925 channel_abort(si_oc(si));
926 channel_auto_close(si_oc(si));
927 channel_erase(si_oc(si));
928 channel_auto_close(si_ic(si));
929 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200930 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200931 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200932 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200933 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200934 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200935 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936}
937
Willy Tarreau87b09662015-04-03 00:22:06 +0200938/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100939 * and message.
940 */
941
Willy Tarreau83061a82018-07-13 11:56:34 +0200942struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100943{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200944 const int msgnum = http_get_status_idx(s->txn->status);
945
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200946 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200947 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200948 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200949 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100950 else
951 return &http_err_chunks[msgnum];
952}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200953
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100954void
Willy Tarreau83061a82018-07-13 11:56:34 +0200955http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100956{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200957 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100958 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100959 stream_int_retnclose(&s->si[0], msg);
960}
961
Willy Tarreau53b6c742006-12-17 13:37:46 +0100962/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200963 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
964 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100965 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100966enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100967{
968 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100969 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100970
971 m = ((unsigned)*str - 'A');
972
973 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100974 for (h = http_methods[m]; h->len > 0; h++) {
975 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100976 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100977 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100978 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100979 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200981 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100982}
983
Willy Tarreau21d2af32008-02-14 20:25:24 +0100984/* Parse the URI from the given transaction (which is assumed to be in request
985 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
986 * It is returned otherwise.
987 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200988char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100989{
990 char *ptr, *end;
991
Willy Tarreauf37954d2018-06-15 18:31:02 +0200992 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100993 end = ptr + txn->req.sl.rq.u_l;
994
995 if (ptr >= end)
996 return NULL;
997
Lukas Tribus23953682017-04-28 13:24:30 +0000998 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100999 * Request-URI = "*" | absuri | abspath | authority
1000 */
1001
1002 if (*ptr == '*')
1003 return NULL;
1004
1005 if (isalpha((unsigned char)*ptr)) {
1006 /* this is a scheme as described by RFC3986, par. 3.1 */
1007 ptr++;
1008 while (ptr < end &&
1009 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1010 ptr++;
1011 /* skip '://' */
1012 if (ptr == end || *ptr++ != ':')
1013 return NULL;
1014 if (ptr == end || *ptr++ != '/')
1015 return NULL;
1016 if (ptr == end || *ptr++ != '/')
1017 return NULL;
1018 }
1019 /* skip [user[:passwd]@]host[:[port]] */
1020
1021 while (ptr < end && *ptr != '/')
1022 ptr++;
1023
1024 if (ptr == end)
1025 return NULL;
1026
1027 /* OK, we got the '/' ! */
1028 return ptr;
1029}
1030
William Lallemand65ad6e12014-01-31 15:08:02 +01001031/* Parse the URI from the given string and look for the "/" beginning the PATH.
1032 * If not found, return NULL. It is returned otherwise.
1033 */
1034static char *
1035http_get_path_from_string(char *str)
1036{
1037 char *ptr = str;
1038
1039 /* RFC2616, par. 5.1.2 :
1040 * Request-URI = "*" | absuri | abspath | authority
1041 */
1042
1043 if (*ptr == '*')
1044 return NULL;
1045
1046 if (isalpha((unsigned char)*ptr)) {
1047 /* this is a scheme as described by RFC3986, par. 3.1 */
1048 ptr++;
1049 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1050 ptr++;
1051 /* skip '://' */
1052 if (*ptr == '\0' || *ptr++ != ':')
1053 return NULL;
1054 if (*ptr == '\0' || *ptr++ != '/')
1055 return NULL;
1056 if (*ptr == '\0' || *ptr++ != '/')
1057 return NULL;
1058 }
1059 /* skip [user[:passwd]@]host[:[port]] */
1060
1061 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1062 ptr++;
1063
1064 if (*ptr == '\0' || *ptr == ' ')
1065 return NULL;
1066
1067 /* OK, we got the '/' ! */
1068 return ptr;
1069}
1070
Willy Tarreau71241ab2012-12-27 11:30:54 +01001071/* Returns a 302 for a redirectable request that reaches a server working in
1072 * in redirect mode. This may only be called just after the stream interface
1073 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1074 * follow normal proxy processing. NOTE: this function is designed to support
1075 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001076 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001077void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001078{
1079 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001080 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001081 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001082 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083
1084 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001085 trash.data = strlen(HTTP_302);
1086 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001087
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001088 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001089
Willy Tarreauefb453c2008-10-26 20:49:47 +01001090 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001091 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001092 return;
1093
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001094 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001095 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001096 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
1097 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001098 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001099
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001100 /* 3: add the request URI. Since it was already forwarded, we need
1101 * to temporarily rewind the buffer.
1102 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001103 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001104 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001105
Willy Tarreauefb453c2008-10-26 20:49:47 +01001106 path = http_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001107 len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001108
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001109 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001110
Willy Tarreauefb453c2008-10-26 20:49:47 +01001111 if (!path)
1112 return;
1113
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001114 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001115 return;
1116
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001117 memcpy(trash.area + trash.data, path, len);
1118 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001119
1120 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001121 memcpy(trash.area + trash.data,
1122 "\r\nProxy-Connection: close\r\n\r\n", 29);
1123 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001124 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001125 memcpy(trash.area + trash.data,
1126 "\r\nConnection: close\r\n\r\n", 23);
1127 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001128 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001129
1130 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001131 si_shutr(si);
1132 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001134 si->state = SI_ST_CLO;
1135
1136 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001137 txn->status = 302;
1138 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001139
1140 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001141 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001142 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001143}
1144
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001145/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001146 * that the server side is closed. Note that err_type is actually a
1147 * bitmask, where almost only aborts may be cumulated with other
1148 * values. We consider that aborted operations are more important
1149 * than timeouts or errors due to the fact that nobody else in the
1150 * logs might explain incomplete retries. All others should avoid
1151 * being cumulated. It should normally not be possible to have multiple
1152 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001153 * Note that connection errors appearing on the second request of a keep-alive
1154 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001155 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001156void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001157{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001158 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001159
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001160 /* set s->txn->status for http_error_message(s) */
1161 s->txn->status = 503;
1162
Willy Tarreauefb453c2008-10-26 20:49:47 +01001163 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001164 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001165 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001166 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001167 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001168 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001169 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001170 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001171 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001172 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001173 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001174 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001175 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001176 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001177 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001178 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001179 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001180 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001181 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001182 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001183 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001184 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001185 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001186 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001187 http_error_message(s));
1188 else { /* SI_ET_CONN_OTHER and others */
1189 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001190 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001191 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001192 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001193}
1194
Willy Tarreau42250582007-04-01 01:30:43 +02001195extern const char sess_term_cond[8];
1196extern const char sess_fin_state[8];
1197extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001198struct pool_head *pool_head_http_txn;
1199struct pool_head *pool_head_requri;
1200struct pool_head *pool_head_capture = NULL;
1201struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202
Willy Tarreau117f59e2007-03-04 18:17:17 +01001203/*
1204 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001205 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001206 */
1207void capture_headers(char *som, struct hdr_idx *idx,
1208 char **cap, struct cap_hdr *cap_hdr)
1209{
1210 char *eol, *sol, *col, *sov;
1211 int cur_idx;
1212 struct cap_hdr *h;
1213 int len;
1214
1215 sol = som + hdr_idx_first_pos(idx);
1216 cur_idx = hdr_idx_first_idx(idx);
1217
1218 while (cur_idx) {
1219 eol = sol + idx->v[cur_idx].len;
1220
1221 col = sol;
1222 while (col < eol && *col != ':')
1223 col++;
1224
1225 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001226 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001227 sov++;
1228
1229 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001230 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001231 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1232 if (cap[h->index] == NULL)
1233 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001234 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001235
1236 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001237 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001238 continue;
1239 }
1240
1241 len = eol - sov;
1242 if (len > h->len)
1243 len = h->len;
1244
1245 memcpy(cap[h->index], sov, len);
1246 cap[h->index][len]=0;
1247 }
1248 }
1249 sol = eol + idx->v[cur_idx].cr + 1;
1250 cur_idx = idx->v[cur_idx].next;
1251 }
1252}
1253
Willy Tarreaubaaee002006-06-26 02:48:02 +02001254/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001255 * Returns the data from Authorization header. Function may be called more
1256 * than once so data is stored in txn->auth_data. When no header is found
1257 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001258 * searching again for something we are unable to find anyway. However, if
1259 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001260 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001261 */
1262
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001263int
Willy Tarreau87b09662015-04-03 00:22:06 +02001264get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001265{
1266
Willy Tarreaueee5b512015-04-03 23:46:31 +02001267 struct http_txn *txn = s->txn;
Willy Tarreau83061a82018-07-13 11:56:34 +02001268 struct buffer auth_method;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001269 struct hdr_ctx ctx;
1270 char *h, *p;
1271 int len;
1272
1273#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001274 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001275#endif
1276
1277 if (txn->auth.method == HTTP_AUTH_WRONG)
1278 return 0;
1279
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001280 txn->auth.method = HTTP_AUTH_WRONG;
1281
1282 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001283
1284 if (txn->flags & TX_USE_PX_CONN) {
1285 h = "Proxy-Authorization";
1286 len = strlen(h);
1287 } else {
1288 h = "Authorization";
1289 len = strlen(h);
1290 }
1291
Willy Tarreauf37954d2018-06-15 18:31:02 +02001292 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001293 return 0;
1294
1295 h = ctx.line + ctx.val;
1296
1297 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001298 len = p - h;
1299 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001300 return 0;
1301
David Carlier7365f7d2016-04-04 11:54:42 +01001302 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1303 return 0;
1304
Willy Tarreau5c557d12016-03-13 08:17:02 +01001305 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001306
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001307 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001308 struct buffer *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001309
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001310 len = base64dec(txn->auth.method_data.area,
1311 txn->auth.method_data.data,
1312 http_auth->area, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001313
1314 if (len < 0)
1315 return 0;
1316
1317
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001318 http_auth->area[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001319
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001320 p = strchr(http_auth->area, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001321
1322 if (!p)
1323 return 0;
1324
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001325 txn->auth.user = http_auth->area;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001326 *p = '\0';
1327 txn->auth.pass = p+1;
1328
1329 txn->auth.method = HTTP_AUTH_BASIC;
1330 return 1;
1331 }
1332
1333 return 0;
1334}
1335
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001336
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001337/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1338 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1339 * nothing is done and 1 is returned.
1340 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001341static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001342{
1343 int delta;
1344 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001345 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001346
1347 if (msg->sl.rq.v_l != 0)
1348 return 1;
1349
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001350 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1351 if (txn->meth != HTTP_METH_GET)
1352 return 0;
1353
Willy Tarreauf37954d2018-06-15 18:31:02 +02001354 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001355
1356 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001357 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1358 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001359 }
1360 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001361 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001362 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001363 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001364 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001365 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001366 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001367 NULL, NULL);
1368 if (unlikely(!cur_end))
1369 return 0;
1370
1371 /* we have a full HTTP/1.0 request now and we know that
1372 * we have either a CR or an LF at <ptr>.
1373 */
1374 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1375 return 1;
1376}
1377
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001378/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001379 * and "keep-alive" values. If we already know that some headers may safely
1380 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001381 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1382 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001383 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001384 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1385 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1386 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001387 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001388 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001389void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001390{
Willy Tarreau5b154472009-12-21 20:11:07 +01001391 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001392 const char *hdr_val = "Connection";
1393 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001394
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001395 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001396 return;
1397
Willy Tarreau88d349d2010-01-25 12:15:43 +01001398 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1399 hdr_val = "Proxy-Connection";
1400 hdr_len = 16;
1401 }
1402
Willy Tarreau5b154472009-12-21 20:11:07 +01001403 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001404 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001405 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001406 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1407 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001408 if (to_del & 2)
1409 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001410 else
1411 txn->flags |= TX_CON_KAL_SET;
1412 }
1413 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1414 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001415 if (to_del & 1)
1416 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001417 else
1418 txn->flags |= TX_CON_CLO_SET;
1419 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001420 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1421 txn->flags |= TX_HDR_CONN_UPG;
1422 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001423 }
1424
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001425 txn->flags |= TX_HDR_CONN_PRS;
1426 return;
1427}
Willy Tarreau5b154472009-12-21 20:11:07 +01001428
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001429/* Apply desired changes on the Connection: header. Values may be removed and/or
1430 * added depending on the <wanted> flags, which are exclusively composed of
1431 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1432 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1433 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001434void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001435{
1436 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001437 const char *hdr_val = "Connection";
1438 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001439
1440 ctx.idx = 0;
1441
Willy Tarreau88d349d2010-01-25 12:15:43 +01001442
1443 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1444 hdr_val = "Proxy-Connection";
1445 hdr_len = 16;
1446 }
1447
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001448 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001449 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001450 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1451 if (wanted & TX_CON_KAL_SET)
1452 txn->flags |= TX_CON_KAL_SET;
1453 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001454 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001455 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001456 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1457 if (wanted & TX_CON_CLO_SET)
1458 txn->flags |= TX_CON_CLO_SET;
1459 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001460 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001461 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001462 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001463
1464 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1465 return;
1466
1467 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1468 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001469 hdr_val = "Connection: close";
1470 hdr_len = 17;
1471 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1472 hdr_val = "Proxy-Connection: close";
1473 hdr_len = 23;
1474 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001475 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001476 }
1477
1478 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1479 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001480 hdr_val = "Connection: keep-alive";
1481 hdr_len = 22;
1482 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1483 hdr_val = "Proxy-Connection: keep-alive";
1484 hdr_len = 28;
1485 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001486 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001487 }
1488 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001489}
1490
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001491/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1492 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1493 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1494 * Unparsable qvalues return 1000 as "q=1.000".
1495 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001496int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001497{
1498 int q = 1000;
1499
Willy Tarreau506c69a2014-07-08 00:59:48 +02001500 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001501 goto out;
1502 q = (*qvalue++ - '0') * 1000;
1503
1504 if (*qvalue++ != '.')
1505 goto out;
1506
Willy Tarreau506c69a2014-07-08 00:59:48 +02001507 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001508 goto out;
1509 q += (*qvalue++ - '0') * 100;
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') * 10;
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') * 1;
1518 out:
1519 if (q > 1000)
1520 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001521 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001522 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001523 return q;
1524}
William Lallemand82fe75c2012-10-23 10:25:10 +02001525
Willy Tarreau87b09662015-04-03 00:22:06 +02001526void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001527{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001528 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001529 int tmp = TX_CON_WANT_KAL;
1530
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001531 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1532 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1534 tmp = TX_CON_WANT_TUN;
1535
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001536 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001537 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1538 tmp = TX_CON_WANT_TUN;
1539 }
1540
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001541 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001542 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1543 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001544 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001545 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1546 tmp = TX_CON_WANT_CLO;
1547 else
1548 tmp = TX_CON_WANT_SCL;
1549 }
1550
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001551 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001552 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1553 tmp = TX_CON_WANT_CLO;
1554
1555 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1556 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1557
1558 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1559 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1560 /* parse the Connection header and possibly clean it */
1561 int to_del = 0;
1562 if ((msg->flags & HTTP_MSGF_VER_11) ||
1563 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001564 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001565 to_del |= 2; /* remove "keep-alive" */
1566 if (!(msg->flags & HTTP_MSGF_VER_11))
1567 to_del |= 1; /* remove "close" */
1568 http_parse_connection_header(txn, msg, to_del);
1569 }
1570
1571 /* check if client or config asks for explicit close in KAL/SCL */
1572 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1573 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1574 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1575 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1576 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001577 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001578 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1579}
William Lallemand82fe75c2012-10-23 10:25:10 +02001580
Willy Tarreaud787e662009-07-07 10:14:51 +02001581/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1582 * processing can continue on next analysers, or zero if it either needs more
1583 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001584 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001585 * when it has nothing left to do, and may remove any analyser when it wants to
1586 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001588int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589{
Willy Tarreau59234e92008-11-30 23:51:27 +01001590 /*
1591 * We will parse the partial (or complete) lines.
1592 * We will check the request syntax, and also join multi-line
1593 * headers. An index of all the lines will be elaborated while
1594 * parsing.
1595 *
1596 * For the parsing, we use a 28 states FSM.
1597 *
1598 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02001599 * ci_head(req) = beginning of request
1600 * ci_head(req) + msg->eoh = end of processed headers / start of current one
1601 * ci_tail(req) = end of input data
1602 * msg->eol = end of current header or line (LF or CRLF)
1603 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001604 *
1605 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001606 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001607 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1608 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001609 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001610
Willy Tarreau59234e92008-11-30 23:51:27 +01001611 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001612 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001613 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001614 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001615 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001616
Christopher Faulet45073512018-07-20 10:16:29 +02001617 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01001618 now_ms, __FUNCTION__,
1619 s,
1620 req,
1621 req->rex, req->wex,
1622 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001623 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +01001624 req->analysers);
1625
Willy Tarreau52a0c602009-08-16 22:45:38 +02001626 /* we're speaking HTTP here, so let's speak HTTP to the client */
1627 s->srv_error = http_return_srv_error;
1628
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001629 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001630 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001631 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1632
Willy Tarreau83e3af02009-12-28 17:39:57 +01001633 /* There's a protected area at the end of the buffer for rewriting
1634 * purposes. We don't want to start to parse the request if the
1635 * protected area is affected, because we may have to move processed
1636 * data later, which is much more complicated.
1637 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001638 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001639 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001640 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001641 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001642 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001643 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001644 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001645 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001646 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001647 return 0;
1648 }
Willy Tarreau188e2302018-06-15 11:11:53 +02001649 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001650 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001651 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001652 }
1653
Willy Tarreauf37954d2018-06-15 18:31:02 +02001654 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001655 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001656 }
1657
Willy Tarreau59234e92008-11-30 23:51:27 +01001658 /* 1: we might have to print this header in debug mode */
1659 if (unlikely((global.mode & MODE_DEBUG) &&
1660 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001661 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001662 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001663
Willy Tarreauf37954d2018-06-15 18:31:02 +02001664 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +02001665 /* this is a bit complex : in case of error on the request line,
1666 * we know that rq.l is still zero, so we display only the part
1667 * up to the end of the line (truncated by debug_hdr).
1668 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001669 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +01001670 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001671
Willy Tarreau59234e92008-11-30 23:51:27 +01001672 sol += hdr_idx_first_pos(&txn->hdr_idx);
1673 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001674
Willy Tarreau59234e92008-11-30 23:51:27 +01001675 while (cur_idx) {
1676 eol = sol + txn->hdr_idx.v[cur_idx].len;
1677 debug_hdr("clihdr", s, sol, eol);
1678 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1679 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001681 }
1682
Willy Tarreau58f10d72006-12-04 02:26:12 +01001683
Willy Tarreau59234e92008-11-30 23:51:27 +01001684 /*
1685 * Now we quickly check if we have found a full valid request.
1686 * If not so, we check the FD and buffer states before leaving.
1687 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001688 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001689 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001690 * on a keep-alive stream, if we encounter and error, close, t/o,
1691 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001692 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001693 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001694 * Last, we may increase some tracked counters' http request errors on
1695 * the cases that are deliberately the client's fault. For instance,
1696 * a timeout or connection reset is not counted as an error. However
1697 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001698 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001699
Willy Tarreau655dce92009-11-08 13:10:58 +01001700 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001701 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001702 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001703 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001704 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001705 stream_inc_http_req_ctr(s);
1706 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001707 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001708 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau59234e92008-11-30 23:51:27 +01001711 /* 1: Since we are in header mode, if there's no space
1712 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001713 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001714 * must terminate it now.
1715 */
Willy Tarreau23752332018-06-15 14:54:53 +02001716 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001717 /* FIXME: check if URI is set and return Status
1718 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001719 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001720 stream_inc_http_req_ctr(s);
1721 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001722 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001723 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02001724 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01001725 goto return_bad_req;
1726 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001727
Willy Tarreau59234e92008-11-30 23:51:27 +01001728 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001729 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001730 if (!(s->flags & SF_ERR_MASK))
1731 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001732
Willy Tarreaufcffa692010-01-10 14:21:19 +01001733 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001734 goto failed_keep_alive;
1735
Willy Tarreau0f228a02015-05-01 15:37:53 +02001736 if (sess->fe->options & PR_O_IGNORE_PRB)
1737 goto failed_keep_alive;
1738
Willy Tarreau59234e92008-11-30 23:51:27 +01001739 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001740 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001741 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001742 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001743 }
1744
Willy Tarreaudc979f22012-12-04 10:39:01 +01001745 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001746 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001747 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001748 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001749 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001750 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001751 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001752 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001753 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001754 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001755
Willy Tarreaue7dff022015-04-03 01:14:29 +02001756 if (!(s->flags & SF_FINST_MASK))
1757 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001758 return 0;
1759 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001760
Willy Tarreau59234e92008-11-30 23:51:27 +01001761 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001762 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001763 if (!(s->flags & SF_ERR_MASK))
1764 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001765
Willy Tarreaufcffa692010-01-10 14:21:19 +01001766 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001767 goto failed_keep_alive;
1768
Willy Tarreau0f228a02015-05-01 15:37:53 +02001769 if (sess->fe->options & PR_O_IGNORE_PRB)
1770 goto failed_keep_alive;
1771
Willy Tarreau59234e92008-11-30 23:51:27 +01001772 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001773 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001774 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001775 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001776 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001777 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001778 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001779 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001780 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001781 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001782
Willy Tarreau87b09662015-04-03 00:22:06 +02001783 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001784 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001785 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001786 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001787 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001788
Willy Tarreaue7dff022015-04-03 01:14:29 +02001789 if (!(s->flags & SF_FINST_MASK))
1790 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001791 return 0;
1792 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001793
Willy Tarreau59234e92008-11-30 23:51:27 +01001794 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001795 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001796 if (!(s->flags & SF_ERR_MASK))
1797 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001798
Willy Tarreaufcffa692010-01-10 14:21:19 +01001799 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001800 goto failed_keep_alive;
1801
Willy Tarreau0f228a02015-05-01 15:37:53 +02001802 if (sess->fe->options & PR_O_IGNORE_PRB)
1803 goto failed_keep_alive;
1804
Willy Tarreau4076a152009-04-02 15:18:36 +02001805 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001806 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001807 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001808 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001809 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001810 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001811 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001812 stream_inc_http_err_ctr(s);
1813 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001814 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001815 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001816 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001817 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001818
Willy Tarreaue7dff022015-04-03 01:14:29 +02001819 if (!(s->flags & SF_FINST_MASK))
1820 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001821 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001822 }
1823
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001824 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001825 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001826 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001827#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001828 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001829 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001830 /* We need more data, we have to re-enable quick-ack in case we
1831 * previously disabled it, otherwise we might cause the client
1832 * to delay next data.
1833 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001834 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001835 }
1836#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001837
Willy Tarreaufcffa692010-01-10 14:21:19 +01001838 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1839 /* If the client starts to talk, let's fall back to
1840 * request timeout processing.
1841 */
1842 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001843 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001844 }
1845
Willy Tarreau59234e92008-11-30 23:51:27 +01001846 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001847 if (!tick_isset(req->analyse_exp)) {
1848 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1849 (txn->flags & TX_WAIT_NEXT_RQ) &&
1850 tick_isset(s->be->timeout.httpka))
1851 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1852 else
1853 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1854 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001855
Willy Tarreau59234e92008-11-30 23:51:27 +01001856 /* we're not ready yet */
1857 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001858
1859 failed_keep_alive:
1860 /* Here we process low-level errors for keep-alive requests. In
1861 * short, if the request is not the first one and it experiences
1862 * a timeout, read error or shutdown, we just silently close so
1863 * that the client can try again.
1864 */
1865 txn->status = 0;
1866 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001867 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001868 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001869 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001870 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001871 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001872 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001873 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001874
Willy Tarreaud787e662009-07-07 10:14:51 +02001875 /* OK now we have a complete HTTP request with indexed headers. Let's
1876 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001877 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001878 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001879 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001880 * byte after the last LF. msg->sov points to the first byte of data.
1881 * msg->eol cannot be trusted because it may have been left uninitialized
1882 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001883 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001884
Willy Tarreau87b09662015-04-03 00:22:06 +02001885 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001886 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001887
Willy Tarreaub16a5742010-01-10 14:46:16 +01001888 if (txn->flags & TX_WAIT_NEXT_RQ) {
1889 /* kill the pending keep-alive timeout */
1890 txn->flags &= ~TX_WAIT_NEXT_RQ;
1891 req->analyse_exp = TICK_ETERNITY;
1892 }
1893
1894
Willy Tarreaud787e662009-07-07 10:14:51 +02001895 /* Maybe we found in invalid header name while we were configured not
1896 * to block on that, so we have to capture it now.
1897 */
1898 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001899 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001900
Willy Tarreau59234e92008-11-30 23:51:27 +01001901 /*
1902 * 1: identify the method
1903 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001904 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001905
1906 /* we can make use of server redirect on GET and HEAD */
1907 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001908 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001909 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001910 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001911 /* PRI is reserved for the HTTP/2 preface */
1912 msg->err_pos = 0;
1913 goto return_bad_req;
1914 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001915
Willy Tarreau59234e92008-11-30 23:51:27 +01001916 /*
1917 * 2: check if the URI matches the monitor_uri.
1918 * We have to do this for every request which gets in, because
1919 * the monitor-uri is defined by the frontend.
1920 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001921 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1922 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001923 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001924 sess->fe->monitor_uri,
1925 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001926 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001927 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001928 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001929 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001930
Willy Tarreaue7dff022015-04-03 01:14:29 +02001931 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001932 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001933
Willy Tarreau59234e92008-11-30 23:51:27 +01001934 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001935 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001936 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001937
Willy Tarreau59234e92008-11-30 23:51:27 +01001938 ret = acl_pass(ret);
1939 if (cond->pol == ACL_COND_UNLESS)
1940 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001941
Willy Tarreau59234e92008-11-30 23:51:27 +01001942 if (ret) {
1943 /* we fail this request, let's return 503 service unavail */
1944 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001945 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001946 if (!(s->flags & SF_ERR_MASK))
1947 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001948 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001949 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001950 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001951
Willy Tarreau59234e92008-11-30 23:51:27 +01001952 /* nothing to fail, let's reply normaly */
1953 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001954 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001955 if (!(s->flags & SF_ERR_MASK))
1956 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001957 goto return_prx_cond;
1958 }
1959
1960 /*
1961 * 3: Maybe we have to copy the original REQURI for the logs ?
1962 * Note: we cannot log anymore if the request has been
1963 * classified as invalid.
1964 */
1965 if (unlikely(s->logs.logwait & LW_REQ)) {
1966 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001967 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001968 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001970 if (urilen >= global.tune.requri_len )
1971 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001972 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001973 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001975 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001976 s->do_log(s);
1977 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001978 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001979 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001980 }
Willy Tarreau06619262006-12-17 08:37:22 +01001981
Willy Tarreau91852eb2015-05-01 13:26:00 +02001982 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1983 * exactly one digit "." one digit. This check may be disabled using
1984 * option accept-invalid-http-request.
1985 */
1986 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1987 if (msg->sl.rq.v_l != 8) {
1988 msg->err_pos = msg->sl.rq.v;
1989 goto return_bad_req;
1990 }
1991
Willy Tarreauf37954d2018-06-15 18:31:02 +02001992 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1993 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1994 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1995 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001996 msg->err_pos = msg->sl.rq.v + 4;
1997 goto return_bad_req;
1998 }
1999 }
Willy Tarreau13317662015-05-01 13:47:08 +02002000 else {
2001 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2002 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2003 goto return_bad_req;
2004 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002005
Willy Tarreau5b154472009-12-21 20:11:07 +01002006 /* ... and check if the request is HTTP/1.1 or above */
2007 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02002008 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
2009 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
2010 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002011 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002012
2013 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002014 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 +01002015
Willy Tarreau88d349d2010-01-25 12:15:43 +01002016 /* if the frontend has "option http-use-proxy-header", we'll check if
2017 * we have what looks like a proxied connection instead of a connection,
2018 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2019 * Note that this is *not* RFC-compliant, however browsers and proxies
2020 * happen to do that despite being non-standard :-(
2021 * We consider that a request not beginning with either '/' or '*' is
2022 * a proxied connection, which covers both "scheme://location" and
2023 * CONNECT ip:port.
2024 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002025 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02002026 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002027 txn->flags |= TX_USE_PX_CONN;
2028
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002029 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002030 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002031
Willy Tarreau59234e92008-11-30 23:51:27 +01002032 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002033 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02002034 capture_headers(ci_head(req), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002035 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002036
Willy Tarreau557f1992015-05-01 10:05:17 +02002037 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2038 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002039 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002040 * The length of a message body is determined by one of the following
2041 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002042 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002043 * 1. Any response to a HEAD request and any response with a 1xx
2044 * (Informational), 204 (No Content), or 304 (Not Modified) status
2045 * code is always terminated by the first empty line after the
2046 * header fields, regardless of the header fields present in the
2047 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002048 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002049 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2050 * the connection will become a tunnel immediately after the empty
2051 * line that concludes the header fields. A client MUST ignore any
2052 * Content-Length or Transfer-Encoding header fields received in
2053 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002054 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002055 * 3. If a Transfer-Encoding header field is present and the chunked
2056 * transfer coding (Section 4.1) is the final encoding, the message
2057 * body length is determined by reading and decoding the chunked
2058 * data until the transfer coding indicates the data is complete.
2059 *
2060 * If a Transfer-Encoding header field is present in a response and
2061 * the chunked transfer coding is not the final encoding, the
2062 * message body length is determined by reading the connection until
2063 * it is closed by the server. If a Transfer-Encoding header field
2064 * is present in a request and the chunked transfer coding is not
2065 * the final encoding, the message body length cannot be determined
2066 * reliably; the server MUST respond with the 400 (Bad Request)
2067 * status code and then close the connection.
2068 *
2069 * If a message is received with both a Transfer-Encoding and a
2070 * Content-Length header field, the Transfer-Encoding overrides the
2071 * Content-Length. Such a message might indicate an attempt to
2072 * perform request smuggling (Section 9.5) or response splitting
2073 * (Section 9.4) and ought to be handled as an error. A sender MUST
2074 * remove the received Content-Length field prior to forwarding such
2075 * a message downstream.
2076 *
2077 * 4. If a message is received without Transfer-Encoding and with
2078 * either multiple Content-Length header fields having differing
2079 * field-values or a single Content-Length header field having an
2080 * invalid value, then the message framing is invalid and the
2081 * recipient MUST treat it as an unrecoverable error. If this is a
2082 * request message, the server MUST respond with a 400 (Bad Request)
2083 * status code and then close the connection. If this is a response
2084 * message received by a proxy, the proxy MUST close the connection
2085 * to the server, discard the received response, and send a 502 (Bad
2086 * Gateway) response to the client. If this is a response message
2087 * received by a user agent, the user agent MUST close the
2088 * connection to the server and discard the received response.
2089 *
2090 * 5. If a valid Content-Length header field is present without
2091 * Transfer-Encoding, its decimal value defines the expected message
2092 * body length in octets. If the sender closes the connection or
2093 * the recipient times out before the indicated number of octets are
2094 * received, the recipient MUST consider the message to be
2095 * incomplete and close the connection.
2096 *
2097 * 6. If this is a request message and none of the above are true, then
2098 * the message body length is zero (no message body is present).
2099 *
2100 * 7. Otherwise, this is a response message without a declared message
2101 * body length, so the message body length is determined by the
2102 * number of octets received prior to the server closing the
2103 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002104 */
2105
Willy Tarreau32b47f42009-10-18 20:55:02 +02002106 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002107 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02002108 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002109 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002110 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002111 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002112 /* chunked not last, return badreq */
2113 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002114 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002115 }
2116
Willy Tarreau1c913912015-04-30 10:57:51 +02002117 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002118 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002119 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002120 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02002121 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2122 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02002123 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002124 signed long long cl;
2125
Willy Tarreauad14f752011-09-02 20:33:27 +02002126 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002127 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002128 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002129 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002130
Willy Tarreauad14f752011-09-02 20:33:27 +02002131 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002132 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002133 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002134 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002135
Willy Tarreauad14f752011-09-02 20:33:27 +02002136 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002137 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002138 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002139 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002140
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002141 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002142 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002143 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002144 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002145
Christopher Fauletbe821b92017-03-30 11:21:53 +02002146 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002147 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002148 }
2149
Willy Tarreau34dfc602015-05-01 10:09:49 +02002150 /* even bodyless requests have a known length */
2151 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002152
Willy Tarreau179085c2014-04-28 16:48:56 +02002153 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2154 * only change if both the request and the config reference something else.
2155 * Option httpclose by itself sets tunnel mode where headers are mangled.
2156 * However, if another mode is set, it will affect it (eg: server-close/
2157 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2158 * if FE and BE have the same settings (common). The method consists in
2159 * checking if options changed between the two calls (implying that either
2160 * one is non-null, or one of them is non-null and we are there for the first
2161 * time.
2162 */
2163 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002164 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002165 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002166
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002167 /* we may have to wait for the request's body */
2168 if ((s->be->options & PR_O_WREQ_BODY) &&
2169 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2170 req->analysers |= AN_REQ_HTTP_BODY;
2171
Willy Tarreau83ece462017-12-21 15:13:09 +01002172 /*
2173 * RFC7234#4:
2174 * A cache MUST write through requests with methods
2175 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2176 * the origin server; i.e., a cache is not allowed
2177 * to generate a reply to such a request before
2178 * having forwarded the request and having received
2179 * a corresponding response.
2180 *
2181 * RFC7231#4.2.1:
2182 * Of the request methods defined by this
2183 * specification, the GET, HEAD, OPTIONS, and TRACE
2184 * methods are defined to be safe.
2185 */
2186 if (likely(txn->meth == HTTP_METH_GET ||
2187 txn->meth == HTTP_METH_HEAD ||
2188 txn->meth == HTTP_METH_OPTIONS ||
2189 txn->meth == HTTP_METH_TRACE))
2190 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2191
Willy Tarreaud787e662009-07-07 10:14:51 +02002192 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002193 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002194 req->analyse_exp = TICK_ETERNITY;
2195 return 1;
2196
2197 return_bad_req:
2198 /* We centralize bad requests processing here */
2199 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2200 /* we detected a parsing error. We want to archive this request
2201 * in the dedicated proxy area for later troubleshooting.
2202 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002203 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002204 }
2205
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002206 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002207 txn->req.msg_state = HTTP_MSG_ERROR;
2208 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002209 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002210
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002211 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002212 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002213 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002214
2215 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002216 if (!(s->flags & SF_ERR_MASK))
2217 s->flags |= SF_ERR_PRXCOND;
2218 if (!(s->flags & SF_FINST_MASK))
2219 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002220
Christopher Faulet0184ea72017-01-05 14:06:34 +01002221 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002222 req->analyse_exp = TICK_ETERNITY;
2223 return 0;
2224}
2225
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002226
Willy Tarreau347a35d2013-11-22 17:51:09 +01002227/* This function prepares an applet to handle the stats. It can deal with the
2228 * "100-continue" expectation, check that admin rules are met for POST requests,
2229 * and program a response message if something was unexpected. It cannot fail
2230 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002231 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002233 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002234 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002235int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002236{
2237 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002238 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002239 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002240 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002241 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002242 struct uri_auth *uri_auth = s->be->uri_auth;
2243 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002244 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002245
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002246 appctx = si_appctx(si);
2247 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2248 appctx->st1 = appctx->st2 = 0;
2249 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2250 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002251 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002252 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002253
Willy Tarreauf37954d2018-06-15 18:31:02 +02002254 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002255 lookup = uri + uri_auth->uri_len;
2256
2257 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2258 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002259 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002260 break;
2261 }
2262 }
2263
2264 if (uri_auth->refresh) {
2265 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2266 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002267 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002268 break;
2269 }
2270 }
2271 }
2272
2273 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2274 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002275 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002276 break;
2277 }
2278 }
2279
Willy Tarreau1e62df92016-01-11 18:57:53 +01002280 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2281 if (memcmp(h, ";typed", 6) == 0) {
2282 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2283 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2284 break;
2285 }
2286 }
2287
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002288 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2289 if (memcmp(h, ";st=", 4) == 0) {
2290 int i;
2291 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002292 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002293 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2294 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002295 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002296 break;
2297 }
2298 }
2299 break;
2300 }
2301 }
2302
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002303 appctx->ctx.stats.scope_str = 0;
2304 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002305 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2306 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2307 int itx = 0;
2308 const char *h2;
2309 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2310 const char *err;
2311
2312 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2313 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002314 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002315 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2316 itx++;
2317 h++;
2318 }
2319
2320 if (itx > STAT_SCOPE_TXT_MAXLEN)
2321 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002322 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002325 memcpy(scope_txt, h2, itx);
2326 scope_txt[itx] = '\0';
2327 err = invalid_char(scope_txt);
2328 if (err) {
2329 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002330 appctx->ctx.stats.scope_str = 0;
2331 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002332 }
2333 break;
2334 }
2335 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002336
2337 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002338 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002339 int ret = 1;
2340
2341 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002342 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 +01002343 ret = acl_pass(ret);
2344 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2345 ret = !ret;
2346 }
2347
2348 if (ret) {
2349 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002350 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002351 break;
2352 }
2353 }
2354
2355 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002356 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002358 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002359 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2360 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002361 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002362 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002363 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002364 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2365 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002366 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002367 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002368 else {
2369 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002370 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002371 }
2372
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002373 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002374 return 1;
2375}
2376
Lukas Tribus67db8df2013-06-23 17:37:13 +02002377/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2378 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2379 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002380void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002381{
2382#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002383 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002384 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2385#endif
2386#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002387 if (from->ss_family == AF_INET6) {
2388 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002389 /* v4-mapped addresses need IP_TOS */
2390 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2391 else
2392 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2393 }
2394#endif
2395}
2396
Willy Tarreau87b09662015-04-03 00:22:06 +02002397int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002398 const char* name, unsigned int name_len,
2399 const char *str, struct my_regex *re,
2400 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002401{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002402 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002403 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02002404 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002405 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2406 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02002407 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002408
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002409 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002410
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002411 /* Choose the header browsing function. */
2412 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002413 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002414 http_find_hdr_func = http_find_header2;
2415 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002416 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002417 http_find_hdr_func = http_find_full_header2;
2418 break;
2419 default: /* impossible */
2420 return -1;
2421 }
2422
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002423 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2424 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02002425 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002426 char *val = ctx.line + ctx.val;
2427 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002428
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002429 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2430 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002431
Willy Tarreau6e27be12018-08-22 04:46:47 +02002432 len = exp_replace(output->area, output->size, val, str, pmatch);
2433 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002434 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002435
Willy Tarreau6e27be12018-08-22 04:46:47 +02002436 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002437
2438 hdr->len += delta;
2439 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002440
2441 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002442 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002443 }
2444
2445 return 0;
2446}
2447
Willy Tarreau87b09662015-04-03 00:22:06 +02002448static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002449 const char* name, unsigned int name_len,
2450 struct list *fmt, struct my_regex *re,
2451 int action)
2452{
Willy Tarreau83061a82018-07-13 11:56:34 +02002453 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002454 int ret = -1;
2455
2456 replace = alloc_trash_chunk();
2457 if (!replace)
2458 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002459
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002460 replace->data = build_logline(s, replace->area, replace->size, fmt);
2461 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002462 goto leave;
2463
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002464 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
2465 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002466
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002467 leave:
2468 free_trash_chunk(replace);
2469 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002470}
2471
Willy Tarreau87b09662015-04-03 00:22:06 +02002472/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002473 * transaction <txn>. Returns the verdict of the first rule that prevents
2474 * further processing of the request (auth, deny, ...), and defaults to
2475 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2476 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002477 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2478 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2479 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002480 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002481enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002482http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002483{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002484 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002485 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002486 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002487 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002488 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002489 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002490 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002491 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002492
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002493 /* If "the current_rule_list" match the executed rule list, we are in
2494 * resume condition. If a resume is needed it is always in the action
2495 * and never in the ACL or converters. In this case, we initialise the
2496 * current rule, and go to the action execution point.
2497 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002498 if (s->current_rule) {
2499 rule = s->current_rule;
2500 s->current_rule = NULL;
2501 if (s->current_rule_list == rules)
2502 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002503 }
2504 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002505
Willy Tarreauff011f22011-01-06 17:51:27 +01002506 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002507
Willy Tarreau96257ec2012-12-27 10:46:37 +01002508 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002509 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002510 int ret;
2511
Willy Tarreau192252e2015-04-04 01:47:55 +02002512 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002513 ret = acl_pass(ret);
2514
Willy Tarreauff011f22011-01-06 17:51:27 +01002515 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002516 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002517
2518 if (!ret) /* condition not matched */
2519 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002520 }
2521
Willy Tarreauacc98002015-09-27 23:34:39 +02002522 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002523resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002524 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002525 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002526 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002527
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002528 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002529 if (deny_status)
2530 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002531 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002532
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002533 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002534 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002535 if (deny_status)
2536 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002537 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002539 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002540 /* Auth might be performed on regular http-req rules as well as on stats */
2541 auth_realm = rule->arg.auth.realm;
2542 if (!auth_realm) {
2543 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2544 auth_realm = STATS_DEFAULT_REALM;
2545 else
2546 auth_realm = px->id;
2547 }
2548 /* send 401/407 depending on whether we use a proxy or not. We still
2549 * count one error, because normal browsing won't significantly
2550 * increase the counter but brute force attempts will.
2551 */
2552 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2553 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002554 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002555 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002556 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002557
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002558 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002559 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2560 return HTTP_RULE_RES_BADREQ;
2561 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002562
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002563 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002564 s->task->nice = rule->arg.nice;
2565 break;
2566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002567 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002568 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002569 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002570 break;
2571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002572 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002573#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002574 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002575 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002576#endif
2577 break;
2578
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002579 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002580 s->logs.level = rule->arg.loglevel;
2581 break;
2582
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002583 case ACT_HTTP_REPLACE_HDR:
2584 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002585 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2586 rule->arg.hdr_add.name_len,
2587 &rule->arg.hdr_add.fmt,
2588 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002589 return HTTP_RULE_RES_BADREQ;
2590 break;
2591
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002592 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002593 ctx.idx = 0;
2594 /* remove all occurrences of the header */
2595 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002596 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002597 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002598 }
Willy Tarreau85603282015-01-21 20:39:27 +01002599 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002600
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002601 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002602 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002603 /* The scope of the trash buffer must be limited to this function. The
2604 * build_logline() function can execute a lot of other function which
2605 * can use the trash buffer. So for limiting the scope of this global
2606 * buffer, we build first the header value using build_logline, and
2607 * after we store the header name.
2608 */
Willy Tarreau83061a82018-07-13 11:56:34 +02002609 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002610
2611 replace = alloc_trash_chunk();
2612 if (!replace)
2613 return HTTP_RULE_RES_BADREQ;
2614
Thierry Fournier4b788f72016-06-01 13:35:36 +02002615 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002616 len += build_logline(s, replace->area + len,
2617 replace->size - len,
2618 &rule->arg.hdr_add.fmt);
2619 memcpy(replace->area, rule->arg.hdr_add.name,
2620 rule->arg.hdr_add.name_len);
2621 replace->area[rule->arg.hdr_add.name_len] = ':';
2622 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
2623 replace->data = 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,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002629 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002630 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2631 }
2632 }
2633
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002635 static unsigned char rate_limit = 0;
2636
2637 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 replace->area[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,
2640 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002641 }
2642
2643 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2644 if (sess->fe != s->be)
2645 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2646 if (sess->listener->counters)
2647 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2648 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002649
2650 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002651 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002652 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002653
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002654 case ACT_HTTP_DEL_ACL:
2655 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002656 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002657 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002658
2659 /* collect reference */
2660 ref = pat_ref_lookup(rule->arg.map.ref);
2661 if (!ref)
2662 continue;
2663
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002664 /* allocate key */
2665 key = alloc_trash_chunk();
2666 if (!key)
2667 return HTTP_RULE_RES_BADREQ;
2668
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002669 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 key->data = build_logline(s, key->area, key->size,
2671 &rule->arg.map.key);
2672 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002673
2674 /* perform update */
2675 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002676 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002677 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002678 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002679
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002680 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002681 break;
2682 }
2683
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002684 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002685 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002686 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002687
2688 /* collect reference */
2689 ref = pat_ref_lookup(rule->arg.map.ref);
2690 if (!ref)
2691 continue;
2692
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002693 /* allocate key */
2694 key = alloc_trash_chunk();
2695 if (!key)
2696 return HTTP_RULE_RES_BADREQ;
2697
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002698 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002699 key->data = build_logline(s, key->area, key->size,
2700 &rule->arg.map.key);
2701 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002702
2703 /* perform update */
2704 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002705 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002706 if (pat_ref_find_elt(ref, key->area) == NULL)
2707 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002708 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002709
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002710 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002711 break;
2712 }
2713
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002714 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002715 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002716 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002717
2718 /* collect reference */
2719 ref = pat_ref_lookup(rule->arg.map.ref);
2720 if (!ref)
2721 continue;
2722
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002723 /* allocate key */
2724 key = alloc_trash_chunk();
2725 if (!key)
2726 return HTTP_RULE_RES_BADREQ;
2727
2728 /* allocate value */
2729 value = alloc_trash_chunk();
2730 if (!value) {
2731 free_trash_chunk(key);
2732 return HTTP_RULE_RES_BADREQ;
2733 }
2734
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002735 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 key->data = build_logline(s, key->area, key->size,
2737 &rule->arg.map.key);
2738 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002739
2740 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002741 value->data = build_logline(s, value->area,
2742 value->size,
2743 &rule->arg.map.value);
2744 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002745
2746 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002747 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002748 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002749 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002750 else
2751 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002752 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002753
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002754 free_trash_chunk(key);
2755 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002756 break;
2757 }
William Lallemand73025dd2014-04-24 14:38:37 +02002758
Thierry FOURNIER42148732015-09-02 17:17:33 +02002759 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002760 if ((s->req.flags & CF_READ_ERROR) ||
2761 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2762 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2763 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002764 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002765
Willy Tarreauacc98002015-09-27 23:34:39 +02002766 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002767 case ACT_RET_ERR:
2768 case ACT_RET_CONT:
2769 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002770 case ACT_RET_STOP:
2771 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002772 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002773 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002774 return HTTP_RULE_RES_YIELD;
2775 }
William Lallemand73025dd2014-04-24 14:38:37 +02002776 break;
2777
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002778 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002779 /* Note: only the first valid tracking parameter of each
2780 * applies.
2781 */
2782
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002783 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002784 struct stktable *t;
2785 struct stksess *ts;
2786 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002787 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002788
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002789 t = rule->arg.trk_ctr.table.t;
2790 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 +02002791
2792 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002793 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002794
2795 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002796 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2797 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2798 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002799 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002800
Emeric Brun819fc6f2017-06-13 19:37:32 +02002801 if (ptr1)
2802 stktable_data_cast(ptr1, http_req_cnt)++;
2803
2804 if (ptr2)
2805 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2806 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2807
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002808 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002809
2810 /* If data was modified, we need to touch to re-schedule sync */
2811 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002812 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002813
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002814 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002815 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002816 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002817 }
2818 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002819 break;
2820
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002821 /* other flags exists, but normaly, they never be matched. */
2822 default:
2823 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002824 }
2825 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002826
2827 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002828 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002829}
2830
Willy Tarreau71241ab2012-12-27 11:30:54 +01002831
Willy Tarreau51d861a2015-05-22 17:30:48 +02002832/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2833 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2834 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2835 * is returned, the process can continue the evaluation of next rule list. If
2836 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2837 * is returned, it means the operation could not be processed and a server error
2838 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2839 * deny rule. If *YIELD is returned, the caller must call again the function
2840 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002841 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002842static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002843http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002844{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002845 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002846 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002847 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002848 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002849 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002850 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002851
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002852 /* If "the current_rule_list" match the executed rule list, we are in
2853 * resume condition. If a resume is needed it is always in the action
2854 * and never in the ACL or converters. In this case, we initialise the
2855 * current rule, and go to the action execution point.
2856 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002857 if (s->current_rule) {
2858 rule = s->current_rule;
2859 s->current_rule = NULL;
2860 if (s->current_rule_list == rules)
2861 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002862 }
2863 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002864
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002865 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002866
2867 /* check optional condition */
2868 if (rule->cond) {
2869 int ret;
2870
Willy Tarreau192252e2015-04-04 01:47:55 +02002871 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002872 ret = acl_pass(ret);
2873
2874 if (rule->cond->pol == ACL_COND_UNLESS)
2875 ret = !ret;
2876
2877 if (!ret) /* condition not matched */
2878 continue;
2879 }
2880
Willy Tarreauacc98002015-09-27 23:34:39 +02002881 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002882resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002883 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002884 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002885 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002886
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002887 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002888 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002889 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002891 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002892 s->task->nice = rule->arg.nice;
2893 break;
2894
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002895 case ACT_HTTP_SET_TOS:
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 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002898 break;
2899
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002900 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002901#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002902 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002903 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002904#endif
2905 break;
2906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002907 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002908 s->logs.level = rule->arg.loglevel;
2909 break;
2910
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002911 case ACT_HTTP_REPLACE_HDR:
2912 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002913 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2914 rule->arg.hdr_add.name_len,
2915 &rule->arg.hdr_add.fmt,
2916 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002917 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002918 break;
2919
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002920 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002921 ctx.idx = 0;
2922 /* remove all occurrences of the header */
2923 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002924 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002925 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2926 }
Willy Tarreau85603282015-01-21 20:39:27 +01002927 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002928
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002929 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002930 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002931 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002932
2933 replace = alloc_trash_chunk();
2934 if (!replace)
2935 return HTTP_RULE_RES_BADREQ;
2936
2937 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002938 memcpy(replace->area, rule->arg.hdr_add.name,
2939 rule->arg.hdr_add.name_len);
2940 replace->data = rule->arg.hdr_add.name_len;
2941 replace->area[replace->data++] = ':';
2942 replace->area[replace->data++] = ' ';
2943 replace->data += build_logline(s,
2944 replace->area + replace->data,
2945 replace->size - replace->data,
2946 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002947
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002948 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002949 /* remove all occurrences of the header */
2950 ctx.idx = 0;
2951 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002952 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002953 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2954 }
2955 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002956
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002957 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002958 static unsigned char rate_limit = 0;
2959
2960 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002961 replace->area[rule->arg.hdr_add.name_len] = 0;
2962 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,
2963 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002964 }
2965
2966 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2967 if (sess->fe != s->be)
2968 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2969 if (sess->listener->counters)
2970 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2971 if (objt_server(s->target))
2972 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2973 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002974
2975 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002976 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002977 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002978
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002979 case ACT_HTTP_DEL_ACL:
2980 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002981 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002982 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002983
2984 /* collect reference */
2985 ref = pat_ref_lookup(rule->arg.map.ref);
2986 if (!ref)
2987 continue;
2988
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002989 /* allocate key */
2990 key = alloc_trash_chunk();
2991 if (!key)
2992 return HTTP_RULE_RES_BADREQ;
2993
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002994 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002995 key->data = build_logline(s, key->area, key->size,
2996 &rule->arg.map.key);
2997 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002998
2999 /* perform update */
3000 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003001 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003002 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003003 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003004
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003005 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003006 break;
3007 }
3008
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003009 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003010 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02003011 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003012
3013 /* collect reference */
3014 ref = pat_ref_lookup(rule->arg.map.ref);
3015 if (!ref)
3016 continue;
3017
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003018 /* allocate key */
3019 key = alloc_trash_chunk();
3020 if (!key)
3021 return HTTP_RULE_RES_BADREQ;
3022
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003023 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003024 key->data = build_logline(s, key->area, key->size,
3025 &rule->arg.map.key);
3026 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003027
3028 /* perform update */
3029 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003030 if (pat_ref_find_elt(ref, key->area) == NULL)
3031 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003032
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003033 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003034 break;
3035 }
3036
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003037 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003038 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02003039 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003040
3041 /* collect reference */
3042 ref = pat_ref_lookup(rule->arg.map.ref);
3043 if (!ref)
3044 continue;
3045
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003046 /* allocate key */
3047 key = alloc_trash_chunk();
3048 if (!key)
3049 return HTTP_RULE_RES_BADREQ;
3050
3051 /* allocate value */
3052 value = alloc_trash_chunk();
3053 if (!value) {
3054 free_trash_chunk(key);
3055 return HTTP_RULE_RES_BADREQ;
3056 }
3057
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003058 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003059 key->data = build_logline(s, key->area, key->size,
3060 &rule->arg.map.key);
3061 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003062
3063 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003064 value->data = build_logline(s, value->area,
3065 value->size,
3066 &rule->arg.map.value);
3067 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003068
3069 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003070 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003071 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003072 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003073 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003074 else
3075 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003076 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003077 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003078 free_trash_chunk(key);
3079 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003080 break;
3081 }
William Lallemand73025dd2014-04-24 14:38:37 +02003082
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003083 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003084 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3085 return HTTP_RULE_RES_BADREQ;
3086 return HTTP_RULE_RES_DONE;
3087
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003088 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3089 /* Note: only the first valid tracking parameter of each
3090 * applies.
3091 */
3092
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003093 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003094 struct stktable *t;
3095 struct stksess *ts;
3096 struct stktable_key *key;
3097 void *ptr;
3098
3099 t = rule->arg.trk_ctr.table.t;
3100 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3101
3102 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003103 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003104
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003105 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003106
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003107 /* let's count a new HTTP request as it's the first time we do it */
3108 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3109 if (ptr)
3110 stktable_data_cast(ptr, http_req_cnt)++;
3111
3112 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3113 if (ptr)
3114 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3115 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3116
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003117 /* When the client triggers a 4xx from the server, it's most often due
3118 * to a missing object or permission. These events should be tracked
3119 * because if they happen often, it may indicate a brute force or a
3120 * vulnerability scan. Normally this is done when receiving the response
3121 * but here we're tracking after this ought to have been done so we have
3122 * to do it on purpose.
3123 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003124 if ((unsigned)(txn->status - 400) < 100) {
3125 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3126 if (ptr)
3127 stktable_data_cast(ptr, http_err_cnt)++;
3128
3129 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3130 if (ptr)
3131 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3132 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3133 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003134
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003135 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003136
Emeric Brun0fed0b02017-11-29 16:15:07 +01003137 /* If data was modified, we need to touch to re-schedule sync */
3138 stktable_touch_local(t, ts, 0);
3139
Emeric Brun819fc6f2017-06-13 19:37:32 +02003140 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3141 if (sess->fe != s->be)
3142 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3143
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003144 }
3145 }
3146 break;
3147
Thierry FOURNIER42148732015-09-02 17:17:33 +02003148 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003149 if ((s->req.flags & CF_READ_ERROR) ||
3150 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3151 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3152 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003153 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003154
Willy Tarreauacc98002015-09-27 23:34:39 +02003155 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003156 case ACT_RET_ERR:
3157 case ACT_RET_CONT:
3158 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003159 case ACT_RET_STOP:
3160 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003161 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003162 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003163 return HTTP_RULE_RES_YIELD;
3164 }
William Lallemand73025dd2014-04-24 14:38:37 +02003165 break;
3166
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003167 /* other flags exists, but normaly, they never be matched. */
3168 default:
3169 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003170 }
3171 }
3172
3173 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003174 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003175}
3176
3177
Willy Tarreau71241ab2012-12-27 11:30:54 +01003178/* Perform an HTTP redirect based on the information in <rule>. The function
3179 * returns non-zero on success, or zero in case of a, irrecoverable error such
3180 * as too large a request to build a valid response.
3181 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003182static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003183{
Willy Tarreaub329a312015-05-22 16:27:37 +02003184 struct http_msg *req = &txn->req;
3185 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003186 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02003187 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003188 int ret = 0;
3189
3190 chunk = alloc_trash_chunk();
3191 if (!chunk)
3192 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003193
3194 /* build redirect message */
3195 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003196 case 308:
3197 msg_fmt = HTTP_308;
3198 break;
3199 case 307:
3200 msg_fmt = HTTP_307;
3201 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003202 case 303:
3203 msg_fmt = HTTP_303;
3204 break;
3205 case 301:
3206 msg_fmt = HTTP_301;
3207 break;
3208 case 302:
3209 default:
3210 msg_fmt = HTTP_302;
3211 break;
3212 }
3213
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003214 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3215 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003216
3217 switch(rule->type) {
3218 case REDIRECT_TYPE_SCHEME: {
3219 const char *path;
3220 const char *host;
3221 struct hdr_ctx ctx;
3222 int pathlen;
3223 int hostlen;
3224
3225 host = "";
3226 hostlen = 0;
3227 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003228 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003229 host = ctx.line + ctx.val;
3230 hostlen = ctx.vlen;
3231 }
3232
3233 path = http_get_path(txn);
3234 /* build message using path */
3235 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003236 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003237 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3238 int qs = 0;
3239 while (qs < pathlen) {
3240 if (path[qs] == '?') {
3241 pathlen = qs;
3242 break;
3243 }
3244 qs++;
3245 }
3246 }
3247 } else {
3248 path = "/";
3249 pathlen = 1;
3250 }
3251
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003252 if (rule->rdr_str) { /* this is an old "redirect" rule */
3253 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003254 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003255 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003256
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003257 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003258 memcpy(chunk->area + chunk->data, rule->rdr_str,
3259 rule->rdr_len);
3260 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003261 }
3262 else {
3263 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003264 chunk->data += build_logline(s,
3265 chunk->area + chunk->data,
3266 chunk->size - chunk->data,
3267 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003268
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003269 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003270 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003271 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003272 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003273 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003274 memcpy(chunk->area + chunk->data, "://", 3);
3275 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003276
3277 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003278 memcpy(chunk->area + chunk->data, host, hostlen);
3279 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003280
3281 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003282 memcpy(chunk->area + chunk->data, path, pathlen);
3283 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003284
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003285 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003286 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003287 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003288 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003289 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003290 chunk->area[chunk->data] = '/';
3291 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003292 }
3293
3294 break;
3295 }
3296 case REDIRECT_TYPE_PREFIX: {
3297 const char *path;
3298 int pathlen;
3299
3300 path = http_get_path(txn);
3301 /* build message using path */
3302 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003303 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003304 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3305 int qs = 0;
3306 while (qs < pathlen) {
3307 if (path[qs] == '?') {
3308 pathlen = qs;
3309 break;
3310 }
3311 qs++;
3312 }
3313 }
3314 } else {
3315 path = "/";
3316 pathlen = 1;
3317 }
3318
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003319 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003320 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003321 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003322
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003323 /* add prefix. Note that if prefix == "/", we don't want to
3324 * add anything, otherwise it makes it hard for the user to
3325 * configure a self-redirection.
3326 */
3327 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003328 memcpy(chunk->area + chunk->data,
3329 rule->rdr_str, rule->rdr_len);
3330 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003331 }
3332 }
3333 else {
3334 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003335 chunk->data += build_logline(s,
3336 chunk->area + chunk->data,
3337 chunk->size - chunk->data,
3338 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003339
3340 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003341 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003342 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003343 }
3344
3345 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003346 memcpy(chunk->area + chunk->data, path, pathlen);
3347 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003348
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003349 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003350 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003351 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003352 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003353 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003354 chunk->area[chunk->data] = '/';
3355 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003356 }
3357
3358 break;
3359 }
3360 case REDIRECT_TYPE_LOCATION:
3361 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003362 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003363 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003364 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003365
3366 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003367 memcpy(chunk->area + chunk->data, rule->rdr_str,
3368 rule->rdr_len);
3369 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003370 }
3371 else {
3372 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003373 chunk->data += build_logline(s,
3374 chunk->area + chunk->data,
3375 chunk->size - chunk->data,
3376 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003377
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003378 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003379 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003380 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003381 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003382 break;
3383 }
3384
3385 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003386 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
3387 chunk->data += 14;
3388 memcpy(chunk->area + chunk->data, rule->cookie_str,
3389 rule->cookie_len);
3390 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003391 }
3392
Willy Tarreau19b14122017-02-28 09:48:11 +01003393 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003394 txn->status = rule->code;
3395 /* let's log the request time */
3396 s->logs.tv_request = now;
3397
Christopher Fauletbe821b92017-03-30 11:21:53 +02003398 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003399 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3400 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3401 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003402 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003403 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003404 memcpy(chunk->area + chunk->data,
3405 "\r\nProxy-Connection: keep-alive", 30);
3406 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003407 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003408 memcpy(chunk->area + chunk->data,
3409 "\r\nConnection: keep-alive", 24);
3410 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003411 }
3412 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003413 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
3414 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003415 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003416 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003417 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003418 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02003419 req->next -= req->sov;
3420 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003421 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003422 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003423 req->msg_state = HTTP_MSG_CLOSED;
3424 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003425 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003426 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02003427 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003428 /* let the server side turn to SI_ST_CLO */
3429 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003430 } else {
3431 /* keep-alive not possible */
3432 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003433 memcpy(chunk->area + chunk->data,
3434 "\r\nProxy-Connection: close\r\n\r\n", 29);
3435 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003436 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003437 memcpy(chunk->area + chunk->data,
3438 "\r\nConnection: close\r\n\r\n", 23);
3439 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003440 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003441 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003442 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003443 }
3444
Willy Tarreaue7dff022015-04-03 01:14:29 +02003445 if (!(s->flags & SF_ERR_MASK))
3446 s->flags |= SF_ERR_LOCAL;
3447 if (!(s->flags & SF_FINST_MASK))
3448 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003449
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003450 ret = 1;
3451 leave:
3452 free_trash_chunk(chunk);
3453 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003454}
3455
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456/* This stream analyser runs all HTTP request processing which is common to
3457 * frontends and backends, which means blocking ACLs, filters, connection-close,
3458 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003459 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003460 * either needs more data or wants to immediately abort the request (eg: deny,
3461 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003462 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003463int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003464{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003465 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003466 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003467 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003468 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003469 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003470 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003471 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003472 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003473
Willy Tarreau655dce92009-11-08 13:10:58 +01003474 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003475 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003476 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003477 }
3478
Christopher Faulet45073512018-07-20 10:16:29 +02003479 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003480 now_ms, __FUNCTION__,
3481 s,
3482 req,
3483 req->rex, req->wex,
3484 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003485 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02003486 req->analysers);
3487
Willy Tarreau65410832014-04-28 21:25:43 +02003488 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003489 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003490
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003491 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003492 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003493 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003494
Willy Tarreau0b748332014-04-29 00:13:29 +02003495 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003496 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3497 goto return_prx_yield;
3498
Willy Tarreau0b748332014-04-29 00:13:29 +02003499 case HTTP_RULE_RES_CONT:
3500 case HTTP_RULE_RES_STOP: /* nothing to do */
3501 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003502
Willy Tarreau0b748332014-04-29 00:13:29 +02003503 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3504 if (txn->flags & TX_CLTARPIT)
3505 goto tarpit;
3506 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003507
Willy Tarreau0b748332014-04-29 00:13:29 +02003508 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3509 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003510
Willy Tarreau0b748332014-04-29 00:13:29 +02003511 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003512 goto done;
3513
Willy Tarreau0b748332014-04-29 00:13:29 +02003514 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3515 goto return_bad_req;
3516 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003517 }
3518
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003519 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3520 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003521 struct hdr_ctx ctx;
3522
3523 ctx.idx = 0;
3524 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02003525 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003526 if (unlikely(http_header_add_tail2(&txn->req,
3527 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02003528 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003529 goto return_bad_req;
3530 }
3531 }
3532
3533 }
3534
Willy Tarreau52542592014-04-28 18:33:26 +02003535 /* OK at this stage, we know that the request was accepted according to
3536 * the http-request rules, we can check for the stats. Note that the
3537 * URI is detected *before* the req* rules in order not to be affected
3538 * by a possible reqrep, while they are processed *after* so that a
3539 * reqdeny can still block them. This clearly needs to change in 1.6!
3540 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003541 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003542 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003543 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003544 txn->status = 500;
3545 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003546 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547
Willy Tarreaue7dff022015-04-03 01:14:29 +02003548 if (!(s->flags & SF_ERR_MASK))
3549 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003550 goto return_prx_cond;
3551 }
3552
3553 /* parse the whole stats request and extract the relevant information */
3554 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003555 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003556 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003557
Willy Tarreau0b748332014-04-29 00:13:29 +02003558 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3559 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003560
Willy Tarreau0b748332014-04-29 00:13:29 +02003561 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3562 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003563 }
3564
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003565 /* evaluate the req* rules except reqadd */
3566 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003567 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003568 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003569
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003570 if (txn->flags & TX_CLDENY)
3571 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003572
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003573 if (txn->flags & TX_CLTARPIT) {
3574 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003575 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003576 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003577 }
Willy Tarreau06619262006-12-17 08:37:22 +01003578
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003579 /* add request headers from the rule sets in the same order */
3580 list_for_each_entry(wl, &px->req_add, list) {
3581 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003582 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003583 ret = acl_pass(ret);
3584 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3585 ret = !ret;
3586 if (!ret)
3587 continue;
3588 }
3589
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003590 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003591 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003592 }
3593
Willy Tarreau52542592014-04-28 18:33:26 +02003594
3595 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003596 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3597 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003598 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003599 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003600 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003601
Willy Tarreaue7dff022015-04-03 01:14:29 +02003602 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3603 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3604 if (!(s->flags & SF_FINST_MASK))
3605 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003606
Willy Tarreau70730dd2014-04-24 18:06:27 +02003607 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003608 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3609 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003610 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003611 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003612 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003613
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003614 /* check whether we have some ACLs set to redirect this request */
3615 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003616 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003617 int ret;
3618
Willy Tarreau192252e2015-04-04 01:47:55 +02003619 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003620 ret = acl_pass(ret);
3621 if (rule->cond->pol == ACL_COND_UNLESS)
3622 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003623 if (!ret)
3624 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003625 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003626 if (!http_apply_redirect_rule(rule, s, txn))
3627 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003628 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003629 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003630
Willy Tarreau2be39392010-01-03 17:24:51 +01003631 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3632 * If this happens, then the data will not come immediately, so we must
3633 * send all what we have without waiting. Note that due to the small gain
3634 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003635 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003636 * itself once used.
3637 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003638 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003639
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003640 done: /* done with this analyser, continue with next ones that the calling
3641 * points will have set, if any.
3642 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003643 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003644 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3645 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003646 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003647
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003648 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003649 /* Allow cookie logging
3650 */
3651 if (s->be->cookie_name || sess->fe->capture_name)
3652 manage_client_side_cookies(s, req);
3653
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003654 /* When a connection is tarpitted, we use the tarpit timeout,
3655 * which may be the same as the connect timeout if unspecified.
3656 * If unset, then set it to zero because we really want it to
3657 * eventually expire. We build the tarpit as an analyser.
3658 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003659 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003660
3661 /* wipe the request out so that we can drop the connection early
3662 * if the client closes first.
3663 */
3664 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003665
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003666 txn->status = http_err_codes[deny_status];
3667
Christopher Faulet0184ea72017-01-05 14:06:34 +01003668 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003669 req->analysers |= AN_REQ_HTTP_TARPIT;
3670 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3671 if (!req->analyse_exp)
3672 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003673 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003674 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003675 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003676 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003677 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003678 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003679 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003680
3681 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003682
3683 /* Allow cookie logging
3684 */
3685 if (s->be->cookie_name || sess->fe->capture_name)
3686 manage_client_side_cookies(s, req);
3687
Willy Tarreau0b748332014-04-29 00:13:29 +02003688 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003689 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003690 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003691 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003692 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003693 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003694 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003695 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003696 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003697 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003698 goto return_prx_cond;
3699
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003700 return_bad_req:
3701 /* We centralize bad requests processing here */
3702 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3703 /* we detected a parsing error. We want to archive this request
3704 * in the dedicated proxy area for later troubleshooting.
3705 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003706 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003707 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003708
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003709 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003710 txn->req.msg_state = HTTP_MSG_ERROR;
3711 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003712 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003713
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003714 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003715 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003716 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003717
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003718 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003719 if (!(s->flags & SF_ERR_MASK))
3720 s->flags |= SF_ERR_PRXCOND;
3721 if (!(s->flags & SF_FINST_MASK))
3722 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003723
Christopher Faulet0184ea72017-01-05 14:06:34 +01003724 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003725 req->analyse_exp = TICK_ETERNITY;
3726 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003727
3728 return_prx_yield:
3729 channel_dont_connect(req);
3730 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003731}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003732
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003733/* This function performs all the processing enabled for the current request.
3734 * It returns 1 if the processing can continue on next analysers, or zero if it
3735 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003736 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003737 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003738int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003739{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003740 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003741 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003742 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003743 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003744
Willy Tarreau655dce92009-11-08 13:10:58 +01003745 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003746 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003747 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003748 return 0;
3749 }
3750
Christopher Faulet45073512018-07-20 10:16:29 +02003751 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003752 now_ms, __FUNCTION__,
3753 s,
3754 req,
3755 req->rex, req->wex,
3756 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003757 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003758 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003759
Willy Tarreau59234e92008-11-30 23:51:27 +01003760 /*
3761 * Right now, we know that we have processed the entire headers
3762 * and that unwanted requests have been filtered out. We can do
3763 * whatever we want with the remaining request. Also, now we
3764 * may have separate values for ->fe, ->be.
3765 */
Willy Tarreau06619262006-12-17 08:37:22 +01003766
Willy Tarreau59234e92008-11-30 23:51:27 +01003767 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003768 * If HTTP PROXY is set we simply get remote server address parsing
3769 * incoming request. Note that this requires that a connection is
3770 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003771 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003772 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003773 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003774 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003775
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003776 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003777 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003778 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003779 txn->req.msg_state = HTTP_MSG_ERROR;
3780 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003781 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003782 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003783
Willy Tarreaue7dff022015-04-03 01:14:29 +02003784 if (!(s->flags & SF_ERR_MASK))
3785 s->flags |= SF_ERR_RESOURCE;
3786 if (!(s->flags & SF_FINST_MASK))
3787 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003788
3789 return 0;
3790 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003791
3792 path = http_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003793 if (url2sa(ci_head(req) + msg->sl.rq.u,
3794 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003795 &conn->addr.to, NULL) == -1)
3796 goto return_bad_req;
3797
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003798 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003799 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3800 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003801 * u_l characters by a single "/".
3802 */
3803 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003804 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003805 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3806 int delta;
3807
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003808 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003809 http_msg_move_end(&txn->req, delta);
3810 cur_end += delta;
3811 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3812 goto return_bad_req;
3813 }
3814 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003815 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003816 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3817 int delta;
3818
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003819 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003820 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003821 http_msg_move_end(&txn->req, delta);
3822 cur_end += delta;
3823 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3824 goto return_bad_req;
3825 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003826 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003827
Willy Tarreau59234e92008-11-30 23:51:27 +01003828 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003829 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003830 * Note that doing so might move headers in the request, but
3831 * the fields will stay coherent and the URI will not move.
3832 * This should only be performed in the backend.
3833 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003834 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003835 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003836
William Lallemanda73203e2012-03-12 12:48:57 +01003837 /* add unique-id if "header-unique-id" is specified */
3838
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003839 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003840 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003841 goto return_bad_req;
3842 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003843 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003844 }
William Lallemanda73203e2012-03-12 12:48:57 +01003845
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003846 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003847 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003848 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003849 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003850 goto return_bad_req;
3851 }
3852
Cyril Bontéb21570a2009-11-29 20:04:48 +01003853 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003854 * 9: add X-Forwarded-For if either the frontend or the backend
3855 * asks for it.
3856 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003857 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003858 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003859 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3860 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3861 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003862 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003863 /* The header is set to be added only if none is present
3864 * and we found it, so don't do anything.
3865 */
3866 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003867 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003868 /* Add an X-Forwarded-For header unless the source IP is
3869 * in the 'except' network range.
3870 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003871 if ((!sess->fe->except_mask.s_addr ||
3872 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3873 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003874 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003875 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003876 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003877 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003878 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003879 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003880
3881 /* Note: we rely on the backend to get the header name to be used for
3882 * x-forwarded-for, because the header is really meant for the backends.
3883 * However, if the backend did not specify any option, we have to rely
3884 * on the frontend's header name.
3885 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003886 if (s->be->fwdfor_hdr_len) {
3887 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003888 memcpy(trash.area,
3889 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003890 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003891 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003892 memcpy(trash.area,
3893 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003894 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003895 len += snprintf(trash.area + len,
3896 trash.size - len,
3897 ": %d.%d.%d.%d", pn[0], pn[1],
3898 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003899
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003901 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003902 }
3903 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003904 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003905 /* FIXME: for the sake of completeness, we should also support
3906 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003907 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003908 int len;
3909 char pn[INET6_ADDRSTRLEN];
3910 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003911 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003912 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003913
Willy Tarreau59234e92008-11-30 23:51:27 +01003914 /* Note: we rely on the backend to get the header name to be used for
3915 * x-forwarded-for, because the header is really meant for the backends.
3916 * However, if the backend did not specify any option, we have to rely
3917 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003918 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003919 if (s->be->fwdfor_hdr_len) {
3920 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003921 memcpy(trash.area, s->be->fwdfor_hdr_name,
3922 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003923 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003924 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003925 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3926 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003927 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003928 len += snprintf(trash.area + len, trash.size - len,
3929 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003930
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003931 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003932 goto return_bad_req;
3933 }
3934 }
3935
3936 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003937 * 10: add X-Original-To if either the frontend or the backend
3938 * asks for it.
3939 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003940 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003941
3942 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003943 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003944 /* Add an X-Original-To header unless the destination IP is
3945 * in the 'except' network range.
3946 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003947 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003948
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003949 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003950 ((!sess->fe->except_mask_to.s_addr ||
3951 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3952 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003953 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003954 (((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 +02003955 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003956 int len;
3957 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003958 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003959
3960 /* Note: we rely on the backend to get the header name to be used for
3961 * x-original-to, because the header is really meant for the backends.
3962 * However, if the backend did not specify any option, we have to rely
3963 * on the frontend's header name.
3964 */
3965 if (s->be->orgto_hdr_len) {
3966 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003967 memcpy(trash.area,
3968 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003969 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003970 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003971 memcpy(trash.area,
3972 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003973 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003974 len += snprintf(trash.area + len,
3975 trash.size - len,
3976 ": %d.%d.%d.%d", pn[0], pn[1],
3977 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003978
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003979 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003980 goto return_bad_req;
3981 }
3982 }
3983 }
3984
Willy Tarreau50fc7772012-11-11 22:19:57 +01003985 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3986 * If an "Upgrade" token is found, the header is left untouched in order not to have
3987 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3988 * "Upgrade" is present in the Connection header.
3989 */
3990 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3991 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003992 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003993 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003994 unsigned int want_flags = 0;
3995
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003996 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003997 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003998 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003999 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004000 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004001 want_flags |= TX_CON_CLO_SET;
4002 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004003 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004004 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004005 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004006 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004007 want_flags |= TX_CON_KAL_SET;
4008 }
4009
4010 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004011 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004012 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004013
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004014
Willy Tarreau522d6c02009-12-06 18:49:18 +01004015 /* If we have no server assigned yet and we're balancing on url_param
4016 * with a POST request, we may be interested in checking the body for
4017 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004018 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004019 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004020 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004021 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004022 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004023 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004024 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004025
Christopher Fauletbe821b92017-03-30 11:21:53 +02004026 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
4027 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004028#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02004029 /* We expect some data from the client. Unless we know for sure
4030 * we already have a full request, we have to re-enable quick-ack
4031 * in case we previously disabled it, otherwise we might cause
4032 * the client to delay further data.
4033 */
4034 if ((sess->listener->options & LI_O_NOQUICKACK) &&
4035 cli_conn && conn_ctrl_ready(cli_conn) &&
4036 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004037 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02004038 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004039#endif
Willy Tarreau03945942009-12-22 16:50:27 +01004040
Willy Tarreau59234e92008-11-30 23:51:27 +01004041 /*************************************************************
4042 * OK, that's finished for the headers. We have done what we *
4043 * could. Let's switch to the DATA state. *
4044 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004045 req->analyse_exp = TICK_ETERNITY;
4046 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004047
Willy Tarreau59234e92008-11-30 23:51:27 +01004048 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004049 /* OK let's go on with the BODY now */
4050 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004051
Willy Tarreau59234e92008-11-30 23:51:27 +01004052 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004053 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004054 /* we detected a parsing error. We want to archive this request
4055 * in the dedicated proxy area for later troubleshooting.
4056 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004057 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004058 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004059
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004060 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004061 txn->req.msg_state = HTTP_MSG_ERROR;
4062 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004063 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004064 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004065
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004066 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004067 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004068 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004069
Willy Tarreaue7dff022015-04-03 01:14:29 +02004070 if (!(s->flags & SF_ERR_MASK))
4071 s->flags |= SF_ERR_PRXCOND;
4072 if (!(s->flags & SF_FINST_MASK))
4073 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004074 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004075}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004076
Willy Tarreau60b85b02008-11-30 23:28:40 +01004077/* This function is an analyser which processes the HTTP tarpit. It always
4078 * returns zero, at the beginning because it prevents any other processing
4079 * from occurring, and at the end because it terminates the request.
4080 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004081int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004082{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004083 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004084
4085 /* This connection is being tarpitted. The CLIENT side has
4086 * already set the connect expiration date to the right
4087 * timeout. We just have to check that the client is still
4088 * there and that the timeout has not expired.
4089 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004090 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004091 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004092 !tick_is_expired(req->analyse_exp, now_ms))
4093 return 0;
4094
4095 /* We will set the queue timer to the time spent, just for
4096 * logging purposes. We fake a 500 server error, so that the
4097 * attacker will not suspect his connection has been tarpitted.
4098 * It will not cause trouble to the logs because we can exclude
4099 * the tarpitted connections by filtering on the 'PT' status flags.
4100 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004101 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4102
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004103 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004104 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004105
Christopher Faulet0184ea72017-01-05 14:06:34 +01004106 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004107 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004108
Willy Tarreaue7dff022015-04-03 01:14:29 +02004109 if (!(s->flags & SF_ERR_MASK))
4110 s->flags |= SF_ERR_PRXCOND;
4111 if (!(s->flags & SF_FINST_MASK))
4112 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004113 return 0;
4114}
4115
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004116/* This function is an analyser which waits for the HTTP request body. It waits
4117 * for either the buffer to be full, or the full advertised contents to have
4118 * reached the buffer. It must only be called after the standard HTTP request
4119 * processing has occurred, because it expects the request to be parsed and will
4120 * look for the Expect header. It may send a 100-Continue interim response. It
4121 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4122 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4123 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004124 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004125int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004126{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004127 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004128 struct http_txn *txn = s->txn;
4129 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004130
4131 /* We have to parse the HTTP request body to find any required data.
4132 * "balance url_param check_post" should have been the only way to get
4133 * into this. We were brought here after HTTP header analysis, so all
4134 * related structures are ready.
4135 */
4136
Willy Tarreau890988f2014-04-10 11:59:33 +02004137 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4138 /* This is the first call */
4139 if (msg->msg_state < HTTP_MSG_BODY)
4140 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004141
Willy Tarreau890988f2014-04-10 11:59:33 +02004142 if (msg->msg_state < HTTP_MSG_100_SENT) {
4143 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4144 * send an HTTP/1.1 100 Continue intermediate response.
4145 */
4146 if (msg->flags & HTTP_MSGF_VER_11) {
4147 struct hdr_ctx ctx;
4148 ctx.idx = 0;
4149 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004150 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02004151 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004152 co_inject(&s->res,
4153 http_100_chunk.area,
4154 http_100_chunk.data);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004155 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004156 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004157 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004158 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004159 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004160
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004161 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02004162 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02004163 * must save the body in msg->next because it survives buffer
4164 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004165 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004166 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004167
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004168 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004169 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4170 else
4171 msg->msg_state = HTTP_MSG_DATA;
4172 }
4173
Willy Tarreau890988f2014-04-10 11:59:33 +02004174 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4175 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004176 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004177 goto missing_data;
4178
4179 /* OK we have everything we need now */
4180 goto http_end;
4181 }
4182
4183 /* OK here we're parsing a chunked-encoded message */
4184
Willy Tarreau522d6c02009-12-06 18:49:18 +01004185 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004186 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004187 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004188 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004189 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004190 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004191 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004192
Willy Tarreau115acb92009-12-26 13:56:06 +01004193 if (!ret)
4194 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004195 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004196 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004197 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004198 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004199 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004200 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004201 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004202
4203 msg->chunk_len = chunk;
4204 msg->body_len += chunk;
4205
4206 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004207 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004208 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004209 }
4210
Willy Tarreaud98cf932009-12-27 22:54:55 +01004211 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004212 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4213 * for at least a whole chunk or the whole content length bytes after
4214 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004215 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004216 if (msg->msg_state == HTTP_MSG_TRAILERS)
4217 goto http_end;
4218
Willy Tarreaue115b492015-05-01 23:05:14 +02004219 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004220 goto http_end;
4221
4222 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004223 /* we get here if we need to wait for more data. If the buffer is full,
4224 * we have the maximum we can expect.
4225 */
Willy Tarreau23752332018-06-15 14:54:53 +02004226 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02004227 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004228
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004229 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004230 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004231 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004232
Willy Tarreaue7dff022015-04-03 01:14:29 +02004233 if (!(s->flags & SF_ERR_MASK))
4234 s->flags |= SF_ERR_CLITO;
4235 if (!(s->flags & SF_FINST_MASK))
4236 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004237 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004238 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004239
4240 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004241 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004242 /* Not enough data. We'll re-use the http-request
4243 * timeout here. Ideally, we should set the timeout
4244 * relative to the accept() date. We just set the
4245 * request timeout once at the beginning of the
4246 * request.
4247 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004248 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004249 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004250 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004251 return 0;
4252 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004253
4254 http_end:
4255 /* The situation will not evolve, so let's give up on the analysis. */
4256 s->logs.tv_request = now; /* update the request timer to reflect full request */
4257 req->analysers &= ~an_bit;
4258 req->analyse_exp = TICK_ETERNITY;
4259 return 1;
4260
4261 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004262 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004263 txn->req.msg_state = HTTP_MSG_ERROR;
4264 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004265 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004266
Willy Tarreaue7dff022015-04-03 01:14:29 +02004267 if (!(s->flags & SF_ERR_MASK))
4268 s->flags |= SF_ERR_PRXCOND;
4269 if (!(s->flags & SF_FINST_MASK))
4270 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004271
Willy Tarreau522d6c02009-12-06 18:49:18 +01004272 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004273 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004274 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004275 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004276 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004277 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004278}
4279
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004280/* send a server's name with an outgoing request over an established connection.
4281 * Note: this function is designed to be called once the request has been scheduled
4282 * for being forwarded. This is the reason why it rewinds the buffer before
4283 * proceeding.
4284 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004285int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004286
4287 struct hdr_ctx ctx;
4288
Mark Lamourinec2247f02012-01-04 13:02:01 -05004289 char *hdr_name = be->server_id_hdr_name;
4290 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004291 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004292 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004293 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004294
William Lallemandd9e90662012-01-30 17:27:17 +01004295 ctx.idx = 0;
4296
Willy Tarreau211cdec2014-04-17 20:18:08 +02004297 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004298 if (old_o) {
4299 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004300 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004301 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004302 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004303 }
4304
Willy Tarreauf37954d2018-06-15 18:31:02 +02004305 old_i = ci_data(chn);
4306 while (http_find_header2(hdr_name, hdr_name_len, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004307 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004308 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004309 }
4310
4311 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004312 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004313 memcpy(hdr_val, hdr_name, hdr_name_len);
4314 hdr_val += hdr_name_len;
4315 *hdr_val++ = ':';
4316 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004317 hdr_val += strlcpy2(hdr_val, srv_name,
4318 trash.area + trash.size - hdr_val);
4319 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
4320 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004321
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004322 if (old_o) {
4323 /* If this was a forwarded request, we must readjust the amount of
4324 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004325 * variations. Note that the current state is >= HTTP_MSG_BODY,
4326 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004327 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004328 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004329 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004330 txn->req.next -= old_o;
4331 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004332 }
4333
Mark Lamourinec2247f02012-01-04 13:02:01 -05004334 return 0;
4335}
4336
Willy Tarreau610ecce2010-01-04 21:15:02 +01004337/* Terminate current transaction and prepare a new one. This is very tricky
4338 * right now but it works.
4339 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004340void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004342 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004343 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004344 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004345 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004346 struct connection *srv_conn;
4347 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004348 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004349
Willy Tarreau610ecce2010-01-04 21:15:02 +01004350 /* FIXME: We need a more portable way of releasing a backend's and a
4351 * server's connections. We need a safer way to reinitialize buffer
4352 * flags. We also need a more accurate method for computing per-request
4353 * data.
4354 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004355 /*
4356 * XXX cognet: This is probably wrong, this is killing a whole
4357 * connection, in the new world order, we probably want to just kill
4358 * the stream, this is to be revisited the day we handle multiple
4359 * streams in one server connection.
4360 */
4361 cs = objt_cs(s->si[1].end);
4362 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363
Willy Tarreau4213a112013-12-15 10:25:42 +01004364 /* unless we're doing keep-alive, we want to quickly close the connection
4365 * to the server.
4366 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004367 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004368 !si_conn_ready(&s->si[1])) {
4369 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4370 si_shutr(&s->si[1]);
4371 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004372 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373
Willy Tarreaue7dff022015-04-03 01:14:29 +02004374 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004375 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004376 if (unlikely(s->srv_conn))
4377 sess_change_server(s, NULL);
4378 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004379
4380 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004381 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004382
Willy Tarreaueee5b512015-04-03 23:46:31 +02004383 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004384 int n;
4385
Willy Tarreaueee5b512015-04-03 23:46:31 +02004386 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004387 if (n < 1 || n > 5)
4388 n = 0;
4389
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004390 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004391 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004392 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004393 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004394 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004395 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4396 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004397 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004398 }
4399
4400 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004401 s->logs.bytes_in -= ci_data(&s->req);
4402 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004403
Willy Tarreau66425e32018-07-25 06:55:12 +02004404 /* we may need to know the position in the queue */
4405 pendconn_free(s);
4406
Willy Tarreau610ecce2010-01-04 21:15:02 +01004407 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004408 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004409 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004410 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004411 s->do_log(s);
4412 }
4413
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004414 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004415 stream_stop_content_counters(s);
4416 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004417
Willy Tarreau610ecce2010-01-04 21:15:02 +01004418 s->logs.accept_date = date; /* user-visible date for logging */
4419 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004420 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4421 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004422 tv_zero(&s->logs.tv_request);
4423 s->logs.t_queue = -1;
4424 s->logs.t_connect = -1;
4425 s->logs.t_data = -1;
4426 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04004427 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
4428 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429
Willy Tarreauf37954d2018-06-15 18:31:02 +02004430 s->logs.bytes_in = s->req.total = ci_data(&s->req);
4431 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004432
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004433 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004434 if (s->flags & SF_CURR_SESS) {
4435 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004436 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004437 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004438 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004439 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004440 }
4441
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004442 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004443
Willy Tarreau4213a112013-12-15 10:25:42 +01004444 /* only release our endpoint if we don't intend to reuse the
4445 * connection.
4446 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004447 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004448 !si_conn_ready(&s->si[1])) {
4449 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004450 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004451 }
4452
Willy Tarreau350f4872014-11-28 14:42:25 +01004453 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4454 s->si[1].err_type = SI_ET_NONE;
4455 s->si[1].conn_retries = 0; /* used for logging too */
4456 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004457 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 +01004458 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 +01004459 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 +02004460 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4461 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4462 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004463
Patrick Hemmere3faf022018-08-22 10:02:00 -04004464 hlua_ctx_destroy(s->hlua);
4465 s->hlua = NULL;
4466
Willy Tarreaueee5b512015-04-03 23:46:31 +02004467 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004469 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004470
4471 if (prev_status == 401 || prev_status == 407) {
4472 /* In HTTP keep-alive mode, if we receive a 401, we still have
4473 * a chance of being able to send the visitor again to the same
4474 * server over the same connection. This is required by some
4475 * broken protocols such as NTLM, and anyway whenever there is
4476 * an opportunity for sending the challenge to the proper place,
4477 * it's better to do it (at least it helps with debugging).
4478 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004479 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004480 if (srv_conn)
4481 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004482 }
4483
Willy Tarreau53f96852016-02-02 18:50:47 +01004484 /* Never ever allow to reuse a connection from a non-reuse backend */
4485 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4486 srv_conn->flags |= CO_FL_PRIVATE;
4487
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004488 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004489 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004491 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004492 s->req.flags |= CF_NEVER_WAIT;
4493 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004494 }
4495
Willy Tarreau714ea782015-11-25 20:11:11 +01004496 /* we're removing the analysers, we MUST re-enable events detection.
4497 * We don't enable close on the response channel since it's either
4498 * already closed, or in keep-alive with an idle connection handler.
4499 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004500 channel_auto_read(&s->req);
4501 channel_auto_close(&s->req);
4502 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004503
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004504 /* we're in keep-alive with an idle connection, monitor it if not already done */
4505 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004506 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004507 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004508 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004509 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004510 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004511 else if (prev_flags & TX_NOT_FIRST)
4512 /* note: we check the request, not the connection, but
4513 * this is valid for strategies SAFE and AGGR, and in
4514 * case of ALWS, we don't care anyway.
4515 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004516 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004517 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004518 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004519 }
Christopher Faulete6006242017-03-10 11:52:44 +01004520 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4521 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522}
4523
4524
4525/* This function updates the request state machine according to the response
4526 * state machine and buffer flags. It returns 1 if it changes anything (flag
4527 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4528 * it is only used to find when a request/response couple is complete. Both
4529 * this function and its equivalent should loop until both return zero. It
4530 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4531 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004532int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004534 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004535 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004536 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004537 unsigned int old_state = txn->req.msg_state;
4538
Christopher Faulet4be98032017-07-18 10:48:24 +02004539 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540 return 0;
4541
4542 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004543 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004544 * We can shut the read side unless we want to abort_on_close,
4545 * or we have a POST request. The issue with POST requests is
4546 * that some browsers still send a CRLF after the request, and
4547 * this CRLF must be read so that it does not remain in the kernel
4548 * buffers, otherwise a close could cause an RST on some systems
4549 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004550 * Note that if we're using keep-alive on the client side, we'd
4551 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004552 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004553 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004554 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004555 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4556 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004557 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4558 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004559 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004560 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004561
Willy Tarreau40f151a2012-12-20 12:10:09 +01004562 /* if the server closes the connection, we want to immediately react
4563 * and close the socket to save packets and syscalls.
4564 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004565 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004566
Willy Tarreau7f876a12015-11-18 11:59:55 +01004567 /* In any case we've finished parsing the request so we must
4568 * disable Nagle when sending data because 1) we're not going
4569 * to shut this side, and 2) the server is waiting for us to
4570 * send pending data.
4571 */
4572 chn->flags |= CF_NEVER_WAIT;
4573
Willy Tarreau610ecce2010-01-04 21:15:02 +01004574 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4575 goto wait_other_side;
4576
4577 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4578 /* The server has not finished to respond, so we
4579 * don't want to move in order not to upset it.
4580 */
4581 goto wait_other_side;
4582 }
4583
Willy Tarreau610ecce2010-01-04 21:15:02 +01004584 /* When we get here, it means that both the request and the
4585 * response have finished receiving. Depending on the connection
4586 * mode, we'll have to wait for the last bytes to leave in either
4587 * direction, and sometimes for a close to be effective.
4588 */
4589
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004590 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4591 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004592 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4593 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004594 }
4595 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4596 /* Option forceclose is set, or either side wants to close,
4597 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004598 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004599 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004600 *
4601 * However, there is an exception if the response
4602 * length is undefined. In this case, we need to wait
4603 * the close from the server. The response will be
4604 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004605 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004606 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4607 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4608 goto check_channel_flags;
4609
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004610 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4611 channel_shutr_now(chn);
4612 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004613 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004614 }
4615 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004616 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4617 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004618 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004619 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4620 channel_auto_read(chn);
4621 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004622 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004623 }
4624
Christopher Faulet4be98032017-07-18 10:48:24 +02004625 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 }
4627
4628 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4629 http_msg_closing:
4630 /* nothing else to forward, just waiting for the output buffer
4631 * to be empty and for the shutw_now to take effect.
4632 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004633 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004634 txn->req.msg_state = HTTP_MSG_CLOSED;
4635 goto http_msg_closed;
4636 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004637 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004638 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004640 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004641 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004642 }
4643
4644 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4645 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004646 /* if we don't know whether the server will close, we need to hard close */
4647 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4648 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4649
Willy Tarreau3988d932013-12-27 23:03:08 +01004650 /* see above in MSG_DONE why we only do this in these states */
4651 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4652 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004653 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4654 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004655 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 goto wait_other_side;
4657 }
4658
Christopher Faulet4be98032017-07-18 10:48:24 +02004659 check_channel_flags:
4660 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4661 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4662 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004663 txn->req.msg_state = HTTP_MSG_CLOSING;
4664 goto http_msg_closing;
4665 }
4666
4667
Willy Tarreau610ecce2010-01-04 21:15:02 +01004668 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004669 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004670}
4671
4672
4673/* This function updates the response state machine according to the request
4674 * state machine and buffer flags. It returns 1 if it changes anything (flag
4675 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4676 * it is only used to find when a request/response couple is complete. Both
4677 * this function and its equivalent should loop until both return zero. It
4678 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4679 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004680int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004682 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004683 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004684 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 unsigned int old_state = txn->rsp.msg_state;
4686
Christopher Faulet4be98032017-07-18 10:48:24 +02004687 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004688 return 0;
4689
4690 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4691 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004692 * still monitor the server connection for a possible close
4693 * while the request is being uploaded, so we don't disable
4694 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004695 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004696 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697
4698 if (txn->req.msg_state == HTTP_MSG_ERROR)
4699 goto wait_other_side;
4700
4701 if (txn->req.msg_state < HTTP_MSG_DONE) {
4702 /* The client seems to still be sending data, probably
4703 * because we got an error response during an upload.
4704 * We have the choice of either breaking the connection
4705 * or letting it pass through. Let's do the later.
4706 */
4707 goto wait_other_side;
4708 }
4709
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 /* When we get here, it means that both the request and the
4711 * response have finished receiving. Depending on the connection
4712 * mode, we'll have to wait for the last bytes to leave in either
4713 * direction, and sometimes for a close to be effective.
4714 */
4715
4716 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4717 /* Server-close mode : shut read and wait for the request
4718 * side to close its output buffer. The caller will detect
4719 * when we're in DONE and the other is in CLOSED and will
4720 * catch that for the final cleanup.
4721 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004722 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4723 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004724 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004725 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4726 /* Option forceclose is set, or either side wants to close,
4727 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004728 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004729 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004731 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004732 channel_shutr_now(chn);
4733 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004734 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004735 }
4736 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004737 /* The last possible modes are keep-alive and tunnel. Tunnel will
4738 * need to forward remaining data. Keep-alive will need to monitor
4739 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004740 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004741 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004742 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004743 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4744 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 }
4746
Christopher Faulet4be98032017-07-18 10:48:24 +02004747 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 }
4749
4750 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4751 http_msg_closing:
4752 /* nothing else to forward, just waiting for the output buffer
4753 * to be empty and for the shutw_now to take effect.
4754 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004755 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004756 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4757 goto http_msg_closed;
4758 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004759 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004760 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004761 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004762 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004763 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004764 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004765 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004766 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004767 }
4768
4769 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4770 http_msg_closed:
4771 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004772 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004773 channel_auto_close(chn);
4774 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004775 goto wait_other_side;
4776 }
4777
Christopher Faulet4be98032017-07-18 10:48:24 +02004778 check_channel_flags:
4779 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4780 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4781 /* if we've just closed an output, let's switch */
4782 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4783 goto http_msg_closing;
4784 }
4785
Willy Tarreau610ecce2010-01-04 21:15:02 +01004786 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004787 /* We force the response to leave immediately if we're waiting for the
4788 * other side, since there is no pending shutdown to push it out.
4789 */
4790 if (!channel_is_empty(chn))
4791 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004792 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793}
4794
4795
Christopher Faulet894da4c2017-07-18 11:29:07 +02004796/* Resync the request and response state machines. */
4797void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004799 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004800#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801 int old_req_state = txn->req.msg_state;
4802 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004803#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804
Willy Tarreau610ecce2010-01-04 21:15:02 +01004805 http_sync_req_state(s);
4806 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807 if (!http_sync_res_state(s))
4808 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809 if (!http_sync_req_state(s))
4810 break;
4811 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004812
Christopher Faulet894da4c2017-07-18 11:29:07 +02004813 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4814 "req->analysers=0x%08x res->analysers=0x%08x\n",
4815 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004816 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4817 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004818 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004819
4820
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 /* OK, both state machines agree on a compatible state.
4822 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4824 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004825 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4826 * means we must abort the request.
4827 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4828 * corresponding channel.
4829 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4830 * on the response with server-close mode means we've completed one
4831 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004832 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004833 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4834 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004835 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004836 channel_auto_close(&s->req);
4837 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004838 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004839 channel_auto_close(&s->res);
4840 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004841 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004842 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4843 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004844 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004845 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004846 channel_auto_close(&s->res);
4847 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004848 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004849 channel_abort(&s->req);
4850 channel_auto_close(&s->req);
4851 channel_auto_read(&s->req);
4852 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004853 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004854 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4855 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4856 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4857 s->req.analysers &= AN_REQ_FLT_END;
4858 if (HAS_REQ_DATA_FILTERS(s))
4859 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4860 }
4861 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4862 s->res.analysers &= AN_RES_FLT_END;
4863 if (HAS_RSP_DATA_FILTERS(s))
4864 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4865 }
4866 channel_auto_close(&s->req);
4867 channel_auto_read(&s->req);
4868 channel_auto_close(&s->res);
4869 channel_auto_read(&s->res);
4870 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004871 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4872 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004873 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004874 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4875 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4876 /* server-close/keep-alive: terminate this transaction,
4877 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004878 * a fresh-new transaction, but only once we're sure there's
4879 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004880 * another request. They must not hold any pending output data
4881 * and the response buffer must realigned
4882 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004883 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004884 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004885 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004886 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004887 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004888 else {
4889 s->req.analysers = AN_REQ_FLT_END;
4890 s->res.analysers = AN_RES_FLT_END;
4891 txn->flags |= TX_WAIT_CLEANUP;
4892 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004893 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894}
4895
Willy Tarreaud98cf932009-12-27 22:54:55 +01004896/* This function is an analyser which forwards request body (including chunk
4897 * sizes if any). It is called as soon as we must forward, even if we forward
4898 * zero byte. The only situation where it must not be called is when we're in
4899 * tunnel mode and we want to forward till the close. It's used both to forward
4900 * remaining data and to resync after end of body. It expects the msg_state to
4901 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004902 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004903 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004904 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004905 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004906int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004907{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004908 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004909 struct http_txn *txn = s->txn;
4910 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004911 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004912
Christopher Faulet45073512018-07-20 10:16:29 +02004913 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Christopher Faulet814d2702017-03-30 11:33:44 +02004914 now_ms, __FUNCTION__,
4915 s,
4916 req,
4917 req->rex, req->wex,
4918 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004919 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004920 req->analysers);
4921
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004922 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4923 return 0;
4924
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004925 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004926 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004927 /* Output closed while we were sending data. We must abort and
4928 * wake the other side up.
4929 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004930 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004931 msg->msg_state = HTTP_MSG_ERROR;
4932 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004933 return 1;
4934 }
4935
Willy Tarreaud98cf932009-12-27 22:54:55 +01004936 /* Note that we don't have to send 100-continue back because we don't
4937 * need the data to complete our job, and it's up to the server to
4938 * decide whether to return 100, 417 or anything else in return of
4939 * an "Expect: 100-continue" header.
4940 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004941 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004942 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4943 ? HTTP_MSG_CHUNK_SIZE
4944 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004945
4946 /* TODO/filters: when http-buffer-request option is set or if a
4947 * rule on url_param exists, the first chunk size could be
4948 * already parsed. In that case, msg->next is after the chunk
4949 * size (including the CRLF after the size). So this case should
4950 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004951 }
4952
Willy Tarreau7ba23542014-04-17 21:50:00 +02004953 /* Some post-connect processing might want us to refrain from starting to
4954 * forward data. Currently, the only reason for this is "balance url_param"
4955 * whichs need to parse/process the request after we've enabled forwarding.
4956 */
4957 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004958 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004959 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004960 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004961 channel_dont_close(req); /* don't fail on early shutr */
4962 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004963 }
4964 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4965 }
4966
Willy Tarreau80a92c02014-03-12 10:41:13 +01004967 /* in most states, we should abort in case of early close */
4968 channel_auto_close(req);
4969
Willy Tarreauefdf0942014-04-24 20:08:57 +02004970 if (req->to_forward) {
4971 /* We can't process the buffer's contents yet */
4972 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004973 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004974 }
4975
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004976 if (msg->msg_state < HTTP_MSG_DONE) {
4977 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4978 ? http_msg_forward_chunked_body(s, msg)
4979 : http_msg_forward_body(s, msg));
4980 if (!ret)
4981 goto missing_data_or_waiting;
4982 if (ret < 0)
4983 goto return_bad_req;
4984 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004985
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004986 /* other states, DONE...TUNNEL */
4987 /* we don't want to forward closes on DONE except in tunnel mode. */
4988 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4989 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004990
Christopher Faulet894da4c2017-07-18 11:29:07 +02004991 http_resync_states(s);
4992 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004993 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4994 if (req->flags & CF_SHUTW) {
4995 /* request errors are most likely due to the
4996 * server aborting the transfer. */
4997 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004998 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004999 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005000 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005001 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005002 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005003 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005004 }
5005
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005006 /* If "option abortonclose" is set on the backend, we want to monitor
5007 * the client's connection and forward any shutdown notification to the
5008 * server, which will decide whether to close or to go on processing the
5009 * request. We only do that in tunnel mode, and not in other modes since
5010 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01005011 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005012 channel_auto_read(req);
5013 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5014 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5015 s->si[1].flags |= SI_FL_NOLINGER;
5016 channel_auto_close(req);
5017 }
5018 else if (s->txn->meth == HTTP_METH_POST) {
5019 /* POST requests may require to read extra CRLF sent by broken
5020 * browsers and which could cause an RST to be sent upon close
5021 * on some systems (eg: Linux). */
5022 channel_auto_read(req);
5023 }
5024 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005025
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005026 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005027 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005028 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005029 if (!(s->flags & SF_ERR_MASK))
5030 s->flags |= SF_ERR_CLICL;
5031 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005032 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005033 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005034 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005035 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005036 }
5037
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005038 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5039 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005040 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005041 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005042
5043 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005044 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005045
Willy Tarreauba20dfc2018-05-16 11:35:05 +02005046 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005047 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005048 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005049 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005050
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005051 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005052 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01005053 * And when content-length is used, we never want to let the possible
5054 * shutdown be forwarded to the other side, as the state machine will
5055 * take care of it once the client responds. It's also important to
5056 * prevent TIME_WAITs from accumulating on the backend side, and for
5057 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005058 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01005059 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005060 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005061
Willy Tarreau5c620922011-05-11 19:56:11 +02005062 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005063 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005064 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005065 * modes are already handled by the stream sock layer. We must not do
5066 * this in content-length mode because it could present the MSG_MORE
5067 * flag with the last block of forwarded data, which would cause an
5068 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005069 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005070 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005071 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005072
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005073 return 0;
5074
Willy Tarreaud98cf932009-12-27 22:54:55 +01005075 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005076 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005077 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005078 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02005079
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005080 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005081 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005082 txn->req.msg_state = HTTP_MSG_ERROR;
5083 if (txn->status) {
5084 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005085 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005086 } else {
5087 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005088 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005089 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005090 req->analysers &= AN_REQ_FLT_END;
5091 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 +01005092
Willy Tarreaue7dff022015-04-03 01:14:29 +02005093 if (!(s->flags & SF_ERR_MASK))
5094 s->flags |= SF_ERR_PRXCOND;
5095 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005096 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005097 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005098 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005099 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005100 }
5101 return 0;
5102
5103 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005104 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005105 txn->req.msg_state = HTTP_MSG_ERROR;
5106 if (txn->status) {
5107 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005108 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005109 } else {
5110 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005111 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005112 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005113 req->analysers &= AN_REQ_FLT_END;
5114 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 +01005115
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005116 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5117 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005118 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005119 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005120
Willy Tarreaue7dff022015-04-03 01:14:29 +02005121 if (!(s->flags & SF_ERR_MASK))
5122 s->flags |= SF_ERR_SRVCL;
5123 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005124 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005125 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005126 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005127 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005128 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005129 return 0;
5130}
5131
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5133 * processing can continue on next analysers, or zero if it either needs more
5134 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005135 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005136 * when it has nothing left to do, and may remove any analyser when it wants to
5137 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005138 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005139int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005140{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005141 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005142 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005144 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005145 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005147 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005148
Christopher Faulet45073512018-07-20 10:16:29 +02005149 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005150 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005152 rep,
5153 rep->rex, rep->wex,
5154 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005155 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005156 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005157
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005158 /*
5159 * Now parse the partial (or complete) lines.
5160 * We will check the response syntax, and also join multi-line
5161 * headers. An index of all the lines will be elaborated while
5162 * parsing.
5163 *
5164 * For the parsing, we use a 28 states FSM.
5165 *
5166 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02005167 * ci_head(rep) = beginning of response
5168 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
5169 * ci_tail(rep) = end of input data
5170 * msg->eol = end of current header or line (LF or CRLF)
5171 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005172 */
5173
Willy Tarreau628c40c2014-04-24 19:11:26 +02005174 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005175 /* There's a protected area at the end of the buffer for rewriting
5176 * purposes. We don't want to start to parse the request if the
5177 * protected area is affected, because we may have to move processed
5178 * data later, which is much more complicated.
5179 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02005180 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005181 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005182 /* some data has still not left the buffer, wake us once that's done */
5183 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5184 goto abort_response;
5185 channel_dont_close(rep);
5186 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005187 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005188 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005189 }
5190
Willy Tarreau188e2302018-06-15 11:11:53 +02005191 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005192 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005193 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02005194
Willy Tarreauf37954d2018-06-15 18:31:02 +02005195 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01005196 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005197 }
5198
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005199 /* 1: we might have to print this header in debug mode */
5200 if (unlikely((global.mode & MODE_DEBUG) &&
5201 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005202 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005203 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005204
Willy Tarreauf37954d2018-06-15 18:31:02 +02005205 sol = ci_head(rep);
5206 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005207 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209 sol += hdr_idx_first_pos(&txn->hdr_idx);
5210 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005211
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005212 while (cur_idx) {
5213 eol = sol + txn->hdr_idx.v[cur_idx].len;
5214 debug_hdr("srvhdr", s, sol, eol);
5215 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5216 cur_idx = txn->hdr_idx.v[cur_idx].next;
5217 }
5218 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005219
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220 /*
5221 * Now we quickly check if we have found a full valid response.
5222 * If not so, we check the FD and buffer states before leaving.
5223 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005224 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225 * responses are checked first.
5226 *
5227 * Depending on whether the client is still there or not, we
5228 * may send an error response back or not. Note that normally
5229 * we should only check for HTTP status there, and check I/O
5230 * errors somewhere else.
5231 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232
Willy Tarreau655dce92009-11-08 13:10:58 +01005233 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005234 /* Invalid response */
5235 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5236 /* we detected a parsing error. We want to archive this response
5237 * in the dedicated proxy area for later troubleshooting.
5238 */
5239 hdr_response_bad:
5240 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005241 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005242
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005243 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005244 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005245 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005246 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005247 }
Willy Tarreau64648412010-03-05 10:41:54 +01005248 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005249 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005250 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005251 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005252 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005253 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005254 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255
Willy Tarreaue7dff022015-04-03 01:14:29 +02005256 if (!(s->flags & SF_ERR_MASK))
5257 s->flags |= SF_ERR_PRXCOND;
5258 if (!(s->flags & SF_FINST_MASK))
5259 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005260
5261 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005262 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005263
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02005265 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005266 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02005267 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268 goto hdr_response_bad;
5269 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005270
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005271 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005272 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005273 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005274 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005275 else if (txn->flags & TX_NOT_FIRST)
5276 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005277
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005278 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005279 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005280 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005281 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005282 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005283
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005284 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005285 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005286 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005287
5288 /* Check to see if the server refused the early data.
5289 * If so, just send a 425
5290 */
5291 if (objt_cs(s->si[1].end)) {
5292 struct connection *conn = objt_cs(s->si[1].end)->conn;
5293
5294 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5295 txn->status = 425;
5296 }
5297
Willy Tarreau350f4872014-11-28 14:42:25 +01005298 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005299 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005300 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005301
Willy Tarreaue7dff022015-04-03 01:14:29 +02005302 if (!(s->flags & SF_ERR_MASK))
5303 s->flags |= SF_ERR_SRVCL;
5304 if (!(s->flags & SF_FINST_MASK))
5305 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005306 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005307 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005308
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005309 /* read timeout : return a 504 to the client. */
5310 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005311 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005312 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005313
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005314 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005315 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005316 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005317 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005318 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005319
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005320 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005321 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005322 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005323 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005324 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005325 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005326
Willy Tarreaue7dff022015-04-03 01:14:29 +02005327 if (!(s->flags & SF_ERR_MASK))
5328 s->flags |= SF_ERR_SRVTO;
5329 if (!(s->flags & SF_FINST_MASK))
5330 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005331 return 0;
5332 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005333
Willy Tarreauf003d372012-11-26 13:35:37 +01005334 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005335 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005336 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5337 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005338 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005339 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005340
Christopher Faulet0184ea72017-01-05 14:06:34 +01005341 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005342 channel_auto_close(rep);
5343
5344 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005345 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005346 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005347
Willy Tarreaue7dff022015-04-03 01:14:29 +02005348 if (!(s->flags & SF_ERR_MASK))
5349 s->flags |= SF_ERR_CLICL;
5350 if (!(s->flags & SF_FINST_MASK))
5351 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005352
Willy Tarreau87b09662015-04-03 00:22:06 +02005353 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005354 return 0;
5355 }
5356
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005357 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005358 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005359 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005360 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005361 else if (txn->flags & TX_NOT_FIRST)
5362 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005363
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005364 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005365 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005366 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005367 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005368 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005369
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005370 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005371 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005372 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005373 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005374 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005375 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005376
Willy Tarreaue7dff022015-04-03 01:14:29 +02005377 if (!(s->flags & SF_ERR_MASK))
5378 s->flags |= SF_ERR_SRVCL;
5379 if (!(s->flags & SF_FINST_MASK))
5380 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005381 return 0;
5382 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005383
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005384 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005385 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005386 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005387 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005388 else if (txn->flags & TX_NOT_FIRST)
5389 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005390
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005391 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005392 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005393 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005394
Willy Tarreaue7dff022015-04-03 01:14:29 +02005395 if (!(s->flags & SF_ERR_MASK))
5396 s->flags |= SF_ERR_CLICL;
5397 if (!(s->flags & SF_FINST_MASK))
5398 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005399
Willy Tarreau87b09662015-04-03 00:22:06 +02005400 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005401 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005402 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005403
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005404 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005405 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005406 return 0;
5407 }
5408
5409 /* More interesting part now : we know that we have a complete
5410 * response which at least looks like HTTP. We have an indicator
5411 * of each header's length, so we can parse them quickly.
5412 */
5413
5414 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005415 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005416
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005417 /*
5418 * 1: get the status code
5419 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005420 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005421 if (n < 1 || n > 5)
5422 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005423 /* when the client triggers a 4xx from the server, it's most often due
5424 * to a missing object or permission. These events should be tracked
5425 * because if they happen often, it may indicate a brute force or a
5426 * vulnerability scan.
5427 */
5428 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005429 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005430
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005431 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005432 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433
Willy Tarreau91852eb2015-05-01 13:26:00 +02005434 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5435 * exactly one digit "." one digit. This check may be disabled using
5436 * option accept-invalid-http-response.
5437 */
5438 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5439 if (msg->sl.st.v_l != 8) {
5440 msg->err_pos = 0;
5441 goto hdr_response_bad;
5442 }
5443
Willy Tarreauf37954d2018-06-15 18:31:02 +02005444 if (ci_head(rep)[4] != '/' ||
5445 !isdigit((unsigned char)ci_head(rep)[5]) ||
5446 ci_head(rep)[6] != '.' ||
5447 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02005448 msg->err_pos = 4;
5449 goto hdr_response_bad;
5450 }
5451 }
5452
Willy Tarreau5b154472009-12-21 20:11:07 +01005453 /* check if the response is HTTP/1.1 or above */
5454 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02005455 ((ci_head(rep)[5] > '1') ||
5456 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005457 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005458
5459 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005460 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 +01005461
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005462 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005463 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005464
Willy Tarreauf37954d2018-06-15 18:31:02 +02005465 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005466
Willy Tarreau39650402010-03-15 19:44:39 +01005467 /* Adjust server's health based on status code. Note: status codes 501
5468 * and 505 are triggered on demand by client request, so we must not
5469 * count them as server failures.
5470 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005471 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005472 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005473 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005474 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005475 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005476 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005477
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005478 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005479 * We may be facing a 100-continue response, or any other informational
5480 * 1xx response which is non-final, in which case this is not the right
5481 * response, and we're waiting for the next one. Let's allow this response
5482 * to go to the client and wait for the next one. There's an exception for
5483 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005484 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005485 if (txn->status < 200 &&
5486 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005487 hdr_idx_init(&txn->hdr_idx);
5488 msg->next -= channel_forward(rep, msg->next);
5489 msg->msg_state = HTTP_MSG_RPBEFORE;
5490 txn->status = 0;
5491 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005492 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005493 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005494 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005495
Willy Tarreaua14ad722017-07-07 11:36:32 +02005496 /*
5497 * 2: check for cacheability.
5498 */
5499
5500 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005501 case 200:
5502 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005503 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005504 case 206:
5505 case 300:
5506 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005507 case 404:
5508 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005509 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005510 case 414:
5511 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005512 break;
5513 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005514 /* RFC7231#6.1:
5515 * Responses with status codes that are defined as
5516 * cacheable by default (e.g., 200, 203, 204, 206,
5517 * 300, 301, 404, 405, 410, 414, and 501 in this
5518 * specification) can be reused by a cache with
5519 * heuristic expiration unless otherwise indicated
5520 * by the method definition or explicit cache
5521 * controls [RFC7234]; all other status codes are
5522 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005523 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005524 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005525 break;
5526 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005527
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 /*
5529 * 3: we may need to capture headers
5530 */
5531 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005532 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02005533 capture_headers(ci_head(rep), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005534 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005535
Willy Tarreau557f1992015-05-01 10:05:17 +02005536 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5537 * by RFC7230#3.3.3 :
5538 *
5539 * The length of a message body is determined by one of the following
5540 * (in order of precedence):
5541 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005542 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5543 * the connection will become a tunnel immediately after the empty
5544 * line that concludes the header fields. A client MUST ignore
5545 * any Content-Length or Transfer-Encoding header fields received
5546 * in such a message. Any 101 response (Switching Protocols) is
5547 * managed in the same manner.
5548 *
5549 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005550 * (Informational), 204 (No Content), or 304 (Not Modified) status
5551 * code is always terminated by the first empty line after the
5552 * header fields, regardless of the header fields present in the
5553 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005554 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005555 * 3. If a Transfer-Encoding header field is present and the chunked
5556 * transfer coding (Section 4.1) is the final encoding, the message
5557 * body length is determined by reading and decoding the chunked
5558 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005559 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005560 * If a Transfer-Encoding header field is present in a response and
5561 * the chunked transfer coding is not the final encoding, the
5562 * message body length is determined by reading the connection until
5563 * it is closed by the server. If a Transfer-Encoding header field
5564 * is present in a request and the chunked transfer coding is not
5565 * the final encoding, the message body length cannot be determined
5566 * reliably; the server MUST respond with the 400 (Bad Request)
5567 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005568 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005569 * If a message is received with both a Transfer-Encoding and a
5570 * Content-Length header field, the Transfer-Encoding overrides the
5571 * Content-Length. Such a message might indicate an attempt to
5572 * perform request smuggling (Section 9.5) or response splitting
5573 * (Section 9.4) and ought to be handled as an error. A sender MUST
5574 * remove the received Content-Length field prior to forwarding such
5575 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005576 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005577 * 4. If a message is received without Transfer-Encoding and with
5578 * either multiple Content-Length header fields having differing
5579 * field-values or a single Content-Length header field having an
5580 * invalid value, then the message framing is invalid and the
5581 * recipient MUST treat it as an unrecoverable error. If this is a
5582 * request message, the server MUST respond with a 400 (Bad Request)
5583 * status code and then close the connection. If this is a response
5584 * message received by a proxy, the proxy MUST close the connection
5585 * to the server, discard the received response, and send a 502 (Bad
5586 * Gateway) response to the client. If this is a response message
5587 * received by a user agent, the user agent MUST close the
5588 * connection to the server and discard the received response.
5589 *
5590 * 5. If a valid Content-Length header field is present without
5591 * Transfer-Encoding, its decimal value defines the expected message
5592 * body length in octets. If the sender closes the connection or
5593 * the recipient times out before the indicated number of octets are
5594 * received, the recipient MUST consider the message to be
5595 * incomplete and close the connection.
5596 *
5597 * 6. If this is a request message and none of the above are true, then
5598 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005599 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005600 * 7. Otherwise, this is a response message without a declared message
5601 * body length, so the message body length is determined by the
5602 * number of octets received prior to the server closing the
5603 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005604 */
5605
5606 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005607 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005608 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005609 * FIXME: should we parse anyway and return an error on chunked encoding ?
5610 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005611 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5612 txn->status == 101)) {
5613 /* Either we've established an explicit tunnel, or we're
5614 * switching the protocol. In both cases, we're very unlikely
5615 * to understand the next protocols. We have to switch to tunnel
5616 * mode, so that we transfer the request and responses then let
5617 * this protocol pass unmodified. When we later implement specific
5618 * parsers for such protocols, we'll want to check the Upgrade
5619 * header which contains information about that protocol for
5620 * responses with status 101 (eg: see RFC2817 about TLS).
5621 */
5622 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5623 msg->flags |= HTTP_MSGF_XFER_LEN;
5624 goto end;
5625 }
5626
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005627 if (txn->meth == HTTP_METH_HEAD ||
5628 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005629 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005630 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005631 goto skip_content_length;
5632 }
5633
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005634 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005635 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005636 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005637 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005638 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5639 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005640 /* bad transfer-encoding (chunked followed by something else) */
5641 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005642 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005643 break;
5644 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005645 }
5646
Willy Tarreau1c913912015-04-30 10:57:51 +02005647 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005648 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005649 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005650 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005651 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5652 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005653 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005654 signed long long cl;
5655
Willy Tarreauad14f752011-09-02 20:33:27 +02005656 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005657 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005658 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005659 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005660
Willy Tarreauad14f752011-09-02 20:33:27 +02005661 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005662 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005663 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005664 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005665
Willy Tarreauad14f752011-09-02 20:33:27 +02005666 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005667 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005668 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005669 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005670
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005671 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005672 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005673 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005674 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005675
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005676 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005677 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005678 }
5679
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005680 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005681 /* Now we have to check if we need to modify the Connection header.
5682 * This is more difficult on the response than it is on the request,
5683 * because we can have two different HTTP versions and we don't know
5684 * how the client will interprete a response. For instance, let's say
5685 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5686 * HTTP/1.1 response without any header. Maybe it will bound itself to
5687 * HTTP/1.0 because it only knows about it, and will consider the lack
5688 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5689 * the lack of header as a keep-alive. Thus we will use two flags
5690 * indicating how a request MAY be understood by the client. In case
5691 * of multiple possibilities, we'll fix the header to be explicit. If
5692 * ambiguous cases such as both close and keepalive are seen, then we
5693 * will fall back to explicit close. Note that we won't take risks with
5694 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005695 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005696 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005697 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5698 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5699 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5700 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005701 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005702
Willy Tarreau70dffda2014-01-30 03:07:23 +01005703 /* this situation happens when combining pretend-keepalive with httpclose. */
5704 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005705 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005706 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005707 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5708
Willy Tarreau60466522010-01-18 19:08:45 +01005709 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005710 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005711 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5712 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005713
Willy Tarreau60466522010-01-18 19:08:45 +01005714 /* now adjust header transformations depending on current state */
5715 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5716 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5717 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005718 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005719 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005720 }
Willy Tarreau60466522010-01-18 19:08:45 +01005721 else { /* SCL / KAL */
5722 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005723 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005724 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005725 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005726
Willy Tarreau60466522010-01-18 19:08:45 +01005727 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005728 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005729
Willy Tarreau60466522010-01-18 19:08:45 +01005730 /* Some keep-alive responses are converted to Server-close if
5731 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005732 */
Willy Tarreau60466522010-01-18 19:08:45 +01005733 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5734 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005735 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005736 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005737 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005738 }
5739
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005740 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005741 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005742 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005743
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005744 /* end of job, return OK */
5745 rep->analysers &= ~an_bit;
5746 rep->analyse_exp = TICK_ETERNITY;
5747 channel_auto_close(rep);
5748 return 1;
5749
5750 abort_keep_alive:
5751 /* A keep-alive request to the server failed on a network error.
5752 * The client is required to retry. We need to close without returning
5753 * any other information so that the client retries.
5754 */
5755 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005756 rep->analysers &= AN_RES_FLT_END;
5757 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005758 channel_auto_close(rep);
5759 s->logs.logwait = 0;
5760 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005761 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005762 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005763 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005764 return 0;
5765}
5766
5767/* This function performs all the processing enabled for the current response.
5768 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005769 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005770 * other functions. It works like process_request (see indications above).
5771 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005772int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005773{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005774 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005775 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005776 struct http_msg *msg = &txn->rsp;
5777 struct proxy *cur_proxy;
5778 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005779 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005780
Christopher Faulet45073512018-07-20 10:16:29 +02005781 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005782 now_ms, __FUNCTION__,
5783 s,
5784 rep,
5785 rep->rex, rep->wex,
5786 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005787 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005788 rep->analysers);
5789
5790 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5791 return 0;
5792
Willy Tarreau70730dd2014-04-24 18:06:27 +02005793 /* The stats applet needs to adjust the Connection header but we don't
5794 * apply any filter there.
5795 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005796 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5797 rep->analysers &= ~an_bit;
5798 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005799 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005800 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005801
Willy Tarreau58975672014-04-24 21:13:57 +02005802 /*
5803 * We will have to evaluate the filters.
5804 * As opposed to version 1.2, now they will be evaluated in the
5805 * filters order and not in the header order. This means that
5806 * each filter has to be validated among all headers.
5807 *
5808 * Filters are tried with ->be first, then with ->fe if it is
5809 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005810 *
5811 * Maybe we are in resume condiion. In this case I choose the
5812 * "struct proxy" which contains the rule list matching the resume
5813 * pointer. If none of theses "struct proxy" match, I initialise
5814 * the process with the first one.
5815 *
5816 * In fact, I check only correspondance betwwen the current list
5817 * pointer and the ->fe rule list. If it doesn't match, I initialize
5818 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005819 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005820 if (s->current_rule_list == &sess->fe->http_res_rules)
5821 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005822 else
5823 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005824 while (1) {
5825 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005826
Willy Tarreau58975672014-04-24 21:13:57 +02005827 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005828 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005829 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005830
Willy Tarreau51d861a2015-05-22 17:30:48 +02005831 if (ret == HTTP_RULE_RES_BADREQ)
5832 goto return_srv_prx_502;
5833
5834 if (ret == HTTP_RULE_RES_DONE) {
5835 rep->analysers &= ~an_bit;
5836 rep->analyse_exp = TICK_ETERNITY;
5837 return 1;
5838 }
5839 }
5840
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005841 /* we need to be called again. */
5842 if (ret == HTTP_RULE_RES_YIELD) {
5843 channel_dont_close(rep);
5844 return 0;
5845 }
5846
Willy Tarreau58975672014-04-24 21:13:57 +02005847 /* try headers filters */
5848 if (rule_set->rsp_exp != NULL) {
5849 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5850 return_bad_resp:
5851 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005852 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005853 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005854 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005855 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005856 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005857 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005858 txn->status = 502;
5859 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005860 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005861 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005862 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005863 if (!(s->flags & SF_ERR_MASK))
5864 s->flags |= SF_ERR_PRXCOND;
5865 if (!(s->flags & SF_FINST_MASK))
5866 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005867 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005868 }
Willy Tarreau58975672014-04-24 21:13:57 +02005869 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005870
Willy Tarreau58975672014-04-24 21:13:57 +02005871 /* has the response been denied ? */
5872 if (txn->flags & TX_SVDENY) {
5873 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005874 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005875
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005876 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5877 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005878 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005879 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005880
Willy Tarreau58975672014-04-24 21:13:57 +02005881 goto return_srv_prx_502;
5882 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005883
Willy Tarreau58975672014-04-24 21:13:57 +02005884 /* add response headers from the rule sets in the same order */
5885 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005886 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005887 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005888 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005889 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005890 ret = acl_pass(ret);
5891 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5892 ret = !ret;
5893 if (!ret)
5894 continue;
5895 }
5896 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5897 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005898 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005899
Willy Tarreau58975672014-04-24 21:13:57 +02005900 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005901 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005902 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005903 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005904 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005905
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005906 /* After this point, this anayzer can't return yield, so we can
5907 * remove the bit corresponding to this analyzer from the list.
5908 *
5909 * Note that the intermediate returns and goto found previously
5910 * reset the analyzers.
5911 */
5912 rep->analysers &= ~an_bit;
5913 rep->analyse_exp = TICK_ETERNITY;
5914
Willy Tarreau58975672014-04-24 21:13:57 +02005915 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005916 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005917 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005918
Willy Tarreau58975672014-04-24 21:13:57 +02005919 /*
5920 * Now check for a server cookie.
5921 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005922 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005923 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005924
Willy Tarreau58975672014-04-24 21:13:57 +02005925 /*
5926 * Check for cache-control or pragma headers if required.
5927 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005928 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005929 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005930
Willy Tarreau58975672014-04-24 21:13:57 +02005931 /*
5932 * Add server cookie in the response if needed
5933 */
5934 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5935 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005936 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005937 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5938 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5939 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5940 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5941 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005942 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005943 /* the server is known, it's not the one the client requested, or the
5944 * cookie's last seen date needs to be refreshed. We have to
5945 * insert a set-cookie here, except if we want to insert only on POST
5946 * requests and this one isn't. Note that servers which don't have cookies
5947 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948 */
Willy Tarreau58975672014-04-24 21:13:57 +02005949 if (!objt_server(s->target)->cookie) {
5950 chunk_printf(&trash,
5951 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5952 s->be->cookie_name);
5953 }
5954 else {
5955 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005956
Willy Tarreau58975672014-04-24 21:13:57 +02005957 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5958 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005959 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5960 s30tob64((date.tv_sec+3) >> 2,
5961 trash.area + trash.data);
5962 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005963
Willy Tarreau58975672014-04-24 21:13:57 +02005964 if (s->be->cookie_maxlife) {
5965 /* emit first_date, which is either the original one or
5966 * the current date.
5967 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005968 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005969 s30tob64(txn->cookie_first_date ?
5970 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005971 (date.tv_sec+3) >> 2,
5972 trash.area + trash.data);
5973 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005974 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005975 }
Willy Tarreau58975672014-04-24 21:13:57 +02005976 chunk_appendf(&trash, "; path=/");
5977 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005978
Willy Tarreau58975672014-04-24 21:13:57 +02005979 if (s->be->cookie_domain)
5980 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005981
Willy Tarreau58975672014-04-24 21:13:57 +02005982 if (s->be->ck_opts & PR_CK_HTTPONLY)
5983 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005984
Willy Tarreau58975672014-04-24 21:13:57 +02005985 if (s->be->ck_opts & PR_CK_SECURE)
5986 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005987
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005988 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005989 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005990
Willy Tarreau58975672014-04-24 21:13:57 +02005991 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005992 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005993 /* the server did not change, only the date was updated */
5994 txn->flags |= TX_SCK_UPDATED;
5995 else
5996 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005997
Willy Tarreau58975672014-04-24 21:13:57 +02005998 /* Here, we will tell an eventual cache on the client side that we don't
5999 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6000 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6001 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006002 */
Willy Tarreau58975672014-04-24 21:13:57 +02006003 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006004
Willy Tarreau58975672014-04-24 21:13:57 +02006005 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006006
Willy Tarreau58975672014-04-24 21:13:57 +02006007 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6008 "Cache-control: private", 22) < 0))
6009 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006010 }
Willy Tarreau58975672014-04-24 21:13:57 +02006011 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006012
Willy Tarreau58975672014-04-24 21:13:57 +02006013 /*
6014 * Check if result will be cacheable with a cookie.
6015 * We'll block the response if security checks have caught
6016 * nasty things such as a cacheable cookie.
6017 */
6018 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6019 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6020 (s->be->options & PR_O_CHK_CACHE)) {
6021 /* we're in presence of a cacheable response containing
6022 * a set-cookie header. We'll block it as requested by
6023 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006024 */
Willy Tarreau58975672014-04-24 21:13:57 +02006025 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006026 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01006027
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006028 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
6029 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006030 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02006031 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006032
Christopher Faulet767a84b2017-11-24 16:50:31 +01006033 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6034 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02006035 send_log(s->be, LOG_ALERT,
6036 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6037 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6038 goto return_srv_prx_502;
6039 }
Willy Tarreau03945942009-12-22 16:50:27 +01006040
Willy Tarreau70730dd2014-04-24 18:06:27 +02006041 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006042 /*
6043 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6044 * If an "Upgrade" token is found, the header is left untouched in order
6045 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006046 * if anything but "Upgrade" is present in the Connection header. We don't
6047 * want to touch any 101 response either since it's switching to another
6048 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006049 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006050 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006051 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006052 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006053 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6054 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006055
Willy Tarreau58975672014-04-24 21:13:57 +02006056 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6057 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6058 /* we want a keep-alive response here. Keep-alive header
6059 * required if either side is not 1.1.
6060 */
6061 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6062 want_flags |= TX_CON_KAL_SET;
6063 }
6064 else {
6065 /* we want a close response here. Close header required if
6066 * the server is 1.1, regardless of the client.
6067 */
6068 if (msg->flags & HTTP_MSGF_VER_11)
6069 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006070 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006071
Willy Tarreau58975672014-04-24 21:13:57 +02006072 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6073 http_change_connection_header(txn, msg, want_flags);
6074 }
6075
6076 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006077 /* Always enter in the body analyzer */
6078 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6079 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006080
Willy Tarreau58975672014-04-24 21:13:57 +02006081 /* if the user wants to log as soon as possible, without counting
6082 * bytes from the server, then this is the right moment. We have
6083 * to temporarily assign bytes_out to log what we currently have.
6084 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006085 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006086 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6087 s->logs.bytes_out = txn->rsp.eoh;
6088 s->do_log(s);
6089 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006090 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006091 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006092}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006093
Willy Tarreaud98cf932009-12-27 22:54:55 +01006094/* This function is an analyser which forwards response body (including chunk
6095 * sizes if any). It is called as soon as we must forward, even if we forward
6096 * zero byte. The only situation where it must not be called is when we're in
6097 * tunnel mode and we want to forward till the close. It's used both to forward
6098 * remaining data and to resync after end of body. It expects the msg_state to
6099 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006100 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006101 *
6102 * It is capable of compressing response data both in content-length mode and
6103 * in chunked mode. The state machines follows different flows depending on
6104 * whether content-length and chunked modes are used, since there are no
6105 * trailers in content-length :
6106 *
6107 * chk-mode cl-mode
6108 * ,----- BODY -----.
6109 * / \
6110 * V size > 0 V chk-mode
6111 * .--> SIZE -------------> DATA -------------> CRLF
6112 * | | size == 0 | last byte |
6113 * | v final crlf v inspected |
6114 * | TRAILERS -----------> DONE |
6115 * | |
6116 * `----------------------------------------------'
6117 *
6118 * Compression only happens in the DATA state, and must be flushed in final
6119 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6120 * is performed at once on final states for all bytes parsed, or when leaving
6121 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006122 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006123int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006124{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006125 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006126 struct http_txn *txn = s->txn;
6127 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006128 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006129
Christopher Faulet45073512018-07-20 10:16:29 +02006130 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Christopher Faulet814d2702017-03-30 11:33:44 +02006131 now_ms, __FUNCTION__,
6132 s,
6133 res,
6134 res->rex, res->wex,
6135 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02006136 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02006137 res->analysers);
6138
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006139 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6140 return 0;
6141
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006142 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02006143 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006144 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006145 /* Output closed while we were sending data. We must abort and
6146 * wake the other side up.
6147 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006148 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006149 msg->msg_state = HTTP_MSG_ERROR;
6150 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006151 return 1;
6152 }
6153
Willy Tarreau4fe41902010-06-07 22:27:41 +02006154 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006155 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006156
Christopher Fauletd7c91962015-04-30 11:48:27 +02006157 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006158 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6159 ? HTTP_MSG_CHUNK_SIZE
6160 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006161 }
6162
Willy Tarreauefdf0942014-04-24 20:08:57 +02006163 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006164 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006165 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006166 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006167 }
6168
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006169 if (msg->msg_state < HTTP_MSG_DONE) {
6170 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6171 ? http_msg_forward_chunked_body(s, msg)
6172 : http_msg_forward_body(s, msg));
6173 if (!ret)
6174 goto missing_data_or_waiting;
6175 if (ret < 0)
6176 goto return_bad_res;
6177 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006178
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006179 /* other states, DONE...TUNNEL */
6180 /* for keep-alive we don't want to forward closes on DONE */
6181 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6182 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6183 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006184
Christopher Faulet894da4c2017-07-18 11:29:07 +02006185 http_resync_states(s);
6186 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006187 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6188 if (res->flags & CF_SHUTW) {
6189 /* response errors are most likely due to the
6190 * client aborting the transfer. */
6191 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006192 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006193 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006194 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 +01006195 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006196 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006197 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006198 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006199 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006200
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006201 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006202 if (res->flags & CF_SHUTW)
6203 goto aborted_xfer;
6204
6205 /* stop waiting for data if the input is closed before the end. If the
6206 * client side was already closed, it means that the client has aborted,
6207 * so we don't want to count this as a server abort. Otherwise it's a
6208 * server abort.
6209 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006210 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006211 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006212 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006213 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02006214 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006215 if (!(s->flags & SF_ERR_MASK))
6216 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006217 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006218 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006219 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006220 goto return_bad_res_stats_ok;
6221 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006222 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006223
Willy Tarreau40dba092010-03-04 18:14:51 +01006224 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006225 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006226 goto return_bad_res;
6227
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006228 /* When TE: chunked is used, we need to get there again to parse
6229 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006230 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6231 * or if there are filters registered on the stream, we don't want to
6232 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006233 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006234 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006235 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006236 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6237 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006238 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006239
Willy Tarreau5c620922011-05-11 19:56:11 +02006240 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006241 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006242 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006243 * modes are already handled by the stream sock layer. We must not do
6244 * this in content-length mode because it could present the MSG_MORE
6245 * flag with the last block of forwarded data, which would cause an
6246 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006247 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006248 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006249 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006250
Willy Tarreau87b09662015-04-03 00:22:06 +02006251 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006252 return 0;
6253
Willy Tarreau40dba092010-03-04 18:14:51 +01006254 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006255 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006256 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006257 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006258
6259 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006260 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006261 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006262 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006263 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006264 res->analysers &= AN_RES_FLT_END;
6265 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 +01006266 if (objt_server(s->target))
6267 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006268
Willy Tarreaue7dff022015-04-03 01:14:29 +02006269 if (!(s->flags & SF_ERR_MASK))
6270 s->flags |= SF_ERR_PRXCOND;
6271 if (!(s->flags & SF_FINST_MASK))
6272 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006273 return 0;
6274
6275 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006276 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006277 txn->rsp.msg_state = HTTP_MSG_ERROR;
6278 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006279 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006280 res->analysers &= AN_RES_FLT_END;
6281 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 +01006282
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006283 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6284 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006285 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006286 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006287
Willy Tarreaue7dff022015-04-03 01:14:29 +02006288 if (!(s->flags & SF_ERR_MASK))
6289 s->flags |= SF_ERR_CLICL;
6290 if (!(s->flags & SF_FINST_MASK))
6291 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006292 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006293}
6294
6295
6296static inline int
6297http_msg_forward_body(struct stream *s, struct http_msg *msg)
6298{
6299 struct channel *chn = msg->chn;
6300 int ret;
6301
6302 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6303
6304 if (msg->msg_state == HTTP_MSG_ENDING)
6305 goto ending;
6306
6307 /* Neither content-length, nor transfer-encoding was found, so we must
6308 * read the body until the server connection is closed. In that case, we
6309 * eat data as they come. Of course, this happens for response only. */
6310 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006311 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006312 msg->chunk_len += len;
6313 msg->body_len += len;
6314 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006315 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006316 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006317 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006318 msg->next += ret;
6319 msg->chunk_len -= ret;
6320 if (msg->chunk_len) {
6321 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006322 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006323 chn->flags |= CF_WAKE_WRITE;
6324 goto missing_data_or_waiting;
6325 }
6326
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006327 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6328 * always set for a request. */
6329 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6330 /* The server still sending data that should be filtered */
6331 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6332 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006333 msg->msg_state = HTTP_MSG_TUNNEL;
6334 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006335 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006336
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006337 msg->msg_state = HTTP_MSG_ENDING;
6338
6339 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006340 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006341 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006342 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6343 /* default_ret */ msg->next,
6344 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006345 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006346 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006347 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6348 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006349 if (msg->next)
6350 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006351
Christopher Fauletda02e172015-12-04 09:25:05 +01006352 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6353 /* default_ret */ 1,
6354 /* on_error */ goto error,
6355 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006356 if (msg->msg_state == HTTP_MSG_ENDING)
6357 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006358 return 1;
6359
6360 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006361 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006362 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6363 /* default_ret */ msg->next,
6364 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006365 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006366 msg->next -= ret;
6367 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6368 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006369 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006370 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006371 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006372 return 0;
6373 error:
6374 return -1;
6375}
6376
6377static inline int
6378http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6379{
6380 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006381 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006382 int ret;
6383
6384 /* Here we have the guarantee to be in one of the following state:
6385 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6386 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6387
6388 switch_states:
6389 switch (msg->msg_state) {
6390 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006391 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006392 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006393 /* on_error */ goto error);
6394 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006395 msg->chunk_len -= ret;
6396 if (msg->chunk_len) {
6397 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006398 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006399 chn->flags |= CF_WAKE_WRITE;
6400 goto missing_data_or_waiting;
6401 }
6402
6403 /* nothing left to forward for this chunk*/
6404 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6405 /* fall through for HTTP_MSG_CHUNK_CRLF */
6406
6407 case HTTP_MSG_CHUNK_CRLF:
6408 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006409 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006410 if (ret == 0)
6411 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006412 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006413 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02006414 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006415 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006416 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006417 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006418 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006419 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6420 /* fall through for HTTP_MSG_CHUNK_SIZE */
6421
6422 case HTTP_MSG_CHUNK_SIZE:
6423 /* read the chunk size and assign it to ->chunk_len,
6424 * then set ->next to point to the body and switch to
6425 * DATA or TRAILERS state.
6426 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006427 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006428 if (ret == 0)
6429 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006430 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006431 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006432 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006433 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006434 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006435 }
6436
6437 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006438 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006439 msg->chunk_len = chunk;
6440 msg->body_len += chunk;
6441
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006442 if (msg->chunk_len) {
6443 msg->msg_state = HTTP_MSG_DATA;
6444 goto switch_states;
6445 }
6446 msg->msg_state = HTTP_MSG_TRAILERS;
6447 /* fall through for HTTP_MSG_TRAILERS */
6448
6449 case HTTP_MSG_TRAILERS:
6450 ret = http_forward_trailers(msg);
6451 if (ret < 0)
6452 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006453 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6454 /* default_ret */ 1,
6455 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006456 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006457 if (!ret)
6458 goto missing_data_or_waiting;
6459 break;
6460
6461 case HTTP_MSG_ENDING:
6462 goto ending;
6463
6464 default:
6465 /* This should no happen in this function */
6466 goto error;
6467 }
6468
6469 msg->msg_state = HTTP_MSG_ENDING;
6470 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006471 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006472 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006473 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006474 /* default_ret */ msg->next,
6475 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006476 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006477 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006478 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6479 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006480 if (msg->next)
6481 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006482
Christopher Fauletda02e172015-12-04 09:25:05 +01006483 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006484 /* default_ret */ 1,
6485 /* on_error */ goto error,
6486 /* on_wait */ goto waiting);
6487 msg->msg_state = HTTP_MSG_DONE;
6488 return 1;
6489
6490 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006491 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006492 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006493 /* default_ret */ msg->next,
6494 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006495 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006496 msg->next -= ret;
6497 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6498 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006499 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006500 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006501 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006502 return 0;
6503
6504 chunk_parsing_error:
6505 if (msg->err_pos >= 0) {
6506 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006507 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006508 msg->msg_state, strm_fe(s));
6509 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006510 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006511 msg, msg->msg_state, s->be);
6512 }
6513 error:
6514 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006515}
6516
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006518/* Iterate the same filter through all request headers.
6519 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006520 * Since it can manage the switch to another backend, it updates the per-proxy
6521 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006522 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006523int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 char *cur_ptr, *cur_end, *cur_next;
6526 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006527 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006529 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 last_hdr = 0;
6532
Willy Tarreauf37954d2018-06-15 18:31:02 +02006533 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006534 old_idx = 0;
6535
6536 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006537 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006538 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006539 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 (exp->action == ACT_ALLOW ||
6541 exp->action == ACT_DENY ||
6542 exp->action == ACT_TARPIT))
6543 return 0;
6544
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006545 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546 if (!cur_idx)
6547 break;
6548
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006549 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 cur_ptr = cur_next;
6551 cur_end = cur_ptr + cur_hdr->len;
6552 cur_next = cur_end + cur_hdr->cr + 1;
6553
6554 /* Now we have one header between cur_ptr and cur_end,
6555 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006556 */
6557
Willy Tarreau15a53a42015-01-21 13:39:42 +01006558 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006559 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006561 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562 last_hdr = 1;
6563 break;
6564
6565 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006566 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 break;
6569
6570 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006571 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006572 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006573 break;
6574
6575 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006576 len = exp_replace(trash.area,
6577 trash.size, cur_ptr,
6578 exp->replace, pmatch);
6579 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006580 return -1;
6581
Willy Tarreau6e27be12018-08-22 04:46:47 +02006582 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6583
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006584 /* FIXME: if the user adds a newline in the replacement, the
6585 * index will not be recalculated for now, and the new line
6586 * will not be counted as a new header.
6587 */
6588
6589 cur_end += delta;
6590 cur_next += delta;
6591 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006592 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006593 break;
6594
6595 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006596 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 cur_next += delta;
6598
Willy Tarreaufa355d42009-11-29 18:12:29 +01006599 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006600 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6601 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006602 cur_hdr->len = 0;
6603 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006604 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006605 break;
6606
6607 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006608 }
6609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006610 /* keep the link from this header to next one in case of later
6611 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006613 old_idx = cur_idx;
6614 }
6615 return 0;
6616}
6617
6618
6619/* Apply the filter to the request line.
6620 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6621 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622 * Since it can manage the switch to another backend, it updates the per-proxy
6623 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006624 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006625int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006626{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006627 char *cur_ptr, *cur_end;
6628 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006629 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006630 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631
Willy Tarreau3d300592007-03-18 18:34:41 +01006632 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006633 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006634 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006635 (exp->action == ACT_ALLOW ||
6636 exp->action == ACT_DENY ||
6637 exp->action == ACT_TARPIT))
6638 return 0;
6639 else if (exp->action == ACT_REMOVE)
6640 return 0;
6641
6642 done = 0;
6643
Willy Tarreauf37954d2018-06-15 18:31:02 +02006644 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006645 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646
6647 /* Now we have the request line between cur_ptr and cur_end */
6648
Willy Tarreau15a53a42015-01-21 13:39:42 +01006649 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006650 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006651 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006652 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006653 done = 1;
6654 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006656 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006657 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006658 done = 1;
6659 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006662 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006663 done = 1;
6664 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006666 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006667 len = exp_replace(trash.area, trash.size,
6668 cur_ptr, exp->replace, pmatch);
6669 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006670 return -1;
6671
Willy Tarreau6e27be12018-08-22 04:46:47 +02006672 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006674 /* FIXME: if the user adds a newline in the replacement, the
6675 * index will not be recalculated for now, and the new line
6676 * will not be counted as a new header.
6677 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006678
Willy Tarreaufa355d42009-11-29 18:12:29 +01006679 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006680 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006681 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006682 HTTP_MSG_RQMETH,
6683 cur_ptr, cur_end + 1,
6684 NULL, NULL);
6685 if (unlikely(!cur_end))
6686 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006688 /* we have a full request and we know that we have either a CR
6689 * or an LF at <ptr>.
6690 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006691 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6692 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006693 /* there is no point trying this regex on headers */
6694 return 1;
6695 }
6696 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006697 return done;
6698}
Willy Tarreau97de6242006-12-27 17:18:38 +01006699
Willy Tarreau58f10d72006-12-04 02:26:12 +01006700
Willy Tarreau58f10d72006-12-04 02:26:12 +01006701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006702/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006703 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006704 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 * unparsable request. Since it can manage the switch to another backend, it
6706 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006707 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006708int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006709{
Willy Tarreau192252e2015-04-04 01:47:55 +02006710 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006711 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006712 struct hdr_exp *exp;
6713
6714 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006715 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006717 /*
6718 * The interleaving of transformations and verdicts
6719 * makes it difficult to decide to continue or stop
6720 * the evaluation.
6721 */
6722
Willy Tarreau6c123b12010-01-28 20:22:06 +01006723 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6724 break;
6725
Willy Tarreau3d300592007-03-18 18:34:41 +01006726 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006727 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006728 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006729 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006730
6731 /* if this filter had a condition, evaluate it now and skip to
6732 * next filter if the condition does not match.
6733 */
6734 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006735 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006736 ret = acl_pass(ret);
6737 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6738 ret = !ret;
6739
6740 if (!ret)
6741 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006742 }
6743
6744 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006745 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006746 if (unlikely(ret < 0))
6747 return -1;
6748
6749 if (likely(ret == 0)) {
6750 /* The filter did not match the request, it can be
6751 * iterated through all headers.
6752 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006753 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6754 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006755 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006757 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758}
6759
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006760
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761/* Find the end of a cookie value contained between <s> and <e>. It works the
6762 * same way as with headers above except that the semi-colon also ends a token.
6763 * See RFC2965 for more information. Note that it requires a valid header to
6764 * return a valid result.
6765 */
6766char *find_cookie_value_end(char *s, const char *e)
6767{
6768 int quoted, qdpair;
6769
6770 quoted = qdpair = 0;
6771 for (; s < e; s++) {
6772 if (qdpair) qdpair = 0;
6773 else if (quoted) {
6774 if (*s == '\\') qdpair = 1;
6775 else if (*s == '"') quoted = 0;
6776 }
6777 else if (*s == '"') quoted = 1;
6778 else if (*s == ',' || *s == ';') return s;
6779 }
6780 return s;
6781}
6782
6783/* Delete a value in a header between delimiters <from> and <next> in buffer
6784 * <buf>. The number of characters displaced is returned, and the pointer to
6785 * the first delimiter is updated if required. The function tries as much as
6786 * possible to respect the following principles :
6787 * - replace <from> delimiter by the <next> one unless <from> points to a
6788 * colon, in which case <next> is simply removed
6789 * - set exactly one space character after the new first delimiter, unless
6790 * there are not enough characters in the block being moved to do so.
6791 * - remove unneeded spaces before the previous delimiter and after the new
6792 * one.
6793 *
6794 * It is the caller's responsibility to ensure that :
6795 * - <from> points to a valid delimiter or the colon ;
6796 * - <next> points to a valid delimiter or the final CR/LF ;
6797 * - there are non-space chars before <from> ;
6798 * - there is a CR/LF at or after <next>.
6799 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006800int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006801{
6802 char *prev = *from;
6803
6804 if (*prev == ':') {
6805 /* We're removing the first value, preserve the colon and add a
6806 * space if possible.
6807 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006808 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006809 next++;
6810 prev++;
6811 if (prev < next)
6812 *prev++ = ' ';
6813
Willy Tarreau2235b262016-11-05 15:50:20 +01006814 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006815 next++;
6816 } else {
6817 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006818 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006819 prev--;
6820 *from = prev;
6821
6822 /* copy the delimiter and if possible a space if we're
6823 * not at the end of the line.
6824 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006825 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006826 *prev++ = *next++;
6827 if (prev + 1 < next)
6828 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006829 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006830 next++;
6831 }
6832 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006833 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834}
6835
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006836/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006837 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006838 * desirable to call it only when needed. This code is quite complex because
6839 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6840 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006841 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006842void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006843{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006844 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006845 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006846 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006847 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006848 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6849 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006850
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006851 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006852 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006853 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006854
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006855 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006856 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006857 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006858
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006859 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006860 hdr_beg = hdr_next;
6861 hdr_end = hdr_beg + cur_hdr->len;
6862 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006863
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006864 /* We have one full header between hdr_beg and hdr_end, and the
6865 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006866 * "Cookie:" headers.
6867 */
6868
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006869 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006870 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871 old_idx = cur_idx;
6872 continue;
6873 }
6874
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006875 del_from = NULL; /* nothing to be deleted */
6876 preserve_hdr = 0; /* assume we may kill the whole header */
6877
Willy Tarreau58f10d72006-12-04 02:26:12 +01006878 /* Now look for cookies. Conforming to RFC2109, we have to support
6879 * attributes whose name begin with a '$', and associate them with
6880 * the right cookie, if we want to delete this cookie.
6881 * So there are 3 cases for each cookie read :
6882 * 1) it's a special attribute, beginning with a '$' : ignore it.
6883 * 2) it's a server id cookie that we *MAY* want to delete : save
6884 * some pointers on it (last semi-colon, beginning of cookie...)
6885 * 3) it's an application cookie : we *MAY* have to delete a previous
6886 * "special" cookie.
6887 * At the end of loop, if a "special" cookie remains, we may have to
6888 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 * *MUST* delete it.
6890 *
6891 * Note: RFC2965 is unclear about the processing of spaces around
6892 * the equal sign in the ATTR=VALUE form. A careful inspection of
6893 * the RFC explicitly allows spaces before it, and not within the
6894 * tokens (attrs or values). An inspection of RFC2109 allows that
6895 * too but section 10.1.3 lets one think that spaces may be allowed
6896 * after the equal sign too, resulting in some (rare) buggy
6897 * implementations trying to do that. So let's do what servers do.
6898 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6899 * allowed quoted strings in values, with any possible character
6900 * after a backslash, including control chars and delimitors, which
6901 * causes parsing to become ambiguous. Browsers also allow spaces
6902 * within values even without quotes.
6903 *
6904 * We have to keep multiple pointers in order to support cookie
6905 * removal at the beginning, middle or end of header without
6906 * corrupting the header. All of these headers are valid :
6907 *
6908 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6909 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6910 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6911 * | | | | | | | | |
6912 * | | | | | | | | hdr_end <--+
6913 * | | | | | | | +--> next
6914 * | | | | | | +----> val_end
6915 * | | | | | +-----------> val_beg
6916 * | | | | +--------------> equal
6917 * | | | +----------------> att_end
6918 * | | +---------------------> att_beg
6919 * | +--------------------------> prev
6920 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921 */
6922
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006923 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6924 /* Iterate through all cookies on this line */
6925
6926 /* find att_beg */
6927 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006928 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006929 att_beg++;
6930
6931 /* find att_end : this is the first character after the last non
6932 * space before the equal. It may be equal to hdr_end.
6933 */
6934 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006935
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006936 while (equal < hdr_end) {
6937 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006938 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006939 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006940 continue;
6941 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006942 }
6943
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6945 * is between <att_beg> and <equal>, both may be identical.
6946 */
6947
6948 /* look for end of cookie if there is an equal sign */
6949 if (equal < hdr_end && *equal == '=') {
6950 /* look for the beginning of the value */
6951 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006952 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 val_beg++;
6954
6955 /* find the end of the value, respecting quotes */
6956 next = find_cookie_value_end(val_beg, hdr_end);
6957
6958 /* make val_end point to the first white space or delimitor after the value */
6959 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006960 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006961 val_end--;
6962 } else {
6963 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006964 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006966 /* We have nothing to do with attributes beginning with '$'. However,
6967 * they will automatically be removed if a header before them is removed,
6968 * since they're supposed to be linked together.
6969 */
6970 if (*att_beg == '$')
6971 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006973 /* Ignore cookies with no equal sign */
6974 if (equal == next) {
6975 /* This is not our cookie, so we must preserve it. But if we already
6976 * scheduled another cookie for removal, we cannot remove the
6977 * complete header, but we can remove the previous block itself.
6978 */
6979 preserve_hdr = 1;
6980 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006981 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006982 val_end += delta;
6983 next += delta;
6984 hdr_end += delta;
6985 hdr_next += delta;
6986 cur_hdr->len += delta;
6987 http_msg_move_end(&txn->req, delta);
6988 prev = del_from;
6989 del_from = NULL;
6990 }
6991 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006992 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006993
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 /* if there are spaces around the equal sign, we need to
6995 * strip them otherwise we'll get trouble for cookie captures,
6996 * or even for rewrites. Since this happens extremely rarely,
6997 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006998 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7000 int stripped_before = 0;
7001 int stripped_after = 0;
7002
7003 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007004 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005 equal += stripped_before;
7006 val_beg += stripped_before;
7007 }
7008
7009 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007010 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007011 val_beg += stripped_after;
7012 stripped_before += stripped_after;
7013 }
7014
7015 val_end += stripped_before;
7016 next += stripped_before;
7017 hdr_end += stripped_before;
7018 hdr_next += stripped_before;
7019 cur_hdr->len += stripped_before;
7020 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007021 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007022 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007023
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007024 /* First, let's see if we want to capture this cookie. We check
7025 * that we don't already have a client side cookie, because we
7026 * can only capture one. Also as an optimisation, we ignore
7027 * cookies shorter than the declared name.
7028 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007029 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7030 (val_end - att_beg >= sess->fe->capture_namelen) &&
7031 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007032 int log_len = val_end - att_beg;
7033
Willy Tarreaubafbe012017-11-24 17:34:44 +01007034 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007035 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007036 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007037 if (log_len > sess->fe->capture_len)
7038 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007039 memcpy(txn->cli_cookie, att_beg, log_len);
7040 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007041 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043
Willy Tarreaubca99692010-10-06 19:25:55 +02007044 /* Persistence cookies in passive, rewrite or insert mode have the
7045 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007047 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007049 * For cookies in prefix mode, the form is :
7050 *
7051 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007052 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007053 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7054 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7055 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007056 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007057
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007058 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7059 * have the server ID between val_beg and delim, and the original cookie between
7060 * delim+1 and val_end. Otherwise, delim==val_end :
7061 *
7062 * Cookie: NAME=SRV; # in all but prefix modes
7063 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7064 * | || || | |+-> next
7065 * | || || | +--> val_end
7066 * | || || +---------> delim
7067 * | || |+------------> val_beg
7068 * | || +-------------> att_end = equal
7069 * | |+-----------------> att_beg
7070 * | +------------------> prev
7071 * +-------------------------> hdr_beg
7072 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007074 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007075 for (delim = val_beg; delim < val_end; delim++)
7076 if (*delim == COOKIE_DELIM)
7077 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007078 } else {
7079 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007080 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007081 /* Now check if the cookie contains a date field, which would
7082 * appear after a vertical bar ('|') just after the server name
7083 * and before the delimiter.
7084 */
7085 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7086 if (vbar1) {
7087 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007088 * right is the last seen date. It is a base64 encoded
7089 * 30-bit value representing the UNIX date since the
7090 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007091 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007092 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007093 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007094 if (val_end - vbar1 >= 5) {
7095 val = b64tos30(vbar1);
7096 if (val > 0)
7097 txn->cookie_last_date = val << 2;
7098 }
7099 /* look for a second vertical bar */
7100 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7101 if (vbar1 && (val_end - vbar1 > 5)) {
7102 val = b64tos30(vbar1 + 1);
7103 if (val > 0)
7104 txn->cookie_first_date = val << 2;
7105 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007106 }
7107 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108
Willy Tarreauf64d1412010-10-07 20:06:11 +02007109 /* if the cookie has an expiration date and the proxy wants to check
7110 * it, then we do that now. We first check if the cookie is too old,
7111 * then only if it has expired. We detect strict overflow because the
7112 * time resolution here is not great (4 seconds). Cookies with dates
7113 * in the future are ignored if their offset is beyond one day. This
7114 * allows an admin to fix timezone issues without expiring everyone
7115 * and at the same time avoids keeping unwanted side effects for too
7116 * long.
7117 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007118 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7119 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007120 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007121 txn->flags &= ~TX_CK_MASK;
7122 txn->flags |= TX_CK_OLD;
7123 delim = val_beg; // let's pretend we have not found the cookie
7124 txn->cookie_first_date = 0;
7125 txn->cookie_last_date = 0;
7126 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007127 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7128 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007129 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007130 txn->flags &= ~TX_CK_MASK;
7131 txn->flags |= TX_CK_EXPIRED;
7132 delim = val_beg; // let's pretend we have not found the cookie
7133 txn->cookie_first_date = 0;
7134 txn->cookie_last_date = 0;
7135 }
7136
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007137 /* Here, we'll look for the first running server which supports the cookie.
7138 * This allows to share a same cookie between several servers, for example
7139 * to dedicate backup servers to specific servers only.
7140 * However, to prevent clients from sticking to cookie-less backup server
7141 * when they have incidentely learned an empty cookie, we simply ignore
7142 * empty cookies and mark them as invalid.
7143 * The same behaviour is applied when persistence must be ignored.
7144 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007145 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007148 while (srv) {
7149 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7150 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007151 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007152 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007153 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007154 /* we found the server and we can use it */
7155 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007156 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007157 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007158 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007159 break;
7160 } else {
7161 /* we found a server, but it's down,
7162 * mark it as such and go on in case
7163 * another one is available.
7164 */
7165 txn->flags &= ~TX_CK_MASK;
7166 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007167 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007168 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007169 srv = srv->next;
7170 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007171
Willy Tarreauf64d1412010-10-07 20:06:11 +02007172 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007173 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007174 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007175 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007176 txn->flags |= TX_CK_UNUSED;
7177 else
7178 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007179 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007180
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007181 /* depending on the cookie mode, we may have to either :
7182 * - delete the complete cookie if we're in insert+indirect mode, so that
7183 * the server never sees it ;
7184 * - remove the server id from the cookie value, and tag the cookie as an
7185 * application cookie so that it does not get accidentely removed later,
7186 * if we're in cookie prefix mode
7187 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007188 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007189 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007190
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007191 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007192 val_end += delta;
7193 next += delta;
7194 hdr_end += delta;
7195 hdr_next += delta;
7196 cur_hdr->len += delta;
7197 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007199 del_from = NULL;
7200 preserve_hdr = 1; /* we want to keep this cookie */
7201 }
7202 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007203 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007204 del_from = prev;
7205 }
7206 } else {
7207 /* This is not our cookie, so we must preserve it. But if we already
7208 * scheduled another cookie for removal, we cannot remove the
7209 * complete header, but we can remove the previous block itself.
7210 */
7211 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007212
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007213 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007214 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007215 if (att_beg >= del_from)
7216 att_beg += delta;
7217 if (att_end >= del_from)
7218 att_end += delta;
7219 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007220 val_end += delta;
7221 next += delta;
7222 hdr_end += delta;
7223 hdr_next += delta;
7224 cur_hdr->len += delta;
7225 http_msg_move_end(&txn->req, delta);
7226 prev = del_from;
7227 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007228 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007229 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007231 /* continue with next cookie on this header line */
7232 att_beg = next;
7233 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007235 /* There are no more cookies on this line.
7236 * We may still have one (or several) marked for deletion at the
7237 * end of the line. We must do this now in two ways :
7238 * - if some cookies must be preserved, we only delete from the
7239 * mark to the end of line ;
7240 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007241 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007242 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007244 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007245 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007246 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007247 cur_hdr->len += delta;
7248 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007249 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007250
7251 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007252 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7253 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007254 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007255 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007256 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007257 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007258 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007259 }
7260
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007261 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007263 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264}
7265
7266
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267/* Iterate the same filter through all response headers contained in <rtr>.
7268 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7269 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007270int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272 char *cur_ptr, *cur_end, *cur_next;
7273 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007274 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02007276 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007277
7278 last_hdr = 0;
7279
Willy Tarreauf37954d2018-06-15 18:31:02 +02007280 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 old_idx = 0;
7282
7283 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007284 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007286 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287 (exp->action == ACT_ALLOW ||
7288 exp->action == ACT_DENY))
7289 return 0;
7290
7291 cur_idx = txn->hdr_idx.v[old_idx].next;
7292 if (!cur_idx)
7293 break;
7294
7295 cur_hdr = &txn->hdr_idx.v[cur_idx];
7296 cur_ptr = cur_next;
7297 cur_end = cur_ptr + cur_hdr->len;
7298 cur_next = cur_end + cur_hdr->cr + 1;
7299
7300 /* Now we have one header between cur_ptr and cur_end,
7301 * and the next header starts at cur_next.
7302 */
7303
Willy Tarreau15a53a42015-01-21 13:39:42 +01007304 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305 switch (exp->action) {
7306 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007307 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308 last_hdr = 1;
7309 break;
7310
7311 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007312 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313 last_hdr = 1;
7314 break;
7315
7316 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02007317 len = exp_replace(trash.area,
7318 trash.size, cur_ptr,
7319 exp->replace, pmatch);
7320 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06007321 return -1;
7322
Willy Tarreau6e27be12018-08-22 04:46:47 +02007323 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
7324
Willy Tarreaua15645d2007-03-18 16:22:39 +01007325 /* FIXME: if the user adds a newline in the replacement, the
7326 * index will not be recalculated for now, and the new line
7327 * will not be counted as a new header.
7328 */
7329
7330 cur_end += delta;
7331 cur_next += delta;
7332 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007333 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 break;
7335
7336 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007337 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007338 cur_next += delta;
7339
Willy Tarreaufa355d42009-11-29 18:12:29 +01007340 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7342 txn->hdr_idx.used--;
7343 cur_hdr->len = 0;
7344 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007345 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007346 break;
7347
7348 }
7349 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350
7351 /* keep the link from this header to next one in case of later
7352 * removal of next header.
7353 */
7354 old_idx = cur_idx;
7355 }
7356 return 0;
7357}
7358
7359
7360/* Apply the filter to the status line in the response buffer <rtr>.
7361 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7362 * or -1 if a replacement resulted in an invalid status line.
7363 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007364int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366 char *cur_ptr, *cur_end;
7367 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007368 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02007369 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007373 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007374 (exp->action == ACT_ALLOW ||
7375 exp->action == ACT_DENY))
7376 return 0;
7377 else if (exp->action == ACT_REMOVE)
7378 return 0;
7379
7380 done = 0;
7381
Willy Tarreauf37954d2018-06-15 18:31:02 +02007382 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007383 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384
7385 /* Now we have the status line between cur_ptr and cur_end */
7386
Willy Tarreau15a53a42015-01-21 13:39:42 +01007387 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 switch (exp->action) {
7389 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007390 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007391 done = 1;
7392 break;
7393
7394 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007395 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 done = 1;
7397 break;
7398
7399 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02007400 len = exp_replace(trash.area, trash.size,
7401 cur_ptr, exp->replace, pmatch);
7402 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06007403 return -1;
7404
Willy Tarreau6e27be12018-08-22 04:46:47 +02007405 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
7406
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 /* FIXME: if the user adds a newline in the replacement, the
7408 * index will not be recalculated for now, and the new line
7409 * will not be counted as a new header.
7410 */
7411
Willy Tarreaufa355d42009-11-29 18:12:29 +01007412 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007413 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007414 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007415 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 cur_ptr, cur_end + 1,
7417 NULL, NULL);
7418 if (unlikely(!cur_end))
7419 return -1;
7420
7421 /* we have a full respnse and we know that we have either a CR
7422 * or an LF at <ptr>.
7423 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007424 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007425 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007426 /* there is no point trying this regex on headers */
7427 return 1;
7428 }
7429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430 return done;
7431}
7432
7433
7434
7435/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007436 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007437 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7438 * unparsable response.
7439 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007440int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441{
Willy Tarreau192252e2015-04-04 01:47:55 +02007442 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007443 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007444 struct hdr_exp *exp;
7445
7446 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 int ret;
7448
7449 /*
7450 * The interleaving of transformations and verdicts
7451 * makes it difficult to decide to continue or stop
7452 * the evaluation.
7453 */
7454
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007455 if (txn->flags & TX_SVDENY)
7456 break;
7457
Willy Tarreau3d300592007-03-18 18:34:41 +01007458 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007459 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7460 exp->action == ACT_PASS)) {
7461 exp = exp->next;
7462 continue;
7463 }
7464
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007465 /* if this filter had a condition, evaluate it now and skip to
7466 * next filter if the condition does not match.
7467 */
7468 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007469 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007470 ret = acl_pass(ret);
7471 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7472 ret = !ret;
7473 if (!ret)
7474 continue;
7475 }
7476
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007478 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007479 if (unlikely(ret < 0))
7480 return -1;
7481
7482 if (likely(ret == 0)) {
7483 /* The filter did not match the response, it can be
7484 * iterated through all headers.
7485 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007486 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7487 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007489 }
7490 return 0;
7491}
7492
7493
Willy Tarreaua15645d2007-03-18 16:22:39 +01007494/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007495 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 * desirable to call it only when needed. This function is also used when we
7497 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007498 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007499void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007500{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007501 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007502 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007503 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007504 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007505 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007506 char *hdr_beg, *hdr_end, *hdr_next;
7507 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007508
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509 /* Iterate through the headers.
7510 * we start with the start line.
7511 */
7512 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007513 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007514
7515 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7516 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007517 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518
7519 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007520 hdr_beg = hdr_next;
7521 hdr_end = hdr_beg + cur_hdr->len;
7522 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523
Willy Tarreau24581ba2010-08-31 22:39:35 +02007524 /* We have one full header between hdr_beg and hdr_end, and the
7525 * next header starts at hdr_next. We're only interested in
7526 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007527 */
7528
Willy Tarreau24581ba2010-08-31 22:39:35 +02007529 is_cookie2 = 0;
7530 prev = hdr_beg + 10;
7531 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007532 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007533 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7534 if (!val) {
7535 old_idx = cur_idx;
7536 continue;
7537 }
7538 is_cookie2 = 1;
7539 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007540 }
7541
Willy Tarreau24581ba2010-08-31 22:39:35 +02007542 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7543 * <prev> points to the colon.
7544 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007545 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007546
Willy Tarreau24581ba2010-08-31 22:39:35 +02007547 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7548 * check-cache is enabled) and we are not interested in checking
7549 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007550 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007551 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007552 return;
7553
Willy Tarreau24581ba2010-08-31 22:39:35 +02007554 /* OK so now we know we have to process this response cookie.
7555 * The format of the Set-Cookie header is slightly different
7556 * from the format of the Cookie header in that it does not
7557 * support the comma as a cookie delimiter (thus the header
7558 * cannot be folded) because the Expires attribute described in
7559 * the original Netscape's spec may contain an unquoted date
7560 * with a comma inside. We have to live with this because
7561 * many browsers don't support Max-Age and some browsers don't
7562 * support quoted strings. However the Set-Cookie2 header is
7563 * clean.
7564 *
7565 * We have to keep multiple pointers in order to support cookie
7566 * removal at the beginning, middle or end of header without
7567 * corrupting the header (in case of set-cookie2). A special
7568 * pointer, <scav> points to the beginning of the set-cookie-av
7569 * fields after the first semi-colon. The <next> pointer points
7570 * either to the end of line (set-cookie) or next unquoted comma
7571 * (set-cookie2). All of these headers are valid :
7572 *
7573 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7574 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7575 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7576 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7577 * | | | | | | | | | |
7578 * | | | | | | | | +-> next hdr_end <--+
7579 * | | | | | | | +------------> scav
7580 * | | | | | | +--------------> val_end
7581 * | | | | | +--------------------> val_beg
7582 * | | | | +----------------------> equal
7583 * | | | +------------------------> att_end
7584 * | | +----------------------------> att_beg
7585 * | +------------------------------> prev
7586 * +-----------------------------------------> hdr_beg
7587 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588
Willy Tarreau24581ba2010-08-31 22:39:35 +02007589 for (; prev < hdr_end; prev = next) {
7590 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007591
Willy Tarreau24581ba2010-08-31 22:39:35 +02007592 /* find att_beg */
7593 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007594 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007595 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007596
Willy Tarreau24581ba2010-08-31 22:39:35 +02007597 /* find att_end : this is the first character after the last non
7598 * space before the equal. It may be equal to hdr_end.
7599 */
7600 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601
Willy Tarreau24581ba2010-08-31 22:39:35 +02007602 while (equal < hdr_end) {
7603 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7604 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007605 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007606 continue;
7607 att_end = equal;
7608 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609
Willy Tarreau24581ba2010-08-31 22:39:35 +02007610 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7611 * is between <att_beg> and <equal>, both may be identical.
7612 */
7613
7614 /* look for end of cookie if there is an equal sign */
7615 if (equal < hdr_end && *equal == '=') {
7616 /* look for the beginning of the value */
7617 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007618 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007619 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620
Willy Tarreau24581ba2010-08-31 22:39:35 +02007621 /* find the end of the value, respecting quotes */
7622 next = find_cookie_value_end(val_beg, hdr_end);
7623
7624 /* make val_end point to the first white space or delimitor after the value */
7625 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007626 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007627 val_end--;
7628 } else {
7629 /* <equal> points to next comma, semi-colon or EOL */
7630 val_beg = val_end = next = equal;
7631 }
7632
7633 if (next < hdr_end) {
7634 /* Set-Cookie2 supports multiple cookies, and <next> points to
7635 * a colon or semi-colon before the end. So skip all attr-value
7636 * pairs and look for the next comma. For Set-Cookie, since
7637 * commas are permitted in values, skip to the end.
7638 */
7639 if (is_cookie2)
7640 next = find_hdr_value_end(next, hdr_end);
7641 else
7642 next = hdr_end;
7643 }
7644
7645 /* Now everything is as on the diagram above */
7646
7647 /* Ignore cookies with no equal sign */
7648 if (equal == val_end)
7649 continue;
7650
7651 /* If there are spaces around the equal sign, we need to
7652 * strip them otherwise we'll get trouble for cookie captures,
7653 * or even for rewrites. Since this happens extremely rarely,
7654 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007656 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7657 int stripped_before = 0;
7658 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659
Willy Tarreau24581ba2010-08-31 22:39:35 +02007660 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007661 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007662 equal += stripped_before;
7663 val_beg += stripped_before;
7664 }
7665
7666 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007667 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007668 val_beg += stripped_after;
7669 stripped_before += stripped_after;
7670 }
7671
7672 val_end += stripped_before;
7673 next += stripped_before;
7674 hdr_end += stripped_before;
7675 hdr_next += stripped_before;
7676 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007677 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007678 }
7679
7680 /* First, let's see if we want to capture this cookie. We check
7681 * that we don't already have a server side cookie, because we
7682 * can only capture one. Also as an optimisation, we ignore
7683 * cookies shorter than the declared name.
7684 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007685 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007686 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007687 (val_end - att_beg >= sess->fe->capture_namelen) &&
7688 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007689 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007690 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007691 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007693 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007694 if (log_len > sess->fe->capture_len)
7695 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007696 memcpy(txn->srv_cookie, att_beg, log_len);
7697 txn->srv_cookie[log_len] = 0;
7698 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007699 }
7700
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007701 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007703 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007704 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7705 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007706 /* assume passive cookie by default */
7707 txn->flags &= ~TX_SCK_MASK;
7708 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007709
7710 /* If the cookie is in insert mode on a known server, we'll delete
7711 * this occurrence because we'll insert another one later.
7712 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007713 * a direct access.
7714 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007715 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007716 /* The "preserve" flag was set, we don't want to touch the
7717 * server's cookie.
7718 */
7719 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007720 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007721 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007722 /* this cookie must be deleted */
7723 if (*prev == ':' && next == hdr_end) {
7724 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007725 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007726 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7727 txn->hdr_idx.used--;
7728 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007729 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007730 hdr_next += delta;
7731 http_msg_move_end(&txn->rsp, delta);
7732 /* note: while both invalid now, <next> and <hdr_end>
7733 * are still equal, so the for() will stop as expected.
7734 */
7735 } else {
7736 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007737 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007738 next = prev;
7739 hdr_end += delta;
7740 hdr_next += delta;
7741 cur_hdr->len += delta;
7742 http_msg_move_end(&txn->rsp, delta);
7743 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007744 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007745 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007746 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007747 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007748 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007749 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007750 * with this server since we know it.
7751 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007752 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007753 next += delta;
7754 hdr_end += delta;
7755 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007756 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007757 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007758
Willy Tarreauf1348312010-10-07 15:54:11 +02007759 txn->flags &= ~TX_SCK_MASK;
7760 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007762 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007763 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007764 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007765 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007766 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007767 next += delta;
7768 hdr_end += delta;
7769 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007770 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007771 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007772
Willy Tarreau827aee92011-03-10 16:55:02 +01007773 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007774 txn->flags &= ~TX_SCK_MASK;
7775 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776 }
7777 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007778 /* that's done for this cookie, check the next one on the same
7779 * line when next != hdr_end (only if is_cookie2).
7780 */
7781 }
7782 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007783 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007784 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007785}
7786
7787
Willy Tarreaua15645d2007-03-18 16:22:39 +01007788/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007789 * Parses the Cache-Control and Pragma request header fields to determine if
7790 * the request may be served from the cache and/or if it is cacheable. Updates
7791 * s->txn->flags.
7792 */
7793void check_request_for_cacheability(struct stream *s, struct channel *chn)
7794{
7795 struct http_txn *txn = s->txn;
7796 char *p1, *p2;
7797 char *cur_ptr, *cur_end, *cur_next;
7798 int pragma_found;
7799 int cc_found;
7800 int cur_idx;
7801
7802 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7803 return; /* nothing more to do here */
7804
7805 cur_idx = 0;
7806 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007807 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007808
7809 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7810 struct hdr_idx_elem *cur_hdr;
7811 int val;
7812
7813 cur_hdr = &txn->hdr_idx.v[cur_idx];
7814 cur_ptr = cur_next;
7815 cur_end = cur_ptr + cur_hdr->len;
7816 cur_next = cur_end + cur_hdr->cr + 1;
7817
7818 /* We have one full header between cur_ptr and cur_end, and the
7819 * next header starts at cur_next.
7820 */
7821
7822 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7823 if (val) {
7824 if ((cur_end - (cur_ptr + val) >= 8) &&
7825 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7826 pragma_found = 1;
7827 continue;
7828 }
7829 }
7830
William Lallemand8a16fe02018-05-22 11:04:33 +02007831 /* Don't use the cache and don't try to store if we found the
7832 * Authorization header */
7833 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7834 if (val) {
7835 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7836 txn->flags |= TX_CACHE_IGNORE;
7837 continue;
7838 }
7839
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007840 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7841 if (!val)
7842 continue;
7843
7844 /* OK, right now we know we have a cache-control header at cur_ptr */
7845 cc_found = 1;
7846 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7847
7848 if (p1 >= cur_end) /* no more info */
7849 continue;
7850
7851 /* p1 is at the beginning of the value */
7852 p2 = p1;
7853 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7854 p2++;
7855
7856 /* we have a complete value between p1 and p2. We don't check the
7857 * values after max-age, max-stale nor min-fresh, we simply don't
7858 * use the cache when they're specified.
7859 */
7860 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7861 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7862 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7863 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7864 txn->flags |= TX_CACHE_IGNORE;
7865 continue;
7866 }
7867
7868 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7869 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7870 continue;
7871 }
7872 }
7873
7874 /* RFC7234#5.4:
7875 * When the Cache-Control header field is also present and
7876 * understood in a request, Pragma is ignored.
7877 * When the Cache-Control header field is not present in a
7878 * request, caches MUST consider the no-cache request
7879 * pragma-directive as having the same effect as if
7880 * "Cache-Control: no-cache" were present.
7881 */
7882 if (!cc_found && pragma_found)
7883 txn->flags |= TX_CACHE_IGNORE;
7884}
7885
7886/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007887 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007888 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007889void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007891 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007892 char *p1, *p2;
7893
7894 char *cur_ptr, *cur_end, *cur_next;
7895 int cur_idx;
7896
Willy Tarreau12b32f22017-12-21 16:08:09 +01007897 if (txn->status < 200) {
7898 /* do not try to cache interim responses! */
7899 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007901 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007902
7903 /* Iterate through the headers.
7904 * we start with the start line.
7905 */
7906 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007907 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908
7909 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7910 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007911 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007912
7913 cur_hdr = &txn->hdr_idx.v[cur_idx];
7914 cur_ptr = cur_next;
7915 cur_end = cur_ptr + cur_hdr->len;
7916 cur_next = cur_end + cur_hdr->cr + 1;
7917
7918 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007919 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007920 */
7921
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007922 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7923 if (val) {
7924 if ((cur_end - (cur_ptr + val) >= 8) &&
7925 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7926 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7927 return;
7928 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929 }
7930
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007931 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7932 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007933 continue;
7934
7935 /* OK, right now we know we have a cache-control header at cur_ptr */
7936
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007937 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007938
7939 if (p1 >= cur_end) /* no more info */
7940 continue;
7941
7942 /* p1 is at the beginning of the value */
7943 p2 = p1;
7944
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007945 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007946 p2++;
7947
7948 /* we have a complete value between p1 and p2 */
7949 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007950 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7951 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7952 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7953 continue;
7954 }
7955
Willy Tarreaua15645d2007-03-18 16:22:39 +01007956 /* we have something of the form no-cache="set-cookie" */
7957 if ((cur_end - p1 >= 21) &&
7958 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7959 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007960 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 continue;
7962 }
7963
7964 /* OK, so we know that either p2 points to the end of string or to a comma */
7965 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007966 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007967 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007968 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007969 return;
7970 }
7971
7972 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007973 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974 continue;
7975 }
7976 }
7977}
7978
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979
Willy Tarreaub2513902006-12-17 14:52:38 +01007980/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007981 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007982 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007983 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007984 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007985 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007986 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007987 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007988 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007989int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007990{
7991 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007992 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007993 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007994
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007995 if (!uri_auth)
7996 return 0;
7997
Cyril Bonté70be45d2010-10-12 00:14:35 +02007998 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007999 return 0;
8000
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008001 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008002 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008003 return 0;
8004
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008005 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008006 return 0;
8007
Willy Tarreaub2513902006-12-17 14:52:38 +01008008 return 1;
8009}
8010
Willy Tarreau4076a152009-04-02 15:18:36 +02008011/*
8012 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008013 * By default it tries to report the error position as msg->err_pos. However if
8014 * this one is not set, it will then report msg->next, which is the last known
8015 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008016 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008017 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02008018void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008019 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02008020 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008021{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008022 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008023 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008024 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008025
Christopher Faulet2a944ee2017-11-07 10:42:54 +01008026 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau7480f322018-09-06 19:41:22 +02008027 es->buf_len = MIN(ci_data(chn), global.tune.bufsize);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008028 len1 = b_wrap(&chn->buf) - ci_head(chn);
Willy Tarreau7480f322018-09-06 19:41:22 +02008029 len1 = MIN(len1, es->buf_len);
8030 len2 = es->buf_len - len1; /* remaining data if buffer wraps */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008031
Willy Tarreauf3764b72016-03-31 13:45:10 +02008032 if (!es->buf)
8033 es->buf = malloc(global.tune.bufsize);
8034
8035 if (es->buf) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02008036 memcpy(es->buf, ci_head(chn), len1);
Willy Tarreauf3764b72016-03-31 13:45:10 +02008037 if (len2)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008038 memcpy(es->buf + len1, b_orig(&chn->buf), len2);
Willy Tarreauf3764b72016-03-31 13:45:10 +02008039 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008040
Willy Tarreau4076a152009-04-02 15:18:36 +02008041 if (msg->err_pos >= 0)
Willy Tarreau7480f322018-09-06 19:41:22 +02008042 es->buf_err = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008043 else
Willy Tarreau7480f322018-09-06 19:41:22 +02008044 es->buf_err = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008045
Willy Tarreau4076a152009-04-02 15:18:36 +02008046 es->when = date; // user-visible date
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008047 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008048 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008049 if (objt_conn(sess->origin))
8050 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008051 else
8052 memset(&es->src, 0, sizeof(es->src));
8053
Willy Tarreaue9e878a2018-09-07 11:29:59 +02008054 es->ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1);
Willy Tarreau7480f322018-09-06 19:41:22 +02008055 es->buf_wrap = b_wrap(&chn->buf) - ci_head(chn);
8056 es->buf_out = co_data(chn);
8057 es->buf_ofs = chn->total;
8058
Willy Tarreau0b5b4802018-09-07 13:49:44 +02008059 /* be sure to indicate the offset of the first IN byte */
8060 if (es->buf_ofs >= es->buf_len)
8061 es->buf_ofs -= es->buf_len;
8062 else
8063 es->buf_ofs = 0;
8064
Willy Tarreau7480f322018-09-06 19:41:22 +02008065 /* http-specific part now */
8066 es->ctx.http.sid = s->uniq_id;
8067 es->ctx.http.state = state;
8068 es->ctx.http.b_flags = chn->flags;
8069 es->ctx.http.s_flags = s->flags;
8070 es->ctx.http.t_flags = s->txn->flags;
8071 es->ctx.http.m_flags = msg->flags;
8072 es->ctx.http.m_clen = msg->chunk_len;
8073 es->ctx.http.m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01008074 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02008075}
Willy Tarreaub2513902006-12-17 14:52:38 +01008076
Willy Tarreau294c4732011-12-16 21:35:50 +01008077/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8078 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8079 * performed over the whole headers. Otherwise it must contain a valid header
8080 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8081 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8082 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8083 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008084 * -1. The value fetch stops at commas, so this function is suited for use with
8085 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008086 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008087 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008088unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008089 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008090 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008091{
Willy Tarreau294c4732011-12-16 21:35:50 +01008092 struct hdr_ctx local_ctx;
8093 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008094 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008095 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008096 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008097
Willy Tarreau294c4732011-12-16 21:35:50 +01008098 if (!ctx) {
8099 local_ctx.idx = 0;
8100 ctx = &local_ctx;
8101 }
8102
Willy Tarreaubce70882009-09-07 11:51:47 +02008103 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008104 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008105 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008106 occ--;
8107 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008108 *vptr = ctx->line + ctx->val;
8109 *vlen = ctx->vlen;
8110 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008111 }
8112 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008113 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008114 }
8115
8116 /* negative occurrence, we scan all the list then walk back */
8117 if (-occ > MAX_HDR_HISTORY)
8118 return 0;
8119
Willy Tarreau294c4732011-12-16 21:35:50 +01008120 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008121 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008122 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8123 len_hist[hist_ptr] = ctx->vlen;
8124 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008125 hist_ptr = 0;
8126 found++;
8127 }
8128 if (-occ > found)
8129 return 0;
8130 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008131 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8132 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8133 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008134 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008135 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008136 if (hist_ptr >= MAX_HDR_HISTORY)
8137 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008138 *vptr = ptr_hist[hist_ptr];
8139 *vlen = len_hist[hist_ptr];
8140 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008141}
8142
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008143/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8144 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8145 * performed over the whole headers. Otherwise it must contain a valid header
8146 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8147 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8148 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8149 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8150 * -1. This function differs from http_get_hdr() in that it only returns full
8151 * line header values and does not stop at commas.
8152 * The return value is 0 if nothing was found, or non-zero otherwise.
8153 */
8154unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8155 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008156 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008157{
8158 struct hdr_ctx local_ctx;
8159 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008160 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008161 unsigned int hist_ptr;
8162 int found;
8163
8164 if (!ctx) {
8165 local_ctx.idx = 0;
8166 ctx = &local_ctx;
8167 }
8168
8169 if (occ >= 0) {
8170 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008171 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008172 occ--;
8173 if (occ <= 0) {
8174 *vptr = ctx->line + ctx->val;
8175 *vlen = ctx->vlen;
8176 return 1;
8177 }
8178 }
8179 return 0;
8180 }
8181
8182 /* negative occurrence, we scan all the list then walk back */
8183 if (-occ > MAX_HDR_HISTORY)
8184 return 0;
8185
8186 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008187 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008188 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8189 len_hist[hist_ptr] = ctx->vlen;
8190 if (++hist_ptr >= MAX_HDR_HISTORY)
8191 hist_ptr = 0;
8192 found++;
8193 }
8194 if (-occ > found)
8195 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008196
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008197 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008198 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8199 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8200 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008201 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008202 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008203 if (hist_ptr >= MAX_HDR_HISTORY)
8204 hist_ptr -= MAX_HDR_HISTORY;
8205 *vptr = ptr_hist[hist_ptr];
8206 *vlen = len_hist[hist_ptr];
8207 return 1;
8208}
8209
Willy Tarreaubaaee002006-06-26 02:48:02 +02008210/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008211 * Print a debug line with a header. Always stop at the first CR or LF char,
8212 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8213 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008214 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008215void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008216{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008217 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008218 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008219
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008220 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008221 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008222 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008223 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 +02008224
8225 for (max = 0; start + max < end; max++)
8226 if (start[max] == '\r' || start[max] == '\n')
8227 break;
8228
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008229 UBOUND(max, trash.size - trash.data - 3);
8230 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
8231 trash.area[trash.data++] = '\n';
8232 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008233}
8234
Willy Tarreaueee5b512015-04-03 23:46:31 +02008235
8236/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8237 * The hdr_idx is allocated as well. In case of allocation failure, everything
8238 * allocated is freed and NULL is returned. Otherwise the new transaction is
8239 * assigned to the stream and returned.
8240 */
8241struct http_txn *http_alloc_txn(struct stream *s)
8242{
8243 struct http_txn *txn = s->txn;
8244
8245 if (txn)
8246 return txn;
8247
Willy Tarreaubafbe012017-11-24 17:34:44 +01008248 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008249 if (!txn)
8250 return txn;
8251
8252 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008253 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008254 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008255 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008256 return NULL;
8257 }
8258
8259 s->txn = txn;
8260 return txn;
8261}
8262
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008263void http_txn_reset_req(struct http_txn *txn)
8264{
8265 txn->req.flags = 0;
8266 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8267 txn->req.next = 0;
8268 txn->req.chunk_len = 0LL;
8269 txn->req.body_len = 0LL;
8270 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8271}
8272
8273void http_txn_reset_res(struct http_txn *txn)
8274{
8275 txn->rsp.flags = 0;
8276 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8277 txn->rsp.next = 0;
8278 txn->rsp.chunk_len = 0LL;
8279 txn->rsp.body_len = 0LL;
8280 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8281}
8282
Willy Tarreau0937bc42009-12-22 15:03:09 +01008283/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008284 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008285 * the required fields are properly allocated and that we only need to (re)init
8286 * them. This should be used before processing any new request.
8287 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008288void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008289{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008290 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008291 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008292
8293 txn->flags = 0;
8294 txn->status = -1;
8295
Willy Tarreauf64d1412010-10-07 20:06:11 +02008296 txn->cookie_first_date = 0;
8297 txn->cookie_last_date = 0;
8298
Willy Tarreaueee5b512015-04-03 23:46:31 +02008299 txn->srv_cookie = NULL;
8300 txn->cli_cookie = NULL;
8301 txn->uri = NULL;
8302
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008303 http_txn_reset_req(txn);
8304 http_txn_reset_res(txn);
8305
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008306 txn->req.chn = &s->req;
8307 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008308
8309 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008310
8311 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8312 if (fe->options2 & PR_O2_REQBUG_OK)
8313 txn->req.err_pos = -1; /* let buggy requests pass */
8314
Willy Tarreau0937bc42009-12-22 15:03:09 +01008315 if (txn->hdr_idx.v)
8316 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008317
8318 vars_init(&s->vars_txn, SCOPE_TXN);
8319 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008320}
8321
8322/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008323void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008324{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008325 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008326 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008327
8328 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008329 pool_free(pool_head_requri, txn->uri);
8330 pool_free(pool_head_capture, txn->cli_cookie);
8331 pool_free(pool_head_capture, txn->srv_cookie);
8332 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008333
William Lallemanda73203e2012-03-12 12:48:57 +01008334 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008335 txn->uri = NULL;
8336 txn->srv_cookie = NULL;
8337 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008338
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008339 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008340 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008341 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008342 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008343 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008344 }
8345
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008346 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008347 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008348 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008349 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008350 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008351 }
8352
Willy Tarreau6204cd92016-03-10 16:33:04 +01008353 vars_prune(&s->vars_txn, s->sess, s);
8354 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008355}
8356
8357/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008358void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008359{
8360 http_end_txn(s);
8361 http_init_txn(s);
8362
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008363 /* reinitialise the current rule list pointer to NULL. We are sure that
8364 * any rulelist match the NULL pointer.
8365 */
8366 s->current_rule_list = NULL;
8367
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008368 s->be = strm_fe(s);
8369 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008370 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008371 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008372 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008373 /* re-init store persistence */
8374 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02008375 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01008376
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008377 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008378
Willy Tarreau739cfba2010-01-25 23:11:14 +01008379 /* We must trim any excess data from the response buffer, because we
8380 * may have blocked an invalid response from a server that we don't
8381 * want to accidentely forward once we disable the analysers, nor do
8382 * we want those data to come along with next response. A typical
8383 * example of such data would be from a buggy server responding to
8384 * a HEAD with some data, or sending more than the advertised
8385 * content-length.
8386 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008387 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008388 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01008389
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008390 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02008391 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008392
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008393 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008394 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008395
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008396 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008397 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008398
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008399 s->req.rex = TICK_ETERNITY;
8400 s->req.wex = TICK_ETERNITY;
8401 s->req.analyse_exp = TICK_ETERNITY;
8402 s->res.rex = TICK_ETERNITY;
8403 s->res.wex = TICK_ETERNITY;
8404 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008405 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008406}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008407
Sasha Pachev218f0642014-06-16 12:05:59 -06008408void free_http_res_rules(struct list *r)
8409{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008410 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008411
8412 list_for_each_entry_safe(pr, tr, r, list) {
8413 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008414 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008415 free(pr);
8416 }
8417}
8418
8419void free_http_req_rules(struct list *r)
8420{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008421 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008422
8423 list_for_each_entry_safe(pr, tr, r, list) {
8424 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008425 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008426 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008427
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008428 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008429 free(pr);
8430 }
8431}
8432
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008433/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008434struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008435{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008436 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008437 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008438 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008439 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008440
Vincent Bernat02779b62016-04-03 13:48:43 +02008441 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008442 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008443 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008444 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008445 }
8446
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008447 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008448 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008449 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008450 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008451 int code;
8452 int hc;
8453
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008454 if (!strcmp(args[0], "tarpit")) {
8455 rule->action = ACT_HTTP_REQ_TARPIT;
8456 rule->deny_status = HTTP_ERR_500;
8457 }
8458 else {
8459 rule->action = ACT_ACTION_DENY;
8460 rule->deny_status = HTTP_ERR_403;
8461 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008462 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008463 if (strcmp(args[cur_arg], "deny_status") == 0) {
8464 cur_arg++;
8465 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008466 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8467 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008468 goto out_err;
8469 }
8470
8471 code = atol(args[cur_arg]);
8472 cur_arg++;
8473 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8474 if (http_err_codes[hc] == code) {
8475 rule->deny_status = hc;
8476 break;
8477 }
8478 }
8479
8480 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008481 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8482 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008483 }
8484 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008485 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008486 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008487 cur_arg = 1;
8488
8489 while(*args[cur_arg]) {
8490 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008491 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008492 cur_arg+=2;
8493 continue;
8494 } else
8495 break;
8496 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008497 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008498 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008499 cur_arg = 1;
8500
8501 if (!*args[cur_arg] ||
8502 (*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 +01008503 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8504 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008505 goto out_err;
8506 }
8507 rule->arg.nice = atoi(args[cur_arg]);
8508 if (rule->arg.nice < -1024)
8509 rule->arg.nice = -1024;
8510 else if (rule->arg.nice > 1024)
8511 rule->arg.nice = 1024;
8512 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008513 } else if (!strcmp(args[0], "set-tos")) {
8514#ifdef IP_TOS
8515 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008516 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008517 cur_arg = 1;
8518
8519 if (!*args[cur_arg] ||
8520 (*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 +01008521 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8522 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008523 goto out_err;
8524 }
8525
8526 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8527 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008528 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8529 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008530 goto out_err;
8531 }
8532 cur_arg++;
8533#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008534 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 +02008535 goto out_err;
8536#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008537 } else if (!strcmp(args[0], "set-mark")) {
8538#ifdef SO_MARK
8539 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008540 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008541 cur_arg = 1;
8542
8543 if (!*args[cur_arg] ||
8544 (*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 +01008545 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8546 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008547 goto out_err;
8548 }
8549
8550 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8551 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008552 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8553 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008554 goto out_err;
8555 }
8556 cur_arg++;
8557 global.last_checks |= LSTCHK_NETADM;
8558#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008559 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 +02008560 goto out_err;
8561#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008562 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008563 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008564 cur_arg = 1;
8565
8566 if (!*args[cur_arg] ||
8567 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8568 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008569 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8570 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008571 goto out_err;
8572 }
8573 if (strcmp(args[cur_arg], "silent") == 0)
8574 rule->arg.loglevel = -1;
8575 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8576 goto bad_log_level;
8577 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008578 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008579 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008580 cur_arg = 1;
8581
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008582 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8583 (*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 +01008584 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8585 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008586 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008587 }
8588
8589 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8590 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8591 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008592
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008593 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008594 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008595 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 +01008596 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008597 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8598 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008599 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008600 goto out_err;
8601 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008602 free(proxy->conf.lfs_file);
8603 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8604 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008605 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008606 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008607 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008608 cur_arg = 1;
8609
8610 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008611 (*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 +01008612 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8613 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008614 goto out_err;
8615 }
8616
8617 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8618 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8619 LIST_INIT(&rule->arg.hdr_add.fmt);
8620
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008621 error = NULL;
8622 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008623 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8624 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008625 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008626 goto out_err;
8627 }
8628
8629 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008630 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008631 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 +01008632 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008633 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8634 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008635 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008636 goto out_err;
8637 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008638
8639 free(proxy->conf.lfs_file);
8640 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8641 proxy->conf.lfs_line = proxy->conf.args.line;
8642 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008643 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008644 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008645 cur_arg = 1;
8646
8647 if (!*args[cur_arg] ||
8648 (*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 +01008649 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8650 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008651 goto out_err;
8652 }
8653
8654 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8655 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8656
8657 proxy->conf.args.ctx = ARGC_HRQ;
8658 free(proxy->conf.lfs_file);
8659 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8660 proxy->conf.lfs_line = proxy->conf.args.line;
8661 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008662 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008663 struct sample_expr *expr;
8664 unsigned int where;
8665 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008666 unsigned int tsc_num;
8667 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008668
8669 cur_arg = 1;
8670 proxy->conf.args.ctx = ARGC_TRK;
8671
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008672 tsc_num_str = &args[0][8];
8673 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8674 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8675 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8676 free(err);
8677 goto out_err;
8678 }
8679
Willy Tarreau09448f72014-06-25 18:12:15 +02008680 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8681 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008682 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8683 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008684 free(err);
8685 goto out_err;
8686 }
8687
8688 where = 0;
8689 if (proxy->cap & PR_CAP_FE)
8690 where |= SMP_VAL_FE_HRQ_HDR;
8691 if (proxy->cap & PR_CAP_BE)
8692 where |= SMP_VAL_BE_HRQ_HDR;
8693
8694 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008695 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8696 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8697 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8698 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008699 free(expr);
8700 goto out_err;
8701 }
8702
8703 if (strcmp(args[cur_arg], "table") == 0) {
8704 cur_arg++;
8705 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008706 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8707 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008708 free(expr);
8709 goto out_err;
8710 }
8711 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008712 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008713 cur_arg++;
8714 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008715 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008716 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008717 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008718 } else if (strcmp(args[0], "redirect") == 0) {
8719 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008720 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008721
Willy Tarreaube4653b2015-05-28 15:26:58 +02008722 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008723 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8724 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008725 goto out_err;
8726 }
8727
8728 /* this redirect rule might already contain a parsed condition which
8729 * we'll pass to the http-request rule.
8730 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008731 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008732 rule->arg.redir = redir;
8733 rule->cond = redir->cond;
8734 redir->cond = NULL;
8735 cur_arg = 2;
8736 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008737 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8738 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008739 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008740 /*
8741 * '+ 8' for 'add-acl('
8742 * '- 9' for 'add-acl(' + trailing ')'
8743 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008744 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008745
8746 cur_arg = 1;
8747
8748 if (!*args[cur_arg] ||
8749 (*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 +01008750 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8751 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008752 goto out_err;
8753 }
8754
8755 LIST_INIT(&rule->arg.map.key);
8756 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008757 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008758 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008759 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008760 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8761 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008762 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008763 goto out_err;
8764 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008765 free(proxy->conf.lfs_file);
8766 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8767 proxy->conf.lfs_line = proxy->conf.args.line;
8768 cur_arg += 1;
8769 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8770 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008771 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008772 /*
8773 * '+ 8' for 'del-acl('
8774 * '- 9' for 'del-acl(' + trailing ')'
8775 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008776 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008777
8778 cur_arg = 1;
8779
8780 if (!*args[cur_arg] ||
8781 (*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 +01008782 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8783 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008784 goto out_err;
8785 }
8786
8787 LIST_INIT(&rule->arg.map.key);
8788 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008789 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008790 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008791 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008792 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8793 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008794 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008795 goto out_err;
8796 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008797 free(proxy->conf.lfs_file);
8798 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8799 proxy->conf.lfs_line = proxy->conf.args.line;
8800 cur_arg += 1;
8801 } else if (strncmp(args[0], "del-map", 7) == 0) {
8802 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008803 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008804 /*
8805 * '+ 8' for 'del-map('
8806 * '- 9' for 'del-map(' + trailing ')'
8807 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008808 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008809
8810 cur_arg = 1;
8811
8812 if (!*args[cur_arg] ||
8813 (*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 +01008814 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8815 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008816 goto out_err;
8817 }
8818
8819 LIST_INIT(&rule->arg.map.key);
8820 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008821 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008822 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008823 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008824 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8825 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008826 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008827 goto out_err;
8828 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008829 free(proxy->conf.lfs_file);
8830 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8831 proxy->conf.lfs_line = proxy->conf.args.line;
8832 cur_arg += 1;
8833 } else if (strncmp(args[0], "set-map", 7) == 0) {
8834 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008835 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008836 /*
8837 * '+ 8' for 'set-map('
8838 * '- 9' for 'set-map(' + trailing ')'
8839 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008840 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008841
8842 cur_arg = 1;
8843
8844 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8845 (*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 +01008846 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8847 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008848 goto out_err;
8849 }
8850
8851 LIST_INIT(&rule->arg.map.key);
8852 LIST_INIT(&rule->arg.map.value);
8853 proxy->conf.args.ctx = ARGC_HRQ;
8854
8855 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008856 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008857 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008858 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008859 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8860 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008861 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008862 goto out_err;
8863 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008864
8865 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008866 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008867 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 +01008868 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008869 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8870 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008871 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008872 goto out_err;
8873 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008874 free(proxy->conf.lfs_file);
8875 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8876 proxy->conf.lfs_line = proxy->conf.args.line;
8877
8878 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008879 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8880 char *errmsg = NULL;
8881 cur_arg = 1;
8882 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008883 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008884 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008885 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008886 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8887 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008888 free(errmsg);
8889 goto out_err;
8890 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008891 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008892 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008893 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8894 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8895 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8896 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008897 file, linenum, *trash.area ? ", " : "", trash.area,
8898 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008899 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008900 }
8901
8902 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8903 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008904 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008905
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008906 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008907 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8908 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008909 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008910 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008911 }
8912 rule->cond = cond;
8913 }
8914 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008915 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8916 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8917 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008918 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008919 }
8920
8921 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008922 out_err:
8923 free(rule);
8924 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008925}
8926
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008927/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008928struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008929{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008930 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008931 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008932 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008933 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008934
8935 rule = calloc(1, sizeof(*rule));
8936 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008937 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008938 goto out_err;
8939 }
8940
8941 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008942 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008943 cur_arg = 1;
8944 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008945 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008946 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008947 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008948 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008949 cur_arg = 1;
8950
8951 if (!*args[cur_arg] ||
8952 (*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 +01008953 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8954 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008955 goto out_err;
8956 }
8957 rule->arg.nice = atoi(args[cur_arg]);
8958 if (rule->arg.nice < -1024)
8959 rule->arg.nice = -1024;
8960 else if (rule->arg.nice > 1024)
8961 rule->arg.nice = 1024;
8962 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008963 } else if (!strcmp(args[0], "set-tos")) {
8964#ifdef IP_TOS
8965 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008966 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008967 cur_arg = 1;
8968
8969 if (!*args[cur_arg] ||
8970 (*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 +01008971 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8972 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008973 goto out_err;
8974 }
8975
8976 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8977 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008978 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8979 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008980 goto out_err;
8981 }
8982 cur_arg++;
8983#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008984 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 +02008985 goto out_err;
8986#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008987 } else if (!strcmp(args[0], "set-mark")) {
8988#ifdef SO_MARK
8989 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008990 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008991 cur_arg = 1;
8992
8993 if (!*args[cur_arg] ||
8994 (*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 +01008995 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8996 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008997 goto out_err;
8998 }
8999
9000 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9001 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009002 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9003 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02009004 goto out_err;
9005 }
9006 cur_arg++;
9007 global.last_checks |= LSTCHK_NETADM;
9008#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01009009 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 +02009010 goto out_err;
9011#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009012 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009013 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009014 cur_arg = 1;
9015
9016 if (!*args[cur_arg] ||
9017 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9018 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01009019 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9020 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009021 goto out_err;
9022 }
9023 if (strcmp(args[cur_arg], "silent") == 0)
9024 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009025 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009026 goto bad_log_level;
9027 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009028 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009029 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009030 cur_arg = 1;
9031
9032 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9033 (*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 +01009034 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9035 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009036 goto out_err;
9037 }
9038
9039 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9040 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9041 LIST_INIT(&rule->arg.hdr_add.fmt);
9042
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009043 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009044 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009045 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 +01009046 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009047 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9048 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009049 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009050 goto out_err;
9051 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009052 free(proxy->conf.lfs_file);
9053 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9054 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009055 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009056 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009057 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009058 cur_arg = 1;
9059
9060 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009061 (*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 +01009062 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
9063 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06009064 goto out_err;
9065 }
9066
9067 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9068 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9069 LIST_INIT(&rule->arg.hdr_add.fmt);
9070
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009071 error = NULL;
9072 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009073 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9074 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009075 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009076 goto out_err;
9077 }
9078
9079 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009080 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009081 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 +01009082 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009083 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9084 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009085 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009086 goto out_err;
9087 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009088
9089 free(proxy->conf.lfs_file);
9090 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9091 proxy->conf.lfs_line = proxy->conf.args.line;
9092 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009093 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009094 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009095 cur_arg = 1;
9096
9097 if (!*args[cur_arg] ||
9098 (*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 +01009099 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9100 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009101 goto out_err;
9102 }
9103
9104 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9105 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9106
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009107 proxy->conf.args.ctx = ARGC_HRS;
9108 free(proxy->conf.lfs_file);
9109 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9110 proxy->conf.lfs_line = proxy->conf.args.line;
9111 cur_arg += 1;
9112 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9113 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009114 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009115 /*
9116 * '+ 8' for 'add-acl('
9117 * '- 9' for 'add-acl(' + trailing ')'
9118 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009119 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009120
9121 cur_arg = 1;
9122
9123 if (!*args[cur_arg] ||
9124 (*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 +01009125 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9126 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009127 goto out_err;
9128 }
9129
9130 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009131 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009132 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009133 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009134 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009135 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9136 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009137 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009138 goto out_err;
9139 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009140 free(proxy->conf.lfs_file);
9141 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9142 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009143
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009144 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009145 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9146 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009147 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009148 /*
9149 * '+ 8' for 'del-acl('
9150 * '- 9' for 'del-acl(' + trailing ')'
9151 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009152 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009153
9154 cur_arg = 1;
9155
9156 if (!*args[cur_arg] ||
9157 (*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 +01009158 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9159 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009160 goto out_err;
9161 }
9162
9163 LIST_INIT(&rule->arg.map.key);
9164 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009165 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009166 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009167 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009168 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9169 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009170 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009171 goto out_err;
9172 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009173 free(proxy->conf.lfs_file);
9174 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9175 proxy->conf.lfs_line = proxy->conf.args.line;
9176 cur_arg += 1;
9177 } else if (strncmp(args[0], "del-map", 7) == 0) {
9178 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009179 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009180 /*
9181 * '+ 8' for 'del-map('
9182 * '- 9' for 'del-map(' + trailing ')'
9183 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009184 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009185
9186 cur_arg = 1;
9187
9188 if (!*args[cur_arg] ||
9189 (*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 +01009190 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9191 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009192 goto out_err;
9193 }
9194
9195 LIST_INIT(&rule->arg.map.key);
9196 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009197 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009198 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009199 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009200 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9201 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009202 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009203 goto out_err;
9204 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009205 free(proxy->conf.lfs_file);
9206 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9207 proxy->conf.lfs_line = proxy->conf.args.line;
9208 cur_arg += 1;
9209 } else if (strncmp(args[0], "set-map", 7) == 0) {
9210 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009211 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009212 /*
9213 * '+ 8' for 'set-map('
9214 * '- 9' for 'set-map(' + trailing ')'
9215 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009216 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009217
9218 cur_arg = 1;
9219
9220 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9221 (*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 +01009222 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9223 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009224 goto out_err;
9225 }
9226
9227 LIST_INIT(&rule->arg.map.key);
9228 LIST_INIT(&rule->arg.map.value);
9229
9230 proxy->conf.args.ctx = ARGC_HRS;
9231
9232 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009233 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009234 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009235 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009236 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9237 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009238 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009239 goto out_err;
9240 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009241
9242 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009243 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009244 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 +01009245 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009246 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9247 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009248 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009249 goto out_err;
9250 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009251
9252 free(proxy->conf.lfs_file);
9253 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9254 proxy->conf.lfs_line = proxy->conf.args.line;
9255
9256 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009257 } else if (strcmp(args[0], "redirect") == 0) {
9258 struct redirect_rule *redir;
9259 char *errmsg = NULL;
9260
9261 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009262 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9263 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009264 goto out_err;
9265 }
9266
9267 /* this redirect rule might already contain a parsed condition which
9268 * we'll pass to the http-request rule.
9269 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009270 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009271 rule->arg.redir = redir;
9272 rule->cond = redir->cond;
9273 redir->cond = NULL;
9274 cur_arg = 2;
9275 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009276 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009277 struct sample_expr *expr;
9278 unsigned int where;
9279 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009280 unsigned int tsc_num;
9281 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009282
9283 cur_arg = 1;
9284 proxy->conf.args.ctx = ARGC_TRK;
9285
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009286 tsc_num_str = &args[0][8];
9287 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9288 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9289 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9290 free(err);
9291 goto out_err;
9292 }
9293
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009294 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9295 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009296 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9297 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009298 free(err);
9299 goto out_err;
9300 }
9301
9302 where = 0;
9303 if (proxy->cap & PR_CAP_FE)
9304 where |= SMP_VAL_FE_HRS_HDR;
9305 if (proxy->cap & PR_CAP_BE)
9306 where |= SMP_VAL_BE_HRS_HDR;
9307
9308 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009309 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9310 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9311 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9312 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009313 free(expr);
9314 goto out_err;
9315 }
9316
9317 if (strcmp(args[cur_arg], "table") == 0) {
9318 cur_arg++;
9319 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009320 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9321 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009322 free(expr);
9323 goto out_err;
9324 }
9325 /* we copy the table name for now, it will be resolved later */
9326 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9327 cur_arg++;
9328 }
9329 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009330 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009331 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009332 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9333 char *errmsg = NULL;
9334 cur_arg = 1;
9335 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009336 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009337 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009338 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009339 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9340 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009341 free(errmsg);
9342 goto out_err;
9343 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009344 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009345 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009346 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9347 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9348 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9349 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009350 file, linenum, *trash.area ? ", " : "", trash.area,
9351 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009352 goto out_err;
9353 }
9354
9355 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9356 struct acl_cond *cond;
9357 char *errmsg = NULL;
9358
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009359 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009360 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9361 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009362 free(errmsg);
9363 goto out_err;
9364 }
9365 rule->cond = cond;
9366 }
9367 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009368 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9369 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9370 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009371 goto out_err;
9372 }
9373
9374 return rule;
9375 out_err:
9376 free(rule);
9377 return NULL;
9378}
9379
Willy Tarreau4baae242012-12-27 12:00:31 +01009380/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009381 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009382 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9383 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009384 */
9385struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009386 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009387{
9388 struct redirect_rule *rule;
9389 int cur_arg;
9390 int type = REDIRECT_TYPE_NONE;
9391 int code = 302;
9392 const char *destination = NULL;
9393 const char *cookie = NULL;
9394 int cookie_set = 0;
9395 unsigned int flags = REDIRECT_FLAG_NONE;
9396 struct acl_cond *cond = NULL;
9397
9398 cur_arg = 0;
9399 while (*(args[cur_arg])) {
9400 if (strcmp(args[cur_arg], "location") == 0) {
9401 if (!*args[cur_arg + 1])
9402 goto missing_arg;
9403
9404 type = REDIRECT_TYPE_LOCATION;
9405 cur_arg++;
9406 destination = args[cur_arg];
9407 }
9408 else if (strcmp(args[cur_arg], "prefix") == 0) {
9409 if (!*args[cur_arg + 1])
9410 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009411 type = REDIRECT_TYPE_PREFIX;
9412 cur_arg++;
9413 destination = args[cur_arg];
9414 }
9415 else if (strcmp(args[cur_arg], "scheme") == 0) {
9416 if (!*args[cur_arg + 1])
9417 goto missing_arg;
9418
9419 type = REDIRECT_TYPE_SCHEME;
9420 cur_arg++;
9421 destination = args[cur_arg];
9422 }
9423 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9424 if (!*args[cur_arg + 1])
9425 goto missing_arg;
9426
9427 cur_arg++;
9428 cookie = args[cur_arg];
9429 cookie_set = 1;
9430 }
9431 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9432 if (!*args[cur_arg + 1])
9433 goto missing_arg;
9434
9435 cur_arg++;
9436 cookie = args[cur_arg];
9437 cookie_set = 0;
9438 }
9439 else if (strcmp(args[cur_arg], "code") == 0) {
9440 if (!*args[cur_arg + 1])
9441 goto missing_arg;
9442
9443 cur_arg++;
9444 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009445 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009446 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009447 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009448 args[cur_arg - 1], args[cur_arg]);
9449 return NULL;
9450 }
9451 }
9452 else if (!strcmp(args[cur_arg],"drop-query")) {
9453 flags |= REDIRECT_FLAG_DROP_QS;
9454 }
9455 else if (!strcmp(args[cur_arg],"append-slash")) {
9456 flags |= REDIRECT_FLAG_APPEND_SLASH;
9457 }
9458 else if (strcmp(args[cur_arg], "if") == 0 ||
9459 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009460 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009461 if (!cond) {
9462 memprintf(errmsg, "error in condition: %s", *errmsg);
9463 return NULL;
9464 }
9465 break;
9466 }
9467 else {
9468 memprintf(errmsg,
9469 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9470 args[cur_arg]);
9471 return NULL;
9472 }
9473 cur_arg++;
9474 }
9475
9476 if (type == REDIRECT_TYPE_NONE) {
9477 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9478 return NULL;
9479 }
9480
Willy Tarreaube4653b2015-05-28 15:26:58 +02009481 if (dir && type != REDIRECT_TYPE_LOCATION) {
9482 memprintf(errmsg, "response only supports redirect type 'location'");
9483 return NULL;
9484 }
9485
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009486 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009487 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009488 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009489
9490 if (!use_fmt) {
9491 /* old-style static redirect rule */
9492 rule->rdr_str = strdup(destination);
9493 rule->rdr_len = strlen(destination);
9494 }
9495 else {
9496 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009497
9498 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9499 * if prefix == "/", we don't want to add anything, otherwise it
9500 * makes it hard for the user to configure a self-redirection.
9501 */
Godbachd9722032014-12-18 15:44:58 +08009502 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009503 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009504 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9505 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 +01009506 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9507 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009508 return NULL;
9509 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009510 free(curproxy->conf.lfs_file);
9511 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9512 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009513 }
9514 }
9515
Willy Tarreau4baae242012-12-27 12:00:31 +01009516 if (cookie) {
9517 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9518 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9519 */
9520 rule->cookie_len = strlen(cookie);
9521 if (cookie_set) {
9522 rule->cookie_str = malloc(rule->cookie_len + 10);
9523 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9524 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9525 rule->cookie_len += 9;
9526 } else {
9527 rule->cookie_str = malloc(rule->cookie_len + 21);
9528 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9529 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9530 rule->cookie_len += 20;
9531 }
9532 }
9533 rule->type = type;
9534 rule->code = code;
9535 rule->flags = flags;
9536 LIST_INIT(&rule->list);
9537 return rule;
9538
9539 missing_arg:
9540 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9541 return NULL;
9542}
9543
Willy Tarreau8797c062007-05-07 00:55:35 +02009544/************************************************************************/
9545/* The code below is dedicated to ACL parsing and matching */
9546/************************************************************************/
9547
9548
Willy Tarreau14174bc2012-04-16 14:34:04 +02009549/* This function ensures that the prerequisites for an L7 fetch are ready,
9550 * which means that a request or response is ready. If some data is missing,
9551 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009552 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9553 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009554 *
9555 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009556 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9557 * decide whether or not an HTTP message is present ;
9558 * 0 if the requested data cannot be fetched or if it is certain that
9559 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009560 * 1 if an HTTP message is ready
9561 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009562int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009563 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009564{
Willy Tarreau192252e2015-04-04 01:47:55 +02009565 struct http_txn *txn;
9566 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009567
Willy Tarreaube508f12016-03-10 11:47:01 +01009568 /* Note: it is possible that <s> is NULL when called before stream
9569 * initialization (eg: tcp-request connection), so this function is the
9570 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009571 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009572 if (!s)
9573 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009574
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009575 if (!s->txn) {
9576 if (unlikely(!http_alloc_txn(s)))
9577 return 0; /* not enough memory */
9578 http_init_txn(s);
9579 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009580 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009581 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009582
9583 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009584 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009585
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009586 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009587 /* If the buffer does not leave enough free space at the end,
9588 * we must first realign it.
9589 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009590 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
9591 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009592 channel_slow_realign(&s->req, trash.area);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009593
Willy Tarreau14174bc2012-04-16 14:34:04 +02009594 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009595 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009596 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009597
9598 /* Try to decode HTTP request */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009599 if (likely(msg->next < ci_data(&s->req)))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009600 http_msg_analyzer(msg, &txn->hdr_idx);
9601
9602 /* Still no valid request ? */
9603 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009604 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02009605 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009606 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009607 }
9608 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009609 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009610 return 0;
9611 }
9612
9613 /* OK we just got a valid HTTP request. We have some minor
9614 * preparation to perform so that further checks can rely
9615 * on HTTP tests.
9616 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009617
9618 /* If the request was parsed but was too large, we must absolutely
9619 * return an error so that it is not processed. At the moment this
9620 * cannot happen, but if the parsers are to change in the future,
9621 * we want this check to be maintained.
9622 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009623 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009624 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009625 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009626 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009627 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009628 return 1;
9629 }
9630
Willy Tarreauf37954d2018-06-15 18:31:02 +02009631 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009632 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009633 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009634
Willy Tarreau506d0502013-07-06 13:29:24 +02009635 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9636 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009637 }
9638
Willy Tarreau506d0502013-07-06 13:29:24 +02009639 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009640 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009641 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009642
9643 /* otherwise everything's ready for the request */
9644 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009645 else {
9646 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009647 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9648 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009649 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009650 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009651 }
9652
9653 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009654 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009655 return 1;
9656}
Willy Tarreau8797c062007-05-07 00:55:35 +02009657
Willy Tarreau8797c062007-05-07 00:55:35 +02009658/* 1. Check on METHOD
9659 * We use the pre-parsed method if it is known, and store its number as an
9660 * integer. If it is unknown, we use the pointer and the length.
9661 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009662static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009663{
9664 int len, meth;
9665
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009666 len = strlen(text);
9667 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009668
9669 pattern->val.i = meth;
9670 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009671 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009672 pattern->len = len;
9673 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009674 else {
9675 pattern->ptr.str = NULL;
9676 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009677 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009678 return 1;
9679}
9680
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009681/* This function fetches the method of current HTTP request and stores
9682 * it in the global pattern struct as a chunk. There are two possibilities :
9683 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9684 * in <len> and <ptr> is NULL ;
9685 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9686 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009687 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009688 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009689static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009690smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009691{
9692 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009693 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009694
Willy Tarreau24e32d82012-04-23 23:55:44 +02009695 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009696
Willy Tarreaube508f12016-03-10 11:47:01 +01009697 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009698 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009699 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009700 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009701 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009702 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9703 /* ensure the indexes are not affected */
9704 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009705 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009706 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
9707 smp->data.u.meth.str.area = ci_head(txn->req.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009708 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009709 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009710 return 1;
9711}
9712
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009713/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009714static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009715{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009716 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009717 struct pattern_list *lst;
9718 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009719
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009720 list_for_each_entry(lst, &expr->patterns, list) {
9721 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009722
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009723 /* well-known method */
9724 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009725 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009726 return pattern;
9727 else
9728 continue;
9729 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009730
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009731 /* Other method, we must compare the strings */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009732 if (pattern->len != smp->data.u.meth.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009733 continue;
9734
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009735 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009736 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) ||
9737 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009738 return pattern;
9739 }
9740 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009741}
9742
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009743static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009744smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009745{
Willy Tarreaube508f12016-03-10 11:47:01 +01009746 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009747 char *ptr;
9748 int len;
9749
Willy Tarreauc0239e02012-04-16 14:42:55 +02009750 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009751
Willy Tarreaube508f12016-03-10 11:47:01 +01009752 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009753 len = txn->req.sl.rq.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009754 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009755
9756 while ((len-- > 0) && (*ptr++ != '/'));
9757 if (len <= 0)
9758 return 0;
9759
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009760 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009761 smp->data.u.str.area = ptr;
9762 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009763
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009764 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009765 return 1;
9766}
9767
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009768static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009769smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009770{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009771 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009772 char *ptr;
9773 int len;
9774
Willy Tarreauc0239e02012-04-16 14:42:55 +02009775 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009776
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009777 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009778 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9779 return 0;
9780
Willy Tarreau8797c062007-05-07 00:55:35 +02009781 len = txn->rsp.sl.st.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009782 ptr = ci_head(txn->rsp.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009783
9784 while ((len-- > 0) && (*ptr++ != '/'));
9785 if (len <= 0)
9786 return 0;
9787
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009788 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009789 smp->data.u.str.area = ptr;
9790 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009791
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009792 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009793 return 1;
9794}
9795
9796/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009797static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009798smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009799{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009800 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009801 char *ptr;
9802 int len;
9803
Willy Tarreauc0239e02012-04-16 14:42:55 +02009804 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009805
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009806 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009807 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9808 return 0;
9809
Willy Tarreau8797c062007-05-07 00:55:35 +02009810 len = txn->rsp.sl.st.c_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009811 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009812
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009813 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009814 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009815 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009816 return 1;
9817}
9818
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009819static int
9820smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9821{
9822 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9823 return 0;
9824
9825 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009826 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009827 return 0;
9828 smp->strm->unique_id[0] = '\0';
9829 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009830 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009831 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9832
9833 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009834 smp->data.u.str.area = smp->strm->unique_id;
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009835 smp->flags = SMP_F_CONST;
9836 return 1;
9837}
9838
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009839/* Returns a string block containing all headers including the
9840 * empty line wich separes headers from the body. This is useful
9841 * form some headers analysis.
9842 */
9843static int
9844smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9845{
9846 struct http_msg *msg;
9847 struct hdr_idx *idx;
9848 struct http_txn *txn;
9849
9850 CHECK_HTTP_MESSAGE_FIRST();
9851
9852 txn = smp->strm->txn;
9853 idx = &txn->hdr_idx;
9854 msg = &txn->req;
9855
9856 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009857 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
9858 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Willy Tarreauf37954d2018-06-15 18:31:02 +02009859 (ci_head(msg->chn)[msg->eoh] == '\r');
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009860
9861 return 1;
9862}
9863
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009864/* Returns the header request in a length/value encoded format.
9865 * This is useful for exchanges with the SPOE.
9866 *
9867 * A "length value" is a multibyte code encoding numbers. It uses the
9868 * SPOE format. The encoding is the following:
9869 *
9870 * Each couple "header name" / "header value" is composed
9871 * like this:
9872 * "length value" "header name bytes"
9873 * "length value" "header value bytes"
9874 * When the last header is reached, the header name and the header
9875 * value are empty. Their length are 0
9876 */
9877static int
9878smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9879{
9880 struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009881 struct buffer *temp;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009882 struct hdr_idx *idx;
9883 const char *cur_ptr, *cur_next, *p;
9884 int old_idx, cur_idx;
9885 struct hdr_idx_elem *cur_hdr;
9886 const char *hn, *hv;
9887 int hnl, hvl;
9888 int ret;
9889 struct http_txn *txn;
9890 char *buf;
9891 char *end;
9892
9893 CHECK_HTTP_MESSAGE_FIRST();
9894
9895 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009896 buf = temp->area;
9897 end = temp->area + temp->size;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009898
9899 txn = smp->strm->txn;
9900 idx = &txn->hdr_idx;
9901 msg = &txn->req;
9902
9903 /* Build array of headers. */
9904 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009905 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009906 while (1) {
9907 cur_idx = idx->v[old_idx].next;
9908 if (!cur_idx)
9909 break;
9910 old_idx = cur_idx;
9911
9912 cur_hdr = &idx->v[cur_idx];
9913 cur_ptr = cur_next;
9914 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9915
9916 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9917 * and the next header starts at cur_next. We'll check
9918 * this header in the list as well as against the default
9919 * rule.
9920 */
9921
9922 /* look for ': *'. */
9923 hn = cur_ptr;
9924 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9925 if (p >= cur_ptr+cur_hdr->len)
9926 continue;
9927 hnl = p - hn;
9928 p++;
9929 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9930 p++;
9931 if (p >= cur_ptr + cur_hdr->len)
9932 continue;
9933 hv = p;
9934 hvl = cur_ptr + cur_hdr->len-p;
9935
9936 /* encode the header name. */
9937 ret = encode_varint(hnl, &buf, end);
9938 if (ret == -1)
9939 return 0;
9940 if (buf + hnl > end)
9941 return 0;
9942 memcpy(buf, hn, hnl);
9943 buf += hnl;
9944
9945 /* encode and copy the value. */
9946 ret = encode_varint(hvl, &buf, end);
9947 if (ret == -1)
9948 return 0;
9949 if (buf + hvl > end)
9950 return 0;
9951 memcpy(buf, hv, hvl);
9952 buf += hvl;
9953 }
9954
9955 /* encode the end of the header list with empty
9956 * header name and header value.
9957 */
9958 ret = encode_varint(0, &buf, end);
9959 if (ret == -1)
9960 return 0;
9961 ret = encode_varint(0, &buf, end);
9962 if (ret == -1)
9963 return 0;
9964
9965 /* Initialise sample data which will be filled. */
9966 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009967 smp->data.u.str.area = temp->area;
9968 smp->data.u.str.data = buf - temp->area;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009969 smp->data.u.str.size = temp->size;
9970
9971 return 1;
9972}
9973
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009974/* returns the longest available part of the body. This requires that the body
9975 * has been waited for using http-buffer-request.
9976 */
9977static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009978smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009979{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009980 struct http_msg *msg;
9981 unsigned long len;
9982 unsigned long block1;
9983 char *body;
Willy Tarreau83061a82018-07-13 11:56:34 +02009984 struct buffer *temp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009985
9986 CHECK_HTTP_MESSAGE_FIRST();
9987
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009988 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009989 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009990 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009991 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009992
9993 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009994 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009995
9996 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009997 if (block1 > b_wrap(&msg->chn->buf) - body)
9998 block1 = b_wrap(&msg->chn->buf) - body;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009999
10000 if (block1 == len) {
10001 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010002 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010003 smp->data.u.str.area = body;
10004 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010005 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10006 }
10007 else {
10008 /* buffer is wrapped, we need to defragment it */
10009 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010010 memcpy(temp->area, body, block1);
10011 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
10012 len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010013 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010014 smp->data.u.str.area = temp->area;
10015 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010016 smp->flags = SMP_F_VOL_TEST;
10017 }
10018 return 1;
10019}
10020
10021
10022/* returns the available length of the body. This requires that the body
10023 * has been waited for using http-buffer-request.
10024 */
10025static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010026smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010027{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010028 struct http_msg *msg;
10029
10030 CHECK_HTTP_MESSAGE_FIRST();
10031
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010032 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010033 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010034 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010035 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010036
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010037 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010038 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010039
10040 smp->flags = SMP_F_VOL_TEST;
10041 return 1;
10042}
10043
10044
10045/* returns the advertised length of the body, or the advertised size of the
10046 * chunks available in the buffer. This requires that the body has been waited
10047 * for using http-buffer-request.
10048 */
10049static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010050smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010051{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010052 struct http_msg *msg;
10053
10054 CHECK_HTTP_MESSAGE_FIRST();
10055
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010056 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010057 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010058 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010059 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010060
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010061 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010062 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010063
10064 smp->flags = SMP_F_VOL_TEST;
10065 return 1;
10066}
10067
10068
Willy Tarreau8797c062007-05-07 00:55:35 +020010069/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010070static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010071smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010072{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010073 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010074
Willy Tarreauc0239e02012-04-16 14:42:55 +020010075 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010076
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010077 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010078 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010079 smp->data.u.str.data = txn->req.sl.rq.u_l;
10080 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010081 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010082 return 1;
10083}
10084
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010085static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010086smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010087{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010088 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010089 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010090
Willy Tarreauc0239e02012-04-16 14:42:55 +020010091 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010092
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010093 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010094 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010095 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010096 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010097
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010098 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010099 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010100 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010101 return 1;
10102}
10103
10104static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010105smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010106{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010107 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010108 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010109
Willy Tarreauc0239e02012-04-16 14:42:55 +020010110 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010111
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010112 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010113 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010114 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10115 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010116
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010117 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010118 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010119 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010120 return 1;
10121}
10122
Willy Tarreau185b5c42012-04-26 15:11:51 +020010123/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10124 * Accepts an optional argument of type string containing the header field name,
10125 * and an optional argument of type signed or unsigned integer to request an
10126 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010127 * headers are considered from the first one. It does not stop on commas and
10128 * returns full lines instead (useful for User-Agent or Date for example).
10129 */
10130static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010131smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010132{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010133 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010134 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010135 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010136 int occ = 0;
10137 const char *name_str = NULL;
10138 int name_len = 0;
10139
10140 if (!ctx) {
10141 /* first call */
10142 ctx = &static_hdr_ctx;
10143 ctx->idx = 0;
10144 smp->ctx.a[0] = ctx;
10145 }
10146
10147 if (args) {
10148 if (args[0].type != ARGT_STR)
10149 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010150 name_str = args[0].data.str.area;
10151 name_len = args[0].data.str.data;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010152
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010153 if (args[1].type == ARGT_SINT)
10154 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010155 }
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 Tarreau04ff9f12013-06-10 18:39:42 +020010162 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10163 /* search for header from the beginning */
10164 ctx->idx = 0;
10165
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010166 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010167 /* no explicit occurrence and single fetch => last header by default */
10168 occ = -1;
10169
10170 if (!occ)
10171 /* prepare to report multiple occurrences for ACL fetches */
10172 smp->flags |= SMP_F_NOT_LAST;
10173
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010174 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010175 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010176 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010177 return 1;
10178
10179 smp->flags &= ~SMP_F_NOT_LAST;
10180 return 0;
10181}
10182
10183/* 6. Check on HTTP header count. The number of occurrences is returned.
10184 * Accepts exactly 1 argument of type string. It does not stop on commas and
10185 * returns full lines instead (useful for User-Agent or Date for example).
10186 */
10187static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010188smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010189{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010190 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010191 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010192 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010193 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010194 const char *name = NULL;
10195 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010196
Willy Tarreau601a4d12015-04-01 19:16:09 +020010197 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010198 name = args->data.str.area;
10199 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010200 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010201
10202 CHECK_HTTP_MESSAGE_FIRST();
10203
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010204 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010205 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 +020010206
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010207 ctx.idx = 0;
10208 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010209 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010210 cnt++;
10211
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010212 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010213 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010214 smp->flags = SMP_F_VOL_HDR;
10215 return 1;
10216}
10217
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010218static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010219smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010220{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010221 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010222 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010223 const struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +020010224 struct buffer *temp;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010225 char del = ',';
10226
10227 if (args && args->type == ARGT_STR)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010228 del = *args[0].data.str.area;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010229
10230 CHECK_HTTP_MESSAGE_FIRST();
10231
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010232 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010233 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 +020010234
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010235 temp = get_trash_chunk();
10236
10237 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010238 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010239 if (temp->data)
10240 temp->area[temp->data++] = del;
10241 memcpy(temp->area + temp->data, ctx.line, ctx.del);
10242 temp->data += ctx.del;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010243 }
10244
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010245 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010246 smp->data.u.str.area = temp->area;
10247 smp->data.u.str.data = temp->data;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010248 smp->flags = SMP_F_VOL_HDR;
10249 return 1;
10250}
10251
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010252/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10253 * Accepts an optional argument of type string containing the header field name,
10254 * and an optional argument of type signed or unsigned integer to request an
10255 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010256 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010257 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010258static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010259smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010260{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010261 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010262 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010263 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010264 int occ = 0;
10265 const char *name_str = NULL;
10266 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010267
Willy Tarreaua890d072013-04-02 12:01:06 +020010268 if (!ctx) {
10269 /* first call */
10270 ctx = &static_hdr_ctx;
10271 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010272 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010273 }
10274
Willy Tarreau185b5c42012-04-26 15:11:51 +020010275 if (args) {
10276 if (args[0].type != ARGT_STR)
10277 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010278 name_str = args[0].data.str.area;
10279 name_len = args[0].data.str.data;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010280
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010281 if (args[1].type == ARGT_SINT)
10282 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010283 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010284
Willy Tarreaue333ec92012-04-16 16:26:40 +020010285 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010286
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010287 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010288 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 +020010289
Willy Tarreau185b5c42012-04-26 15:11:51 +020010290 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010291 /* search for header from the beginning */
10292 ctx->idx = 0;
10293
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010294 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010295 /* no explicit occurrence and single fetch => last header by default */
10296 occ = -1;
10297
10298 if (!occ)
10299 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010300 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010301
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010302 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010303 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010304 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010305 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010306
Willy Tarreau37406352012-04-23 16:16:37 +020010307 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010308 return 0;
10309}
10310
Willy Tarreauc11416f2007-06-17 16:58:38 +020010311/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010312 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010313 */
10314static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010315smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010316{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010317 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010318 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010319 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010320 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010321 const char *name = NULL;
10322 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010323
Willy Tarreau601a4d12015-04-01 19:16:09 +020010324 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010325 name = args->data.str.area;
10326 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010327 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010328
Willy Tarreaue333ec92012-04-16 16:26:40 +020010329 CHECK_HTTP_MESSAGE_FIRST();
10330
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010331 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010332 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 +020010333
Willy Tarreau33a7e692007-06-10 19:45:56 +020010334 ctx.idx = 0;
10335 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010336 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010337 cnt++;
10338
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010339 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010340 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010341 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010342 return 1;
10343}
10344
Willy Tarreau185b5c42012-04-26 15:11:51 +020010345/* Fetch an HTTP header's integer value. The integer value is returned. It
10346 * takes a mandatory argument of type string and an optional one of type int
10347 * to designate a specific occurrence. It returns an unsigned integer, which
10348 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010349 */
10350static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010351smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010352{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010353 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010354
Willy Tarreauf853c462012-04-23 18:53:56 +020010355 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010356 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010357 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10358 smp->data.u.str.data);
Willy Tarreauf853c462012-04-23 18:53:56 +020010359 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010360
Willy Tarreaud53e2422012-04-16 17:21:11 +020010361 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010362}
10363
Cyril Bonté69fa9922012-10-25 00:01:06 +020010364/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10365 * and an optional one of type int to designate a specific occurrence.
10366 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010367 */
10368static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010369smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010370{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010371 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010372
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010373 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010374 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010375 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010376 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010377 } else {
Willy Tarreau83061a82018-07-13 11:56:34 +020010378 struct buffer *temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010379 if (smp->data.u.str.data < temp->size - 1) {
10380 memcpy(temp->area, smp->data.u.str.area,
10381 smp->data.u.str.data);
10382 temp->area[smp->data.u.str.data] = '\0';
10383 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010384 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010385 break;
10386 }
10387 }
10388 }
10389
Willy Tarreaud53e2422012-04-16 17:21:11 +020010390 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010391 if (!(smp->flags & SMP_F_NOT_LAST))
10392 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010393 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010394 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010395}
10396
Willy Tarreau737b0c12007-06-10 21:28:46 +020010397/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10398 * the first '/' after the possible hostname, and ends before the possible '?'.
10399 */
10400static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010401smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010402{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010403 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010404 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010405
Willy Tarreauc0239e02012-04-16 14:42:55 +020010406 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010407
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010408 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010409 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010410 ptr = http_get_path(txn);
10411 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010412 return 0;
10413
10414 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010415 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010416 smp->data.u.str.area = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010417
10418 while (ptr < end && *ptr != '?')
10419 ptr++;
10420
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010421 smp->data.u.str.data = ptr - smp->data.u.str.area;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010422 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010423 return 1;
10424}
10425
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010426/* This produces a concatenation of the first occurrence of the Host header
10427 * followed by the path component if it begins with a slash ('/'). This means
10428 * that '*' will not be added, resulting in exactly the first Host entry.
10429 * If no Host header is found, then the path is returned as-is. The returned
10430 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010431 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010432 */
10433static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010434smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010435{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010436 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010437 char *ptr, *end, *beg;
10438 struct hdr_ctx ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +020010439 struct buffer *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010440
10441 CHECK_HTTP_MESSAGE_FIRST();
10442
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010443 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010444 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010445 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010446 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010447
10448 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010449 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010450 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010451 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010452 smp->data.u.str.area = temp->area;
10453 smp->data.u.str.data = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010454
10455 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010456 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010457 beg = http_get_path(txn);
10458 if (!beg)
10459 beg = end;
10460
10461 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10462
10463 if (beg < ptr && *beg == '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010464 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
10465 ptr - beg);
10466 smp->data.u.str.data += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010467 }
10468
10469 smp->flags = SMP_F_VOL_1ST;
10470 return 1;
10471}
10472
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010473/* This produces a 32-bit hash of the concatenation of the first occurrence of
10474 * the Host header followed by the path component if it begins with a slash ('/').
10475 * This means that '*' will not be added, resulting in exactly the first Host
10476 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010477 * is hashed using the path hash followed by a full avalanche hash and provides a
10478 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010479 * high-traffic sites without having to store whole paths.
10480 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010481int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010482smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010483{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010484 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010485 struct hdr_ctx ctx;
10486 unsigned int hash = 0;
10487 char *ptr, *beg, *end;
10488 int len;
10489
10490 CHECK_HTTP_MESSAGE_FIRST();
10491
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010492 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010493 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010494 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010495 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10496 ptr = ctx.line + ctx.val;
10497 len = ctx.vlen;
10498 while (len--)
10499 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10500 }
10501
10502 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010503 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010504 beg = http_get_path(txn);
10505 if (!beg)
10506 beg = end;
10507
10508 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10509
10510 if (beg < ptr && *beg == '/') {
10511 while (beg < ptr)
10512 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10513 }
10514 hash = full_hash(hash);
10515
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010516 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010517 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010518 smp->flags = SMP_F_VOL_1ST;
10519 return 1;
10520}
10521
Willy Tarreau4a550602012-12-09 14:53:32 +010010522/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010523 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10524 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10525 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010526 * that in environments where IPv6 is insignificant, truncating the output to
10527 * 8 bytes would still work.
10528 */
10529static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010530smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010531{
Willy Tarreau83061a82018-07-13 11:56:34 +020010532 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010533 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010534
10535 if (!cli_conn)
10536 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010537
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010538 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010539 return 0;
10540
Willy Tarreau47ca5452012-12-23 20:22:19 +010010541 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010542 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
10543 temp->data += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010544
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010545 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010546 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010547 memcpy(temp->area + temp->data,
10548 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
10549 4);
10550 temp->data += 4;
Willy Tarreau4a550602012-12-09 14:53:32 +010010551 break;
10552 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010553 memcpy(temp->area + temp->data,
10554 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
10555 16);
10556 temp->data += 16;
Willy Tarreau4a550602012-12-09 14:53:32 +010010557 break;
10558 default:
10559 return 0;
10560 }
10561
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010562 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010563 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010564 return 1;
10565}
10566
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010567/* Extracts the query string, which comes after the question mark '?'. If no
10568 * question mark is found, nothing is returned. Otherwise it returns a sample
10569 * of type string carrying the whole query string.
10570 */
10571static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010572smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010573{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010574 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010575 char *ptr, *end;
10576
10577 CHECK_HTTP_MESSAGE_FIRST();
10578
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010579 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010580 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010581 end = ptr + txn->req.sl.rq.u_l;
10582
10583 /* look up the '?' */
10584 do {
10585 if (ptr == end)
10586 return 0;
10587 } while (*ptr++ != '?');
10588
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010589 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010590 smp->data.u.str.area = ptr;
10591 smp->data.u.str.data = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010592 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10593 return 1;
10594}
10595
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010596static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010597smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010598{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010599 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10600 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10601 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010602
Willy Tarreau24e32d82012-04-23 23:55:44 +020010603 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010604
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010605 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010606 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010607 return 1;
10608}
10609
Willy Tarreau7f18e522010-10-22 20:04:13 +020010610/* return a valid test if the current request is the first one on the connection */
10611static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010612smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010613{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010614 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010615 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010616 return 1;
10617}
10618
Willy Tarreau34db1082012-04-19 17:16:54 +020010619/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010620static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010621smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010622{
10623
Willy Tarreau24e32d82012-04-23 23:55:44 +020010624 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010625 return 0;
10626
Willy Tarreauc0239e02012-04-16 14:42:55 +020010627 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010628
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010629 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010630 return 0;
10631
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010632 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010633 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010634 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010635 return 1;
10636}
Willy Tarreau8797c062007-05-07 00:55:35 +020010637
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010638/* Accepts exactly 1 argument of type userlist */
10639static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010640smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010641{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010642 if (!args || args->type != ARGT_USR)
10643 return 0;
10644
10645 CHECK_HTTP_MESSAGE_FIRST();
10646
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010647 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010648 return 0;
10649
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010650 /* if the user does not belong to the userlist or has a wrong password,
10651 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010652 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010653 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010654 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10655 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010656 return 0;
10657
10658 /* pat_match_auth() will need the user list */
10659 smp->ctx.a[0] = args->data.usr;
10660
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010661 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010662 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010663 smp->data.u.str.area = smp->strm->txn->auth.user;
10664 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010665
10666 return 1;
10667}
10668
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010669/* Try to find the next occurrence of a cookie name in a cookie header value.
10670 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10671 * the cookie value is returned into *value and *value_l, and the function
10672 * returns a pointer to the next pointer to search from if the value was found.
10673 * Otherwise if the cookie was not found, NULL is returned and neither value
10674 * nor value_l are touched. The input <hdr> string should first point to the
10675 * header's value, and the <hdr_end> pointer must point to the first character
10676 * not part of the value. <list> must be non-zero if value may represent a list
10677 * of values (cookie headers). This makes it faster to abort parsing when no
10678 * list is expected.
10679 */
David Carlier4686f792015-09-25 14:10:50 +010010680char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010681extract_cookie_value(char *hdr, const char *hdr_end,
10682 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010683 char **value, size_t *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010684{
10685 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10686 char *next;
10687
10688 /* we search at least a cookie name followed by an equal, and more
10689 * generally something like this :
10690 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10691 */
10692 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10693 /* Iterate through all cookies on this line */
10694
Willy Tarreau2235b262016-11-05 15:50:20 +010010695 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010696 att_beg++;
10697
10698 /* find att_end : this is the first character after the last non
10699 * space before the equal. It may be equal to hdr_end.
10700 */
10701 equal = att_end = att_beg;
10702
10703 while (equal < hdr_end) {
10704 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10705 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010706 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010707 continue;
10708 att_end = equal;
10709 }
10710
10711 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10712 * is between <att_beg> and <equal>, both may be identical.
10713 */
10714
10715 /* look for end of cookie if there is an equal sign */
10716 if (equal < hdr_end && *equal == '=') {
10717 /* look for the beginning of the value */
10718 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010719 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010720 val_beg++;
10721
10722 /* find the end of the value, respecting quotes */
10723 next = find_cookie_value_end(val_beg, hdr_end);
10724
10725 /* make val_end point to the first white space or delimitor after the value */
10726 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010727 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010728 val_end--;
10729 } else {
10730 val_beg = val_end = next = equal;
10731 }
10732
10733 /* We have nothing to do with attributes beginning with '$'. However,
10734 * they will automatically be removed if a header before them is removed,
10735 * since they're supposed to be linked together.
10736 */
10737 if (*att_beg == '$')
10738 continue;
10739
10740 /* Ignore cookies with no equal sign */
10741 if (equal == next)
10742 continue;
10743
10744 /* Now we have the cookie name between att_beg and att_end, and
10745 * its value between val_beg and val_end.
10746 */
10747
10748 if (att_end - att_beg == cookie_name_l &&
10749 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10750 /* let's return this value and indicate where to go on from */
10751 *value = val_beg;
10752 *value_l = val_end - val_beg;
10753 return next + 1;
10754 }
10755
10756 /* Set-Cookie headers only have the name in the first attr=value part */
10757 if (!list)
10758 break;
10759 }
10760
10761 return NULL;
10762}
10763
William Lallemanda43ba4e2014-01-28 18:14:25 +010010764/* Fetch a captured HTTP request header. The index is the position of
10765 * the "capture" option in the configuration file
10766 */
10767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010768smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010769{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010770 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010771 int idx;
10772
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010773 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010774 return 0;
10775
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010776 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010777
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010778 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 +010010779 return 0;
10780
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010781 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010782 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010783 smp->data.u.str.area = smp->strm->req_cap[idx];
10784 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010785
10786 return 1;
10787}
10788
10789/* Fetch a captured HTTP response header. The index is the position of
10790 * the "capture" option in the configuration file
10791 */
10792static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010793smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010794{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010795 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010796 int idx;
10797
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010798 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010799 return 0;
10800
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010801 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010802
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010803 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 +010010804 return 0;
10805
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010806 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010807 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010808 smp->data.u.str.area = smp->strm->res_cap[idx];
10809 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010810
10811 return 1;
10812}
10813
William Lallemand65ad6e12014-01-31 15:08:02 +010010814/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10815static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010816smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010817{
Willy Tarreau83061a82018-07-13 11:56:34 +020010818 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010819 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010820 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010821
Willy Tarreau15e91e12015-04-04 00:52:09 +020010822 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010823 return 0;
10824
William Lallemand96a77852014-02-05 00:30:02 +010010825 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010826
William Lallemand96a77852014-02-05 00:30:02 +010010827 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10828 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010829
William Lallemand96a77852014-02-05 00:30:02 +010010830 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010831 temp->area = txn->uri;
10832 temp->data = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010833 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010834 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010835 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010836
10837 return 1;
10838
10839}
10840
10841/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10842static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010843smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010844{
Willy Tarreau83061a82018-07-13 11:56:34 +020010845 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010846 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010847 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010848
Willy Tarreau15e91e12015-04-04 00:52:09 +020010849 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010850 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010851
William Lallemand65ad6e12014-01-31 15:08:02 +010010852 ptr = txn->uri;
10853
10854 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10855 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010856
William Lallemand65ad6e12014-01-31 15:08:02 +010010857 if (!*ptr)
10858 return 0;
10859
10860 ptr++; /* skip the space */
10861
10862 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010863 ptr = temp->area = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010864 if (!ptr)
10865 return 0;
10866 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10867 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010868
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010869 smp->data.u.str = *temp;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010870 smp->data.u.str.data = ptr - temp->area;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010871 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010872 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010873
10874 return 1;
10875}
10876
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010877/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10878 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10879 */
10880static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010881smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010882{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010883 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010884
Willy Tarreau15e91e12015-04-04 00:52:09 +020010885 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010886 return 0;
10887
10888 if (txn->req.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010889 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010890 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010891 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010892
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010893 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010894 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010895 smp->flags = SMP_F_CONST;
10896 return 1;
10897
10898}
10899
10900/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10901 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10902 */
10903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010904smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010905{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010906 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010907
Willy Tarreau15e91e12015-04-04 00:52:09 +020010908 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010909 return 0;
10910
10911 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010912 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010913 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010914 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010915
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010916 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010917 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010918 smp->flags = SMP_F_CONST;
10919 return 1;
10920
10921}
10922
William Lallemand65ad6e12014-01-31 15:08:02 +010010923
Willy Tarreaue333ec92012-04-16 16:26:40 +020010924/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010925 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010926 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010927 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010928 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010929 * Accepts exactly 1 argument of type string. If the input options indicate
10930 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010931 * The returned sample is of type CSTR. Can be used to parse cookies in other
10932 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010933 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010934int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010935{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010936 struct http_txn *txn;
10937 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010938 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010939 const struct http_msg *msg;
10940 const char *hdr_name;
10941 int hdr_name_len;
10942 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010943 int occ = 0;
10944 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010945
Willy Tarreau24e32d82012-04-23 23:55:44 +020010946 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010947 return 0;
10948
Willy Tarreaua890d072013-04-02 12:01:06 +020010949 if (!ctx) {
10950 /* first call */
10951 ctx = &static_hdr_ctx;
10952 ctx->idx = 0;
10953 smp->ctx.a[2] = ctx;
10954 }
10955
Willy Tarreaue333ec92012-04-16 16:26:40 +020010956 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010957
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010958 txn = smp->strm->txn;
10959 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010960
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010961 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010962 msg = &txn->req;
10963 hdr_name = "Cookie";
10964 hdr_name_len = 6;
10965 } else {
10966 msg = &txn->rsp;
10967 hdr_name = "Set-Cookie";
10968 hdr_name_len = 10;
10969 }
10970
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010971 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010972 /* no explicit occurrence and single fetch => last cookie by default */
10973 occ = -1;
10974
10975 /* OK so basically here, either we want only one value and it's the
10976 * last one, or we want to iterate over all of them and we fetch the
10977 * next one.
10978 */
10979
Willy Tarreauf37954d2018-06-15 18:31:02 +020010980 sol = ci_head(msg->chn);
Willy Tarreau37406352012-04-23 16:16:37 +020010981 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010982 /* search for the header from the beginning, we must first initialize
10983 * the search parameters.
10984 */
Willy Tarreau37406352012-04-23 16:16:37 +020010985 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010986 ctx->idx = 0;
10987 }
10988
Willy Tarreau28376d62012-04-26 21:26:10 +020010989 smp->flags |= SMP_F_VOL_HDR;
10990
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010991 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010992 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10993 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010994 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10995 goto out;
10996
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010997 if (ctx->vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010998 continue;
10999
Willy Tarreau37406352012-04-23 16:16:37 +020011000 smp->ctx.a[0] = ctx->line + ctx->val;
11001 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011002 }
11003
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011004 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011005 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011006 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011007 args->data.str.area,
11008 args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011009 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011010 &smp->data.u.str.area,
11011 &smp->data.u.str.data);
Willy Tarreau37406352012-04-23 16:16:37 +020011012 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011013 found = 1;
11014 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011015 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011016 smp->flags |= SMP_F_NOT_LAST;
11017 return 1;
11018 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011019 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011020 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011021 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011022 /* all cookie headers and values were scanned. If we're looking for the
11023 * last occurrence, we may return it now.
11024 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011025 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011026 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011027 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011028}
11029
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011030/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011031 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011032 * multiple cookies may be parsed on the same line. The returned sample is of
11033 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011034 */
11035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011036smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011037{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011038 struct http_txn *txn;
11039 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011040 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011041 const struct http_msg *msg;
11042 const char *hdr_name;
11043 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011044 int cnt;
11045 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011046 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011047
Willy Tarreau24e32d82012-04-23 23:55:44 +020011048 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011049 return 0;
11050
Willy Tarreaue333ec92012-04-16 16:26:40 +020011051 CHECK_HTTP_MESSAGE_FIRST();
11052
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011053 txn = smp->strm->txn;
11054 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011055
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011056 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011057 msg = &txn->req;
11058 hdr_name = "Cookie";
11059 hdr_name_len = 6;
11060 } else {
11061 msg = &txn->rsp;
11062 hdr_name = "Set-Cookie";
11063 hdr_name_len = 10;
11064 }
11065
Willy Tarreauf37954d2018-06-15 18:31:02 +020011066 sol = ci_head(msg->chn);
Willy Tarreau46787ed2012-04-11 17:28:40 +020011067 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011068 ctx.idx = 0;
11069 cnt = 0;
11070
11071 while (1) {
11072 /* Note: val_beg == NULL every time we need to fetch a new header */
11073 if (!val_beg) {
11074 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11075 break;
11076
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011077 if (ctx.vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011078 continue;
11079
11080 val_beg = ctx.line + ctx.val;
11081 val_end = val_beg + ctx.vlen;
11082 }
11083
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011084 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011085 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011086 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011087 args->data.str.area, args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011088 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011089 &smp->data.u.str.area,
11090 &smp->data.u.str.data))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011091 cnt++;
11092 }
11093 }
11094
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011095 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011096 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011097 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011098 return 1;
11099}
11100
Willy Tarreau51539362012-05-08 12:46:28 +020011101/* Fetch an cookie's integer value. The integer value is returned. It
11102 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11103 */
11104static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011105smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011106{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011107 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011108
11109 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011110 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011111 smp->data.u.sint = strl2ic(smp->data.u.str.area,
11112 smp->data.u.str.data);
Willy Tarreau51539362012-05-08 12:46:28 +020011113 }
11114
11115 return ret;
11116}
11117
Willy Tarreau8797c062007-05-07 00:55:35 +020011118/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011119/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011120/************************************************************************/
11121
David Cournapeau16023ee2010-12-23 20:55:41 +090011122/*
11123 * Given a path string and its length, find the position of beginning of the
11124 * query string. Returns NULL if no query string is found in the path.
11125 *
11126 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11127 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011128 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011129 */
bedis4c75cca2012-10-05 08:38:24 +020011130static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011131{
11132 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011133
bedis4c75cca2012-10-05 08:38:24 +020011134 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011135 return p ? p + 1 : NULL;
11136}
11137
bedis4c75cca2012-10-05 08:38:24 +020011138static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011139{
bedis4c75cca2012-10-05 08:38:24 +020011140 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011141}
11142
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011143/* after increasing a pointer value, it can exceed the first buffer
11144 * size. This function transform the value of <ptr> according with
11145 * the expected position. <chunks> is an array of the one or two
11146 * avalaible chunks. The first value is the start of the first chunk,
11147 * the second value if the end+1 of the first chunks. The third value
11148 * is NULL or the start of the second chunk and the fourth value is
11149 * the end+1 of the second chunk. The function returns 1 if does a
11150 * wrap, else returns 0.
11151 */
11152static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11153{
11154 if (*ptr < chunks[1])
11155 return 0;
11156 if (!chunks[2])
11157 return 0;
11158 *ptr = chunks[2] + ( *ptr - chunks[1] );
11159 return 1;
11160}
11161
David Cournapeau16023ee2010-12-23 20:55:41 +090011162/*
11163 * Given a url parameter, find the starting position of the first occurence,
11164 * or NULL if the parameter is not found.
11165 *
11166 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11167 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011168 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011169 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011170 * or the second chunk. The caller must be check the position before using the
11171 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011172 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011173static const char *
11174find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011175 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011176 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011177{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011178 const char *pos, *last, *equal;
11179 const char **bufs = chunks;
11180 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011181
David Cournapeau16023ee2010-12-23 20:55:41 +090011182
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011183 pos = bufs[0];
11184 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011185 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011186 /* Check the equal. */
11187 equal = pos + url_param_name_l;
11188 if (fix_pointer_if_wrap(chunks, &equal)) {
11189 if (equal >= chunks[3])
11190 return NULL;
11191 } else {
11192 if (equal >= chunks[1])
11193 return NULL;
11194 }
11195 if (*equal == '=') {
11196 if (pos + url_param_name_l > last) {
11197 /* process wrap case, we detect a wrap. In this case, the
11198 * comparison is performed in two parts.
11199 */
11200
11201 /* This is the end, we dont have any other chunk. */
11202 if (bufs != chunks || !bufs[2])
11203 return NULL;
11204
11205 /* Compute the length of each part of the comparison. */
11206 l1 = last - pos;
11207 l2 = url_param_name_l - l1;
11208
11209 /* The second buffer is too short to contain the compared string. */
11210 if (bufs[2] + l2 > bufs[3])
11211 return NULL;
11212
11213 if (memcmp(pos, url_param_name, l1) == 0 &&
11214 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11215 return pos;
11216
11217 /* Perform wrapping and jump the string who fail the comparison. */
11218 bufs += 2;
11219 pos = bufs[0] + l2;
11220 last = bufs[1];
11221
11222 } else {
11223 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011224 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11225 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011226 pos += url_param_name_l + 1;
11227 if (fix_pointer_if_wrap(chunks, &pos))
11228 last = bufs[2];
11229 }
11230 }
11231
11232 while (1) {
11233 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011234 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011235 pos++;
11236 if (pos < last)
11237 break;
11238 /* process buffer wrapping. */
11239 if (bufs != chunks || !bufs[2])
11240 return NULL;
11241 bufs += 2;
11242 pos = bufs[0];
11243 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011244 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011245 pos++;
11246 }
11247 return NULL;
11248}
11249
11250/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011251 * Given a url parameter name and a query string, find the next value.
11252 * An empty url_param_name matches the first available parameter.
11253 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11254 * respectively provide a pointer to the value and its end.
11255 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011256 */
11257static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011258find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011259 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011260 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011261{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011262 const char *arg_start, *qs_end;
11263 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011264
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011265 arg_start = chunks[0];
11266 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011267 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011268 /* Looks for an argument name. */
11269 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011270 url_param_name, url_param_name_l,
11271 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011272 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011273 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011274 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011275 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011276 if (!arg_start)
11277 return 0;
11278
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011279 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011280 while (1) {
11281 /* looks for the first argument. */
11282 value_start = memchr(arg_start, '=', qs_end - arg_start);
11283 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011284 /* Check for wrapping. */
11285 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011286 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011287 chunks[2]) {
11288 arg_start = chunks[2];
11289 qs_end = chunks[3];
11290 continue;
11291 }
11292 return 0;
11293 }
11294 break;
11295 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011296 value_start++;
11297 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011298 else {
11299 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011300 value_start = arg_start + url_param_name_l + 1;
11301
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011302 /* Check for pointer wrapping. */
11303 if (fix_pointer_if_wrap(chunks, &value_start)) {
11304 /* Update the end pointer. */
11305 qs_end = chunks[3];
11306
11307 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011308 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011309 return 0;
11310 }
11311 }
11312
David Cournapeau16023ee2010-12-23 20:55:41 +090011313 value_end = value_start;
11314
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011315 while (1) {
11316 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11317 value_end++;
11318 if (value_end < qs_end)
11319 break;
11320 /* process buffer wrapping. */
11321 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011322 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011323 chunks[2]) {
11324 value_end = chunks[2];
11325 qs_end = chunks[3];
11326 continue;
11327 }
11328 break;
11329 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011330
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011331 *vstart = value_start;
11332 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011333 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011334}
11335
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011336/* This scans a URL-encoded query string. It takes an optionally wrapping
11337 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11338 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11339 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011340 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011341static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011342smp_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 +090011343{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011344 const char *vstart, *vend;
Willy Tarreau83061a82018-07-13 11:56:34 +020011345 struct buffer *temp;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011346 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011347
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011348 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011349 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011350 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011351 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011352 return 0;
11353
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011354 /* Create sample. If the value is contiguous, return the pointer as CONST,
11355 * if the value is wrapped, copy-it in a buffer.
11356 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011357 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011358 if (chunks[2] &&
11359 vstart >= chunks[0] && vstart <= chunks[1] &&
11360 vend >= chunks[2] && vend <= chunks[3]) {
11361 /* Wrapped case. */
11362 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011363 memcpy(temp->area, vstart, chunks[1] - vstart);
11364 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
11365 vend - chunks[2]);
11366 smp->data.u.str.area = temp->area;
11367 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011368 } else {
11369 /* Contiguous case. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011370 smp->data.u.str.area = (char *)vstart;
11371 smp->data.u.str.data = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011372 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11373 }
11374
11375 /* Update context, check wrapping. */
11376 chunks[0] = vend;
11377 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11378 chunks[1] = chunks[3];
11379 chunks[2] = NULL;
11380 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011381
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011382 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011383 smp->flags |= SMP_F_NOT_LAST;
11384
David Cournapeau16023ee2010-12-23 20:55:41 +090011385 return 1;
11386}
11387
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011388/* This function iterates over each parameter of the query string. It uses
11389 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011390 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11391 * An optional parameter name is passed in args[0], otherwise any parameter is
11392 * considered. It supports an optional delimiter argument for the beginning of
11393 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011394 */
11395static int
11396smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11397{
11398 struct http_msg *msg;
11399 char delim = '?';
11400 const char *name;
11401 int name_len;
11402
Dragan Dosen26f77e52015-05-25 10:02:11 +020011403 if (!args ||
11404 (args[0].type && args[0].type != ARGT_STR) ||
11405 (args[1].type && args[1].type != ARGT_STR))
11406 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011407
Dragan Dosen26f77e52015-05-25 10:02:11 +020011408 name = "";
11409 name_len = 0;
11410 if (args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011411 name = args->data.str.area;
11412 name_len = args->data.str.data;
Dragan Dosen26f77e52015-05-25 10:02:11 +020011413 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011414
Dragan Dosen26f77e52015-05-25 10:02:11 +020011415 if (args[1].type)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011416 delim = *args[1].data.str.area;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011417
Dragan Dosen26f77e52015-05-25 10:02:11 +020011418 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011419 CHECK_HTTP_MESSAGE_FIRST();
11420
11421 msg = &smp->strm->txn->req;
11422
Willy Tarreauf37954d2018-06-15 18:31:02 +020011423 smp->ctx.a[0] = find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011424 msg->sl.rq.u_l, delim);
11425 if (!smp->ctx.a[0])
11426 return 0;
11427
Willy Tarreauf37954d2018-06-15 18:31:02 +020011428 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011429
11430 /* Assume that the context is filled with NULL pointer
11431 * before the first call.
11432 * smp->ctx.a[2] = NULL;
11433 * smp->ctx.a[3] = NULL;
11434 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011435 }
11436
11437 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11438}
11439
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011440/* This function iterates over each parameter of the body. This requires
11441 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011442 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11443 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11444 * optional second part if the body wraps at the end of the buffer. An optional
11445 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011446 */
11447static int
11448smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11449{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011450 struct http_msg *msg;
11451 unsigned long len;
11452 unsigned long block1;
11453 char *body;
11454 const char *name;
11455 int name_len;
11456
11457 if (!args || (args[0].type && args[0].type != ARGT_STR))
11458 return 0;
11459
11460 name = "";
11461 name_len = 0;
11462 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011463 name = args[0].data.str.area;
11464 name_len = args[0].data.str.data;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011465 }
11466
11467 if (!smp->ctx.a[0]) { // first call, find the query string
11468 CHECK_HTTP_MESSAGE_FIRST();
11469
11470 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011471 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011472 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011473 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011474
11475 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020011476 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011477
11478 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011479 if (block1 > b_wrap(&msg->chn->buf) - body)
11480 block1 = b_wrap(&msg->chn->buf) - body;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011481
11482 if (block1 == len) {
11483 /* buffer is not wrapped (or empty) */
11484 smp->ctx.a[0] = body;
11485 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011486
11487 /* Assume that the context is filled with NULL pointer
11488 * before the first call.
11489 * smp->ctx.a[2] = NULL;
11490 * smp->ctx.a[3] = NULL;
11491 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011492 }
11493 else {
11494 /* buffer is wrapped, we need to defragment it */
11495 smp->ctx.a[0] = body;
11496 smp->ctx.a[1] = body + block1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011497 smp->ctx.a[2] = b_orig(&msg->chn->buf);
11498 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011499 }
11500 }
11501 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11502}
11503
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011504/* Return the signed integer value for the specified url parameter (see url_param
11505 * above).
11506 */
11507static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011508smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011509{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011510 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011511
11512 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011513 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011514 smp->data.u.sint = strl2ic(smp->data.u.str.area,
11515 smp->data.u.str.data);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011516 }
11517
11518 return ret;
11519}
11520
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011521/* This produces a 32-bit hash of the concatenation of the first occurrence of
11522 * the Host header followed by the path component if it begins with a slash ('/').
11523 * This means that '*' will not be added, resulting in exactly the first Host
11524 * entry. If no Host header is found, then the path is used. The resulting value
11525 * is hashed using the url hash followed by a full avalanche hash and provides a
11526 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11527 * high-traffic sites without having to store whole paths.
11528 * this differs from the base32 functions in that it includes the url parameters
11529 * as well as the path
11530 */
11531static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011532smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011533{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011534 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011535 struct hdr_ctx ctx;
11536 unsigned int hash = 0;
11537 char *ptr, *beg, *end;
11538 int len;
11539
11540 CHECK_HTTP_MESSAGE_FIRST();
11541
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011542 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011543 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011544 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011545 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11546 ptr = ctx.line + ctx.val;
11547 len = ctx.vlen;
11548 while (len--)
11549 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11550 }
11551
11552 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020011553 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011554 beg = http_get_path(txn);
11555 if (!beg)
11556 beg = end;
11557
11558 for (ptr = beg; ptr < end ; ptr++);
11559
11560 if (beg < ptr && *beg == '/') {
11561 while (beg < ptr)
11562 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11563 }
11564 hash = full_hash(hash);
11565
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011566 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011567 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011568 smp->flags = SMP_F_VOL_1ST;
11569 return 1;
11570}
11571
11572/* This concatenates the source address with the 32-bit hash of the Host and
11573 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11574 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11575 * on the source address length. The URL hash is stored before the address so
11576 * that in environments where IPv6 is insignificant, truncating the output to
11577 * 8 bytes would still work.
11578 */
11579static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011580smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011581{
Willy Tarreau83061a82018-07-13 11:56:34 +020011582 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011583 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011584
Dragan Dosendb5af612016-06-16 11:23:01 +020011585 if (!cli_conn)
11586 return 0;
11587
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011588 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011589 return 0;
11590
11591 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011592 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
11593 temp->data += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011594
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011595 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011596 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011597 memcpy(temp->area + temp->data,
11598 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
11599 4);
11600 temp->data += 4;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011601 break;
11602 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011603 memcpy(temp->area + temp->data,
11604 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
11605 16);
11606 temp->data += 16;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011607 break;
11608 default:
11609 return 0;
11610 }
11611
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011612 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011613 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011614 return 1;
11615}
11616
Willy Tarreau185b5c42012-04-26 15:11:51 +020011617/* This function is used to validate the arguments passed to any "hdr" fetch
11618 * keyword. These keywords support an optional positive or negative occurrence
11619 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11620 * is assumed that the types are already the correct ones. Returns 0 on error,
11621 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11622 * error message in case of error, that the caller is responsible for freeing.
11623 * The initial location must either be freeable or NULL.
11624 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011625int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011626{
11627 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011628 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011629 return 0;
11630 }
11631 return 1;
11632}
11633
Willy Tarreau276fae92013-07-25 14:36:01 +020011634/* takes an UINT value on input supposed to represent the time since EPOCH,
11635 * adds an optional offset found in args[0] and emits a string representing
11636 * the date in RFC-1123/5322 format.
11637 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011638static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011639{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011640 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011641 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Willy Tarreau83061a82018-07-13 11:56:34 +020011642 struct buffer *temp;
Willy Tarreau276fae92013-07-25 14:36:01 +020011643 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011644 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011645 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011646
11647 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011648 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011649 curr_date += args[0].data.sint;
11650
11651 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011652 if (!tm)
11653 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011654
11655 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011656 temp->data = snprintf(temp->area, temp->size - temp->data,
11657 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11658 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
11659 1900+tm->tm_year,
11660 tm->tm_hour, tm->tm_min, tm->tm_sec);
Willy Tarreau276fae92013-07-25 14:36:01 +020011661
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011662 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011663 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011664 return 1;
11665}
11666
Thierry FOURNIERad903512014-04-11 17:51:01 +020011667/* Match language range with language tag. RFC2616 14.4:
11668 *
11669 * A language-range matches a language-tag if it exactly equals
11670 * the tag, or if it exactly equals a prefix of the tag such
11671 * that the first tag character following the prefix is "-".
11672 *
11673 * Return 1 if the strings match, else return 0.
11674 */
11675static inline int language_range_match(const char *range, int range_len,
11676 const char *tag, int tag_len)
11677{
11678 const char *end = range + range_len;
11679 const char *tend = tag + tag_len;
11680 while (range < end) {
11681 if (*range == '-' && tag == tend)
11682 return 1;
11683 if (*range != *tag || tag == tend)
11684 return 0;
11685 range++;
11686 tag++;
11687 }
11688 /* Return true only if the last char of the tag is matched. */
11689 return tag == tend;
11690}
11691
11692/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011693static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011694{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011695 const char *al = smp->data.u.str.area;
11696 const char *end = al + smp->data.u.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011697 const char *token;
11698 int toklen;
11699 int qvalue;
11700 const char *str;
11701 const char *w;
11702 int best_q = 0;
11703
11704 /* Set the constant to the sample, because the output of the
11705 * function will be peek in the constant configuration string.
11706 */
11707 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011708 smp->data.u.str.size = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011709 smp->data.u.str.area = "";
11710 smp->data.u.str.data = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011711
11712 /* Parse the accept language */
11713 while (1) {
11714
11715 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011716 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011717 al++;
11718 if (al >= end)
11719 break;
11720
11721 /* Start of the fisrt word. */
11722 token = al;
11723
11724 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011725 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011726 al++;
11727 if (al == token)
11728 goto expect_comma;
11729
11730 /* Length of the token. */
11731 toklen = al - token;
11732 qvalue = 1000;
11733
11734 /* Check if the token exists in the list. If the token not exists,
11735 * jump to the next token.
11736 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011737 str = args[0].data.str.area;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011738 w = str;
11739 while (1) {
11740 if (*str == ';' || *str == '\0') {
11741 if (language_range_match(token, toklen, w, str-w))
11742 goto look_for_q;
11743 if (*str == '\0')
11744 goto expect_comma;
11745 w = str + 1;
11746 }
11747 str++;
11748 }
11749 goto expect_comma;
11750
11751look_for_q:
11752
11753 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011754 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011755 al++;
11756 if (al >= end)
11757 goto process_value;
11758
11759 /* If ',' is found, process the result */
11760 if (*al == ',')
11761 goto process_value;
11762
11763 /* If the character is different from ';', look
11764 * for the end of the header part in best effort.
11765 */
11766 if (*al != ';')
11767 goto expect_comma;
11768
11769 /* Assumes that the char is ';', now expect "q=". */
11770 al++;
11771
11772 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011773 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011774 al++;
11775 if (al >= end)
11776 goto process_value;
11777
11778 /* Expect 'q'. If no 'q', continue in best effort */
11779 if (*al != 'q')
11780 goto process_value;
11781 al++;
11782
11783 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011784 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011785 al++;
11786 if (al >= end)
11787 goto process_value;
11788
11789 /* Expect '='. If no '=', continue in best effort */
11790 if (*al != '=')
11791 goto process_value;
11792 al++;
11793
11794 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011795 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011796 al++;
11797 if (al >= end)
11798 goto process_value;
11799
11800 /* Parse the q value. */
11801 qvalue = parse_qvalue(al, &al);
11802
11803process_value:
11804
11805 /* If the new q value is the best q value, then store the associated
11806 * language in the response. If qvalue is the biggest value (1000),
11807 * break the process.
11808 */
11809 if (qvalue > best_q) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011810 smp->data.u.str.area = (char *)w;
11811 smp->data.u.str.data = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011812 if (qvalue >= 1000)
11813 break;
11814 best_q = qvalue;
11815 }
11816
11817expect_comma:
11818
11819 /* Expect comma or end. If the end is detected, quit the loop. */
11820 while (al < end && *al != ',')
11821 al++;
11822 if (al >= end)
11823 break;
11824
11825 /* Comma is found, jump it and restart the analyzer. */
11826 al++;
11827 }
11828
11829 /* Set default value if required. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011830 if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
11831 smp->data.u.str.area = args[1].data.str.area;
11832 smp->data.u.str.data = args[1].data.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011833 }
11834
11835 /* Return true only if a matching language was found. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011836 return smp->data.u.str.data != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011837}
11838
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011839/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011840static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011841{
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011842 int len;
11843
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011844 /* If the constant flag is set or if not size is avalaible at
11845 * the end of the buffer, copy the string in other buffer
11846 * before decoding.
11847 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011848 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
Willy Tarreau83061a82018-07-13 11:56:34 +020011849 struct buffer *str = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011850 memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
11851 smp->data.u.str.area = str->area;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011852 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011853 smp->flags &= ~SMP_F_CONST;
11854 }
11855
11856 /* Add final \0 required by url_decode(), and convert the input string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011857 smp->data.u.str.area[smp->data.u.str.data] = '\0';
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011858 len = url_decode(smp->data.u.str.area);
11859 if (len < 0)
11860 return 0;
11861 smp->data.u.str.data = len;
11862 return 1;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011863}
11864
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011865static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11866{
11867 struct proxy *fe = strm_fe(smp->strm);
11868 int idx, i;
11869 struct cap_hdr *hdr;
11870 int len;
11871
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011872 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011873 return 0;
11874
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011875 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011876
11877 /* Check the availibity of the capture id. */
11878 if (idx > fe->nb_req_cap - 1)
11879 return 0;
11880
11881 /* Look for the original configuration. */
11882 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11883 hdr != NULL && i != idx ;
11884 i--, hdr = hdr->next);
11885 if (!hdr)
11886 return 0;
11887
11888 /* check for the memory allocation */
11889 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011890 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011891 if (smp->strm->req_cap[hdr->index] == NULL)
11892 return 0;
11893
11894 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011895 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011896 if (len > hdr->len)
11897 len = hdr->len;
11898
11899 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011900 memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011901 smp->strm->req_cap[idx][len] = '\0';
11902
11903 return 1;
11904}
11905
11906static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11907{
11908 struct proxy *fe = strm_fe(smp->strm);
11909 int idx, i;
11910 struct cap_hdr *hdr;
11911 int len;
11912
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011913 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011914 return 0;
11915
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011916 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011917
11918 /* Check the availibity of the capture id. */
11919 if (idx > fe->nb_rsp_cap - 1)
11920 return 0;
11921
11922 /* Look for the original configuration. */
11923 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11924 hdr != NULL && i != idx ;
11925 i--, hdr = hdr->next);
11926 if (!hdr)
11927 return 0;
11928
11929 /* check for the memory allocation */
11930 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011931 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011932 if (smp->strm->res_cap[hdr->index] == NULL)
11933 return 0;
11934
11935 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011936 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011937 if (len > hdr->len)
11938 len = hdr->len;
11939
11940 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011941 memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011942 smp->strm->res_cap[idx][len] = '\0';
11943
11944 return 1;
11945}
11946
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011947/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011948 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011949 * the relevant part of the request line accordingly. Then it updates various
11950 * pointers to the next elements which were moved, and the total buffer length.
11951 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011952 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11953 * error, though this can be revisited when this code is finally exploited.
11954 *
11955 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11956 * query string and 3 to replace uri.
11957 *
11958 * In query string case, the mark question '?' must be set at the start of the
11959 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011960 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011961int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011962 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011963{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011964 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011965 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011966 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011967 int delta;
11968
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011969 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011970 case 0: // method
Willy Tarreauf37954d2018-06-15 18:31:02 +020011971 cur_ptr = ci_head(&s->req);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011972 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11973
11974 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011975 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011976 txn->req.sl.rq.m_l += delta;
11977 txn->req.sl.rq.u += delta;
11978 txn->req.sl.rq.v += delta;
11979 break;
11980
11981 case 1: // path
11982 cur_ptr = http_get_path(txn);
11983 if (!cur_ptr)
Willy Tarreauf37954d2018-06-15 18:31:02 +020011984 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011985
11986 cur_end = cur_ptr;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011987 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011988 cur_end++;
11989
11990 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011991 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011992 txn->req.sl.rq.u_l += delta;
11993 txn->req.sl.rq.v += delta;
11994 break;
11995
11996 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011997 offset = 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011998 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011999 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12000 while (cur_ptr < cur_end && *cur_ptr != '?')
12001 cur_ptr++;
12002
12003 /* skip the question mark or indicate that we must insert it
12004 * (but only if the format string is not empty then).
12005 */
12006 if (cur_ptr < cur_end)
12007 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012008 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012009 offset = 0;
12010
12011 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012012 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012013 txn->req.sl.rq.u_l += delta;
12014 txn->req.sl.rq.v += delta;
12015 break;
12016
12017 case 3: // uri
Willy Tarreauf37954d2018-06-15 18:31:02 +020012018 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012019 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12020
12021 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012022 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012023 txn->req.sl.rq.u_l += delta;
12024 txn->req.sl.rq.v += delta;
12025 break;
12026
12027 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012028 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012029 }
12030
12031 /* commit changes and adjust end of message */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020012032 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012033 txn->req.sl.rq.l += delta;
12034 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012035 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012036 return 0;
12037}
12038
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012039/* This function replace the HTTP status code and the associated message. The
12040 * variable <status> contains the new status code. This function never fails.
12041 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012042void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012043{
12044 struct http_txn *txn = s->txn;
12045 char *cur_ptr, *cur_end;
12046 int delta;
12047 char *res;
12048 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012049 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012050 int msg_len;
12051
12052 chunk_reset(&trash);
12053
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012054 res = ultoa_o(status, trash.area, trash.size);
12055 c_l = res - trash.area;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012056
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012057 trash.area[c_l] = ' ';
12058 trash.data = c_l + 1;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012059
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012060 /* Do we have a custom reason format string? */
12061 if (msg == NULL)
12062 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012063 msg_len = strlen(msg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012064 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
12065 trash.data += msg_len;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012066
Willy Tarreauf37954d2018-06-15 18:31:02 +020012067 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
12068 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012069
12070 /* commit changes and adjust message */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012071 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
12072 trash.data);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012073
12074 /* adjust res line offsets and lengths */
12075 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12076 txn->rsp.sl.st.c_l = c_l;
12077 txn->rsp.sl.st.r_l = msg_len;
12078
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012079 delta = trash.data - (cur_end - cur_ptr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012080 txn->rsp.sl.st.l += delta;
12081 txn->hdr_idx.v[0].len += delta;
12082 http_msg_move_end(&txn->rsp, delta);
12083}
12084
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012085/* This function executes one of the set-{method,path,query,uri} actions. It
12086 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012087 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012088 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012089 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12090 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012091 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012092enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012093 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012094{
Willy Tarreau83061a82018-07-13 11:56:34 +020012095 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012096 enum act_return ret = ACT_RET_ERR;
12097
12098 replace = alloc_trash_chunk();
12099 if (!replace)
12100 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012101
12102 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012103 if (rule->arg.http.action == 2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012104 replace->area[replace->data++] = '?';
12105 replace->data += build_logline(s, replace->area + replace->data,
12106 replace->size - replace->data,
12107 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012108
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012109 http_replace_req_line(rule->arg.http.action, replace->area,
12110 replace->data, px, s);
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012111
12112 ret = ACT_RET_CONT;
12113
12114leave:
12115 free_trash_chunk(replace);
12116 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012117}
12118
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012119/* This function is just a compliant action wrapper for "set-status". */
12120enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012121 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012122{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012123 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012124 return ACT_RET_CONT;
12125}
12126
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012127/* parse an http-request action among :
12128 * set-method
12129 * set-path
12130 * set-query
12131 * set-uri
12132 *
12133 * All of them accept a single argument of type string representing a log-format.
12134 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12135 * 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 +020012136 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012137 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012138enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12139 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012140{
12141 int cur_arg = *orig_arg;
12142
Thierry FOURNIER42148732015-09-02 17:17:33 +020012143 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012144
12145 switch (args[0][4]) {
12146 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012147 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012148 rule->action_ptr = http_action_set_req_line;
12149 break;
12150 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012151 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012152 rule->action_ptr = http_action_set_req_line;
12153 break;
12154 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012155 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012156 rule->action_ptr = http_action_set_req_line;
12157 break;
12158 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012159 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012160 rule->action_ptr = http_action_set_req_line;
12161 break;
12162 default:
12163 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012164 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012165 }
12166
12167 if (!*args[cur_arg] ||
12168 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12169 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012170 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012171 }
12172
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012173 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012174 px->conf.args.ctx = ARGC_HRQ;
12175 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12176 (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 +010012177 return ACT_RET_PRS_ERR;
12178 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012179
12180 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012181 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012182}
12183
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012184/* parse set-status action:
12185 * This action accepts a single argument of type int representing
12186 * an http status code. It returns ACT_RET_PRS_OK on success,
12187 * ACT_RET_PRS_ERR on error.
12188 */
12189enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12190 struct act_rule *rule, char **err)
12191{
12192 char *error;
12193
Thierry FOURNIER42148732015-09-02 17:17:33 +020012194 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012195 rule->action_ptr = action_http_set_status;
12196
12197 /* Check if an argument is available */
12198 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012199 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012200 return ACT_RET_PRS_ERR;
12201 }
12202
12203 /* convert status code as integer */
12204 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12205 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12206 memprintf(err, "expects an integer status code between 100 and 999");
12207 return ACT_RET_PRS_ERR;
12208 }
12209
12210 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012211
12212 /* set custom reason string */
12213 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12214 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12215 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12216 (*orig_arg)++;
12217 rule->arg.status.reason = strdup(args[*orig_arg]);
12218 (*orig_arg)++;
12219 }
12220
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012221 return ACT_RET_PRS_OK;
12222}
12223
Willy Tarreau53275e82017-11-24 07:52:01 +010012224/* This function executes the "reject" HTTP action. It clears the request and
12225 * response buffer without sending any response. It can be useful as an HTTP
12226 * alternative to the silent-drop action to defend against DoS attacks, and may
12227 * also be used with HTTP/2 to close a connection instead of just a stream.
12228 * The txn status is unchanged, indicating no response was sent. The termination
12229 * flags will indicate "PR". It always returns ACT_RET_STOP.
12230 */
12231enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12232 struct session *sess, struct stream *s, int flags)
12233{
12234 channel_abort(&s->req);
12235 channel_abort(&s->res);
12236 s->req.analysers = 0;
12237 s->res.analysers = 0;
12238
12239 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12240 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12241 if (sess->listener && sess->listener->counters)
12242 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12243
12244 if (!(s->flags & SF_ERR_MASK))
12245 s->flags |= SF_ERR_PRXCOND;
12246 if (!(s->flags & SF_FINST_MASK))
12247 s->flags |= SF_FINST_R;
12248
12249 return ACT_RET_CONT;
12250}
12251
12252/* parse the "reject" action:
12253 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12254 * ACT_RET_PRS_ERR on error.
12255 */
12256enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12257 struct act_rule *rule, char **err)
12258{
12259 rule->action = ACT_CUSTOM;
12260 rule->action_ptr = http_action_reject;
12261 return ACT_RET_PRS_OK;
12262}
12263
Willy Tarreaua9083d02015-05-08 15:27:59 +020012264/* This function executes the "capture" action. It executes a fetch expression,
12265 * turns the result into a string and puts it in a capture slot. It always
12266 * returns 1. If an error occurs the action is cancelled, but the rule
12267 * processing continues.
12268 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012269enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012270 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012271{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012272 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012273 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012274 char **cap = s->req_cap;
12275 int len;
12276
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012277 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 +020012278 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012279 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012280
12281 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012282 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012283
12284 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012285 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012286
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012287 len = key->data.u.str.data;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012288 if (len > h->len)
12289 len = h->len;
12290
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012291 memcpy(cap[h->index], key->data.u.str.area, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012292 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012293 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012294}
12295
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012296/* This function executes the "capture" action and store the result in a
12297 * capture slot if exists. It executes a fetch expression, turns the result
12298 * into a string and puts it in a capture slot. It always returns 1. If an
12299 * error occurs the action is cancelled, but the rule processing continues.
12300 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012301enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012302 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012304 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012305 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012306 char **cap = s->req_cap;
12307 struct proxy *fe = strm_fe(s);
12308 int len;
12309 int i;
12310
12311 /* Look for the original configuration. */
12312 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012313 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012314 i--, h = h->next);
12315 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012316 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012317
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012318 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 +020012319 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012320 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012321
12322 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012323 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012324
12325 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012326 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012327
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012328 len = key->data.u.str.data;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012329 if (len > h->len)
12330 len = h->len;
12331
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012332 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012333 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012334 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012335}
12336
Christopher Faulet29730ba2017-09-18 15:26:32 +020012337/* Check an "http-request capture" action.
12338 *
12339 * The function returns 1 in success case, otherwise, it returns 0 and err is
12340 * filled.
12341 */
12342int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12343{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012344 if (rule->action_ptr != http_action_req_capture_by_id)
12345 return 1;
12346
Christopher Faulet29730ba2017-09-18 15:26:32 +020012347 if (rule->arg.capid.idx >= px->nb_req_cap) {
12348 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12349 rule->arg.capid.idx);
12350 return 0;
12351 }
12352
12353 return 1;
12354}
12355
Willy Tarreaua9083d02015-05-08 15:27:59 +020012356/* parse an "http-request capture" action. It takes a single argument which is
12357 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012358 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012359 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012360 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012361enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12362 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012363{
12364 struct sample_expr *expr;
12365 struct cap_hdr *hdr;
12366 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012367 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012368
12369 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12370 if (strcmp(args[cur_arg], "if") == 0 ||
12371 strcmp(args[cur_arg], "unless") == 0)
12372 break;
12373
12374 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012375 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012376 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012377 }
12378
Willy Tarreaua9083d02015-05-08 15:27:59 +020012379 cur_arg = *orig_arg;
12380 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12381 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012382 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012383
12384 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12385 memprintf(err,
12386 "fetch method '%s' extracts information from '%s', none of which is available here",
12387 args[cur_arg-1], sample_src_names(expr->fetch->use));
12388 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012389 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012390 }
12391
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012392 if (!args[cur_arg] || !*args[cur_arg]) {
12393 memprintf(err, "expects 'len or 'id'");
12394 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012395 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012396 }
12397
Willy Tarreaua9083d02015-05-08 15:27:59 +020012398 if (strcmp(args[cur_arg], "len") == 0) {
12399 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012400
12401 if (!(px->cap & PR_CAP_FE)) {
12402 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012403 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012404 }
12405
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012406 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012407
Willy Tarreaua9083d02015-05-08 15:27:59 +020012408 if (!args[cur_arg]) {
12409 memprintf(err, "missing length value");
12410 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012411 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012412 }
12413 /* we copy the table name for now, it will be resolved later */
12414 len = atoi(args[cur_arg]);
12415 if (len <= 0) {
12416 memprintf(err, "length must be > 0");
12417 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012418 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012419 }
12420 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012421
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012422 if (!len) {
12423 memprintf(err, "a positive 'len' argument is mandatory");
12424 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012425 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012426 }
12427
Vincent Bernat02779b62016-04-03 13:48:43 +020012428 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012429 hdr->next = px->req_cap;
12430 hdr->name = NULL; /* not a header capture */
12431 hdr->namelen = 0;
12432 hdr->len = len;
12433 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12434 hdr->index = px->nb_req_cap++;
12435
12436 px->req_cap = hdr;
12437 px->to_log |= LW_REQHDR;
12438
Thierry FOURNIER42148732015-09-02 17:17:33 +020012439 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012440 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012441 rule->arg.cap.expr = expr;
12442 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012443 }
12444
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012445 else if (strcmp(args[cur_arg], "id") == 0) {
12446 int id;
12447 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012448
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012449 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012450
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012451 if (!args[cur_arg]) {
12452 memprintf(err, "missing id value");
12453 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012454 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012455 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012456
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012457 id = strtol(args[cur_arg], &error, 10);
12458 if (*error != '\0') {
12459 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12460 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012461 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012462 }
12463 cur_arg++;
12464
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012465 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012466
Thierry FOURNIER42148732015-09-02 17:17:33 +020012467 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012468 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012469 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012470 rule->arg.capid.expr = expr;
12471 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012472 }
12473
12474 else {
12475 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12476 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012477 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012478 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012479
12480 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012481 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012482}
12483
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012484/* This function executes the "capture" action and store the result in a
12485 * capture slot if exists. It executes a fetch expression, turns the result
12486 * into a string and puts it in a capture slot. It always returns 1. If an
12487 * error occurs the action is cancelled, but the rule processing continues.
12488 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012489enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012490 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012491{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012492 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012493 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012494 char **cap = s->res_cap;
12495 struct proxy *fe = strm_fe(s);
12496 int len;
12497 int i;
12498
12499 /* Look for the original configuration. */
12500 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012501 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012502 i--, h = h->next);
12503 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012504 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012505
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012506 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 +020012507 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012508 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012509
12510 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012511 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012512
12513 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012514 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012515
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012516 len = key->data.u.str.data;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012517 if (len > h->len)
12518 len = h->len;
12519
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012520 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012521 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012522 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012523}
12524
Christopher Faulet29730ba2017-09-18 15:26:32 +020012525/* Check an "http-response capture" action.
12526 *
12527 * The function returns 1 in success case, otherwise, it returns 0 and err is
12528 * filled.
12529 */
12530int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12531{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012532 if (rule->action_ptr != http_action_res_capture_by_id)
12533 return 1;
12534
Christopher Faulet29730ba2017-09-18 15:26:32 +020012535 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12536 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12537 rule->arg.capid.idx);
12538 return 0;
12539 }
12540
12541 return 1;
12542}
12543
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012544/* parse an "http-response capture" action. It takes a single argument which is
12545 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12546 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012547 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012548 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012549enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12550 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012551{
12552 struct sample_expr *expr;
12553 int cur_arg;
12554 int id;
12555 char *error;
12556
12557 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12558 if (strcmp(args[cur_arg], "if") == 0 ||
12559 strcmp(args[cur_arg], "unless") == 0)
12560 break;
12561
12562 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012563 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012564 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012565 }
12566
12567 cur_arg = *orig_arg;
12568 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12569 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012570 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012571
12572 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12573 memprintf(err,
12574 "fetch method '%s' extracts information from '%s', none of which is available here",
12575 args[cur_arg-1], sample_src_names(expr->fetch->use));
12576 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012577 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012578 }
12579
12580 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012581 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012582 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012583 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012584 }
12585
12586 if (strcmp(args[cur_arg], "id") != 0) {
12587 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12588 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012589 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012590 }
12591
12592 cur_arg++;
12593
12594 if (!args[cur_arg]) {
12595 memprintf(err, "missing id value");
12596 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012597 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012598 }
12599
12600 id = strtol(args[cur_arg], &error, 10);
12601 if (*error != '\0') {
12602 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12603 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012604 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012605 }
12606 cur_arg++;
12607
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012608 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012609
Thierry FOURNIER42148732015-09-02 17:17:33 +020012610 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012611 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012612 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012613 rule->arg.capid.expr = expr;
12614 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012615
12616 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012617 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012618}
12619
William Lallemand73025dd2014-04-24 14:38:37 +020012620/*
12621 * Return the struct http_req_action_kw associated to a keyword.
12622 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012623struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012624{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012625 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012626}
12627
12628/*
12629 * Return the struct http_res_action_kw associated to a keyword.
12630 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012631struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012632{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012633 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012634}
12635
Willy Tarreau12207b32016-11-22 19:48:51 +010012636
12637/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12638 * now.
12639 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012640static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau12207b32016-11-22 19:48:51 +010012641{
12642 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12643 return 1;
12644
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012645 if (*args[2]) {
12646 struct proxy *px;
12647
12648 px = proxy_find_by_name(args[2], 0, 0);
12649 if (px)
12650 appctx->ctx.errors.iid = px->uuid;
12651 else
12652 appctx->ctx.errors.iid = atoi(args[2]);
12653
12654 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012655 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012656 appctx->ctx.cli.msg = "No such proxy.\n";
12657 appctx->st0 = CLI_ST_PRINT;
12658 return 1;
12659 }
12660 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012661 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012662 appctx->ctx.errors.iid = -1; // dump all proxies
12663
Willy Tarreau35069f82016-11-25 09:16:37 +010012664 appctx->ctx.errors.flag = 0;
12665 if (strcmp(args[3], "request") == 0)
12666 appctx->ctx.errors.flag |= 4; // ignore response
12667 else if (strcmp(args[3], "response") == 0)
12668 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012669 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012670 return 0;
12671}
12672
12673/* This function dumps all captured errors onto the stream interface's
12674 * read buffer. It returns 0 if the output buffer is full and it needs
12675 * to be called again, otherwise non-zero.
12676 */
12677static int cli_io_handler_show_errors(struct appctx *appctx)
12678{
12679 struct stream_interface *si = appctx->owner;
12680 extern const char *monthname[12];
12681
12682 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12683 return 1;
12684
12685 chunk_reset(&trash);
12686
12687 if (!appctx->ctx.errors.px) {
12688 /* the function had not been called yet, let's prepare the
12689 * buffer for a response.
12690 */
12691 struct tm tm;
12692
12693 get_localtime(date.tv_sec, &tm);
12694 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12695 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12696 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12697 error_snapshot_id);
12698
Willy Tarreau06d80a92017-10-19 14:32:15 +020012699 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012700 /* Socket buffer full. Let's try again later from the same point */
12701 si_applet_cant_put(si);
12702 return 0;
12703 }
12704
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012705 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012706 appctx->ctx.errors.bol = 0;
12707 appctx->ctx.errors.ptr = -1;
12708 }
12709
12710 /* we have two inner loops here, one for the proxy, the other one for
12711 * the buffer.
12712 */
12713 while (appctx->ctx.errors.px) {
12714 struct error_snapshot *es;
12715
Willy Tarreau35069f82016-11-25 09:16:37 +010012716 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012717 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012718 if (appctx->ctx.errors.flag & 2) // skip req
12719 goto next;
12720 }
12721 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012722 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012723 if (appctx->ctx.errors.flag & 4) // skip resp
12724 goto next;
12725 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012726
12727 if (!es->when.tv_sec)
12728 goto next;
12729
12730 if (appctx->ctx.errors.iid >= 0 &&
12731 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12732 es->oe->uuid != appctx->ctx.errors.iid)
12733 goto next;
12734
12735 if (appctx->ctx.errors.ptr < 0) {
12736 /* just print headers now */
12737
12738 char pn[INET6_ADDRSTRLEN];
12739 struct tm tm;
12740 int port;
12741
12742 get_localtime(es->when.tv_sec, &tm);
12743 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12744 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12745 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12746
12747 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12748 case AF_INET:
12749 case AF_INET6:
12750 port = get_host_port(&es->src);
12751 break;
12752 default:
12753 port = 0;
12754 }
12755
Willy Tarreau35069f82016-11-25 09:16:37 +010012756 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012757 case 0:
12758 chunk_appendf(&trash,
12759 " frontend %s (#%d): invalid request\n"
12760 " backend %s (#%d)",
12761 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12762 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12763 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12764 break;
12765 case 1:
12766 chunk_appendf(&trash,
12767 " backend %s (#%d): invalid response\n"
12768 " frontend %s (#%d)",
12769 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12770 es->oe->id, es->oe->uuid);
12771 break;
12772 }
12773
12774 chunk_appendf(&trash,
Willy Tarreau0b5b4802018-09-07 13:49:44 +020012775 ", server %s (#%d), event #%u, src %s:%d\n"
12776 " buffer starts at %llu (including %u out), %u free,\n"
12777 " len %u, wraps at %u, error at position %u\n",
12778 es->srv ? es->srv->id : "<NONE>",
12779 es->srv ? es->srv->puid : -1,
12780 es->ev_id, pn, port,
12781 es->buf_ofs, es->buf_out,
12782 global.tune.bufsize - es->buf_out - es->buf_len,
12783 es->buf_len, es->buf_wrap, es->buf_err);
12784
12785 chunk_appendf(&trash,
12786 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
12787 " HTTP msg state %s(%d), msg flags 0x%08x\n"
12788 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n \n",
12789 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
12790 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
12791 es->ctx.http.m_flags, es->ctx.http.m_clen,
12792 es->ctx.http.m_blen, es->ctx.http.b_flags);
Willy Tarreau12207b32016-11-22 19:48:51 +010012793
Willy Tarreau06d80a92017-10-19 14:32:15 +020012794 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012795 /* Socket buffer full. Let's try again later from the same point */
12796 si_applet_cant_put(si);
12797 return 0;
12798 }
12799 appctx->ctx.errors.ptr = 0;
Willy Tarreau5865a8f2018-09-07 11:51:51 +020012800 appctx->ctx.errors.ev_id = es->ev_id;
Willy Tarreau12207b32016-11-22 19:48:51 +010012801 }
12802
Willy Tarreau5865a8f2018-09-07 11:51:51 +020012803 if (appctx->ctx.errors.ev_id != es->ev_id) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012804 /* the snapshot changed while we were dumping it */
12805 chunk_appendf(&trash,
12806 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012807 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012808 si_applet_cant_put(si);
12809 return 0;
12810 }
12811 goto next;
12812 }
12813
12814 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7480f322018-09-06 19:41:22 +020012815 while (es->buf && appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012816 int newptr;
12817 int newline;
12818
12819 newline = appctx->ctx.errors.bol;
Willy Tarreau7480f322018-09-06 19:41:22 +020012820 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau12207b32016-11-22 19:48:51 +010012821 if (newptr == appctx->ctx.errors.ptr)
12822 return 0;
12823
Willy Tarreau06d80a92017-10-19 14:32:15 +020012824 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012825 /* Socket buffer full. Let's try again later from the same point */
12826 si_applet_cant_put(si);
12827 return 0;
12828 }
12829 appctx->ctx.errors.ptr = newptr;
12830 appctx->ctx.errors.bol = newline;
12831 };
12832 next:
12833 appctx->ctx.errors.bol = 0;
12834 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012835 appctx->ctx.errors.flag ^= 1;
12836 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012837 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012838 }
12839
12840 /* dump complete */
12841 return 1;
12842}
12843
12844/* register cli keywords */
12845static struct cli_kw_list cli_kws = {{ },{
12846 { { "show", "errors", NULL },
12847 "show errors : report last request and response errors for each proxy",
12848 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12849 },
12850 {{},}
12851}};
12852
Willy Tarreau4a568972010-05-12 08:08:50 +020012853/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012854/* All supported ACL keywords must be declared here. */
12855/************************************************************************/
12856
12857/* Note: must not be declared <const> as its list will be overwritten.
12858 * Please take care of keeping this list alphabetically sorted.
12859 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012860static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012861 { "base", "base", PAT_MATCH_STR },
12862 { "base_beg", "base", PAT_MATCH_BEG },
12863 { "base_dir", "base", PAT_MATCH_DIR },
12864 { "base_dom", "base", PAT_MATCH_DOM },
12865 { "base_end", "base", PAT_MATCH_END },
12866 { "base_len", "base", PAT_MATCH_LEN },
12867 { "base_reg", "base", PAT_MATCH_REG },
12868 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012869
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012870 { "cook", "req.cook", PAT_MATCH_STR },
12871 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12872 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12873 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12874 { "cook_end", "req.cook", PAT_MATCH_END },
12875 { "cook_len", "req.cook", PAT_MATCH_LEN },
12876 { "cook_reg", "req.cook", PAT_MATCH_REG },
12877 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012878
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012879 { "hdr", "req.hdr", PAT_MATCH_STR },
12880 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12881 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12882 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12883 { "hdr_end", "req.hdr", PAT_MATCH_END },
12884 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12885 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12886 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012887
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012888 /* these two declarations uses strings with list storage (in place
12889 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12890 * and delete functions are relative to the list management. The parse
12891 * and match method are related to the corresponding fetch methods. This
12892 * is very particular ACL declaration mode.
12893 */
12894 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12895 { "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 +020012896
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012897 { "path", "path", PAT_MATCH_STR },
12898 { "path_beg", "path", PAT_MATCH_BEG },
12899 { "path_dir", "path", PAT_MATCH_DIR },
12900 { "path_dom", "path", PAT_MATCH_DOM },
12901 { "path_end", "path", PAT_MATCH_END },
12902 { "path_len", "path", PAT_MATCH_LEN },
12903 { "path_reg", "path", PAT_MATCH_REG },
12904 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012905
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012906 { "req_ver", "req.ver", PAT_MATCH_STR },
12907 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012908
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012909 { "scook", "res.cook", PAT_MATCH_STR },
12910 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12911 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12912 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12913 { "scook_end", "res.cook", PAT_MATCH_END },
12914 { "scook_len", "res.cook", PAT_MATCH_LEN },
12915 { "scook_reg", "res.cook", PAT_MATCH_REG },
12916 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012917
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012918 { "shdr", "res.hdr", PAT_MATCH_STR },
12919 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12920 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12921 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12922 { "shdr_end", "res.hdr", PAT_MATCH_END },
12923 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12924 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12925 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012926
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012927 { "url", "url", PAT_MATCH_STR },
12928 { "url_beg", "url", PAT_MATCH_BEG },
12929 { "url_dir", "url", PAT_MATCH_DIR },
12930 { "url_dom", "url", PAT_MATCH_DOM },
12931 { "url_end", "url", PAT_MATCH_END },
12932 { "url_len", "url", PAT_MATCH_LEN },
12933 { "url_reg", "url", PAT_MATCH_REG },
12934 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012935
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012936 { "urlp", "urlp", PAT_MATCH_STR },
12937 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12938 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12939 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12940 { "urlp_end", "urlp", PAT_MATCH_END },
12941 { "urlp_len", "urlp", PAT_MATCH_LEN },
12942 { "urlp_reg", "urlp", PAT_MATCH_REG },
12943 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012944
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012945 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012946}};
12947
12948/************************************************************************/
12949/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012950/************************************************************************/
12951/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012952static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012953 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012954 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012955 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12956
Willy Tarreau87b09662015-04-03 00:22:06 +020012957 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012958 { "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 +020012959
12960 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012961 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12962 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12963 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012964
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012965 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12966 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012967
Willy Tarreau409bcde2013-01-08 00:31:00 +010012968 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12969 * are only here to match the ACL's name, are request-only and are used
12970 * for ACL compatibility only.
12971 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012972 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12973 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012974 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12975 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012976
12977 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12978 * only here to match the ACL's name, are request-only and are used for
12979 * ACL compatibility only.
12980 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012981 { "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 +020012982 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012983 { "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 +020012984 { "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 +010012985
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012986 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012987 { "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 +010012988 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012989 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012990 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012991 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012992
12993 /* HTTP protocol on the request path */
12994 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012995 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012996
12997 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012998 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12999 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013000
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013001 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013002 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13003 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013004 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013005
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020013006 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020013007 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13008
Willy Tarreau18ed2562013-01-14 15:56:36 +010013009 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013010 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13011 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013012
Willy Tarreau18ed2562013-01-14 15:56:36 +010013013 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013014 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013015 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13016 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013017
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013018 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013019 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013020 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013021 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013022 { "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 +010013023 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013024 { "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 +010013025
13026 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013027 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013028 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13029 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013030
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013031 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013032 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013033 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013034 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013035 { "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 +010013036 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013037 { "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 +010013038
Willy Tarreau409bcde2013-01-08 00:31:00 +010013039 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013040 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013041 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13042 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013043 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013044
13045 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013046 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013047 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013048 { "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 +020013049 { "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 +010013050
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013051 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013052 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013053 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013054 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013055 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013056 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013057 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013058 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13059 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013060 { "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 +010013061 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013062}};
13063
Willy Tarreau8797c062007-05-07 00:55:35 +020013064
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013065/************************************************************************/
13066/* All supported converter keywords must be declared here. */
13067/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013068/* Note: must not be declared <const> as its list will be overwritten */
13069static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013070 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013071 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013072 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13073 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013074 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013075 { NULL, NULL, 0, 0, 0 },
13076}};
13077
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013078
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013079/************************************************************************/
13080/* All supported http-request action keywords must be declared here. */
13081/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013082struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013083 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013084 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010013085 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013086 { "set-method", parse_set_req_line },
13087 { "set-path", parse_set_req_line },
13088 { "set-query", parse_set_req_line },
13089 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013090 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013091 }
13092};
13093
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013094struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013095 .kw = {
13096 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013097 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013098 { NULL, NULL }
13099 }
13100};
13101
Willy Tarreau8797c062007-05-07 00:55:35 +020013102__attribute__((constructor))
13103static void __http_protocol_init(void)
13104{
13105 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013106 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013107 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013108 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013109 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013110 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013111}
13112
13113
Willy Tarreau58f10d72006-12-04 02:26:12 +010013114/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013115 * Local variables:
13116 * c-indent-level: 8
13117 * c-basic-offset: 8
13118 * End:
13119 */