blob: b153b141333731172437c7bfcbf0b0bfd4141b81 [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/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100491 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200492 * the last CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100493 * The header is also automatically added to the index <hdr_idx>, and the end
494 * of headers is automatically adjusted. The number of bytes added is returned
495 * on success, otherwise <0 is returned indicating an error.
496 */
Willy Tarreau4d893d42018-07-12 15:43:32 +0200497static 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 +0100498{
Willy Tarreau4d893d42018-07-12 15:43:32 +0200499 return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100500}
501
502/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100503 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200504 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100505 * The header is also automatically added to the index <hdr_idx>, and the end
506 * of headers is automatically adjusted. The number of bytes added is returned
507 * on success, otherwise <0 is returned indicating an error.
508 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100509int http_header_add_tail2(struct http_msg *msg,
510 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100511{
512 int bytes;
513
Willy Tarreau4d893d42018-07-12 15:43:32 +0200514 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100515 if (!bytes)
516 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100517 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100518 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
519}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520
521/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100522 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
523 * If so, returns the position of the first non-space character relative to
524 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
525 * to return a pointer to the place after the first space. Returns 0 if the
526 * header name does not match. Checks are case-insensitive.
527 */
528int http_header_match2(const char *hdr, const char *end,
529 const char *name, int len)
530{
531 const char *val;
532
533 if (hdr + len >= end)
534 return 0;
535 if (hdr[len] != ':')
536 return 0;
537 if (strncasecmp(hdr, name, len) != 0)
538 return 0;
539 val = hdr + len + 1;
540 while (val < end && HTTP_IS_SPHT(*val))
541 val++;
542 if ((val >= end) && (len + 2 <= end - hdr))
543 return len + 2; /* we may replace starting from second space */
544 return val - hdr;
545}
546
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200547/* Find the first or next occurrence of header <name> in message buffer <sol>
548 * using headers index <idx>, and return it in the <ctx> structure. This
549 * structure holds everything necessary to use the header and find next
550 * occurrence. If its <idx> member is 0, the header is searched from the
551 * beginning. Otherwise, the next occurrence is returned. The function returns
552 * 1 when it finds a value, and 0 when there is no more. It is very similar to
553 * http_find_header2() except that it is designed to work with full-line headers
554 * whose comma is not a delimiter but is part of the syntax. As a special case,
555 * if ctx->val is NULL when searching for a new values of a header, the current
556 * header is rescanned. This allows rescanning after a header deletion.
557 */
558int http_find_full_header2(const char *name, int len,
559 char *sol, struct hdr_idx *idx,
560 struct hdr_ctx *ctx)
561{
562 char *eol, *sov;
563 int cur_idx, old_idx;
564
565 cur_idx = ctx->idx;
566 if (cur_idx) {
567 /* We have previously returned a header, let's search another one */
568 sol = ctx->line;
569 eol = sol + idx->v[cur_idx].len;
570 goto next_hdr;
571 }
572
573 /* first request for this header */
574 sol += hdr_idx_first_pos(idx);
575 old_idx = 0;
576 cur_idx = hdr_idx_first_idx(idx);
577 while (cur_idx) {
578 eol = sol + idx->v[cur_idx].len;
579
580 if (len == 0) {
581 /* No argument was passed, we want any header.
582 * To achieve this, we simply build a fake request. */
583 while (sol + len < eol && sol[len] != ':')
584 len++;
585 name = sol;
586 }
587
588 if ((len < eol - sol) &&
589 (sol[len] == ':') &&
590 (strncasecmp(sol, name, len) == 0)) {
591 ctx->del = len;
592 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100593 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200594 sov++;
595
596 ctx->line = sol;
597 ctx->prev = old_idx;
598 ctx->idx = cur_idx;
599 ctx->val = sov - sol;
600 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100601 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200602 eol--;
603 ctx->tws++;
604 }
605 ctx->vlen = eol - sov;
606 return 1;
607 }
608 next_hdr:
609 sol = eol + idx->v[cur_idx].cr + 1;
610 old_idx = cur_idx;
611 cur_idx = idx->v[cur_idx].next;
612 }
613 return 0;
614}
615
Willy Tarreauc90dc232015-02-20 13:51:36 +0100616/* Find the first or next header field in message buffer <sol> using headers
617 * index <idx>, and return it in the <ctx> structure. This structure holds
618 * everything necessary to use the header and find next occurrence. If its
619 * <idx> member is 0, the first header is retrieved. Otherwise, the next
620 * occurrence is returned. The function returns 1 when it finds a value, and
621 * 0 when there is no more. It is equivalent to http_find_full_header2() with
622 * no header name.
623 */
624int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
625{
626 char *eol, *sov;
627 int cur_idx, old_idx;
628 int len;
629
630 cur_idx = ctx->idx;
631 if (cur_idx) {
632 /* We have previously returned a header, let's search another one */
633 sol = ctx->line;
634 eol = sol + idx->v[cur_idx].len;
635 goto next_hdr;
636 }
637
638 /* first request for this header */
639 sol += hdr_idx_first_pos(idx);
640 old_idx = 0;
641 cur_idx = hdr_idx_first_idx(idx);
642 while (cur_idx) {
643 eol = sol + idx->v[cur_idx].len;
644
645 len = 0;
646 while (1) {
647 if (len >= eol - sol)
648 goto next_hdr;
649 if (sol[len] == ':')
650 break;
651 len++;
652 }
653
654 ctx->del = len;
655 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100656 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100657 sov++;
658
659 ctx->line = sol;
660 ctx->prev = old_idx;
661 ctx->idx = cur_idx;
662 ctx->val = sov - sol;
663 ctx->tws = 0;
664
Willy Tarreau2235b262016-11-05 15:50:20 +0100665 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100666 eol--;
667 ctx->tws++;
668 }
669 ctx->vlen = eol - sov;
670 return 1;
671
672 next_hdr:
673 sol = eol + idx->v[cur_idx].cr + 1;
674 old_idx = cur_idx;
675 cur_idx = idx->v[cur_idx].next;
676 }
677 return 0;
678}
679
Lukas Tribus23953682017-04-28 13:24:30 +0000680/* Find the end of the header value contained between <s> and <e>. See RFC7230,
681 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100682 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200683 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100684char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200685{
686 int quoted, qdpair;
687
688 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100689
690#if defined(__x86_64__) || \
691 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
692 defined(__ARM_ARCH_7A__)
693 /* speedup: skip everything not a comma nor a double quote */
694 for (; s <= e - sizeof(int); s += sizeof(int)) {
695 unsigned int c = *(int *)s; // comma
696 unsigned int q = c; // quote
697
698 c ^= 0x2c2c2c2c; // contains one zero on a comma
699 q ^= 0x22222222; // contains one zero on a quote
700
701 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
702 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
703
704 if ((c | q) & 0x80808080)
705 break; // found a comma or a quote
706 }
707#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200708 for (; s < e; s++) {
709 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200710 else if (quoted) {
711 if (*s == '\\') qdpair = 1;
712 else if (*s == '"') quoted = 0;
713 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200714 else if (*s == '"') quoted = 1;
715 else if (*s == ',') return s;
716 }
717 return s;
718}
719
720/* Find the first or next occurrence of header <name> in message buffer <sol>
721 * using headers index <idx>, and return it in the <ctx> structure. This
722 * structure holds everything necessary to use the header and find next
723 * occurrence. If its <idx> member is 0, the header is searched from the
724 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 * 1 when it finds a value, and 0 when there is no more. It is designed to work
726 * with headers defined as comma-separated lists. As a special case, if ctx->val
727 * is NULL when searching for a new values of a header, the current header is
728 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200729 */
730int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100731 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200732 struct hdr_ctx *ctx)
733{
Willy Tarreau68085d82010-01-18 14:54:04 +0100734 char *eol, *sov;
735 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200736
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 cur_idx = ctx->idx;
738 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 /* We have previously returned a value, let's search
740 * another one on the same line.
741 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200742 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200743 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100744 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200745 eol = sol + idx->v[cur_idx].len;
746
747 if (sov >= eol)
748 /* no more values in this header */
749 goto next_hdr;
750
Willy Tarreau68085d82010-01-18 14:54:04 +0100751 /* values remaining for this header, skip the comma but save it
752 * for later use (eg: for header deletion).
753 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200754 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100755 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200756 sov++;
757
758 goto return_hdr;
759 }
760
761 /* first request for this header */
762 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100763 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200765 while (cur_idx) {
766 eol = sol + idx->v[cur_idx].len;
767
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200768 if (len == 0) {
769 /* No argument was passed, we want any header.
770 * To achieve this, we simply build a fake request. */
771 while (sol + len < eol && sol[len] != ':')
772 len++;
773 name = sol;
774 }
775
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 if ((len < eol - sol) &&
777 (sol[len] == ':') &&
778 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100779 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200780 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100781 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200782 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100783
Willy Tarreau33a7e692007-06-10 19:45:56 +0200784 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100785 ctx->prev = old_idx;
786 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200787 ctx->idx = cur_idx;
788 ctx->val = sov - sol;
789
790 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200791 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100792 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200793 eol--;
794 ctx->tws++;
795 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200796 ctx->vlen = eol - sov;
797 return 1;
798 }
799 next_hdr:
800 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100801 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200802 cur_idx = idx->v[cur_idx].next;
803 }
804 return 0;
805}
806
807int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200809 struct hdr_ctx *ctx)
810{
811 return http_find_header2(name, strlen(name), sol, idx, ctx);
812}
813
Willy Tarreau68085d82010-01-18 14:54:04 +0100814/* Remove one value of a header. This only works on a <ctx> returned by one of
815 * the http_find_header functions. The value is removed, as well as surrounding
816 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100817 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 * message <msg>. The new index is returned. If it is zero, it means there is
819 * no more header, so any processing may stop. The ctx is always left in a form
820 * that can be handled by http_find_header2() to find next occurrence.
821 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100822int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100823{
824 int cur_idx = ctx->idx;
825 char *sol = ctx->line;
826 struct hdr_idx_elem *hdr;
827 int delta, skip_comma;
828
829 if (!cur_idx)
830 return 0;
831
832 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200833 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100834 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200835 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100836 http_msg_move_end(msg, delta);
837 idx->used--;
838 hdr->len = 0; /* unused entry */
839 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100840 if (idx->tail == ctx->idx)
841 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100842 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100843 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100844 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200845 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100846 return ctx->idx;
847 }
848
849 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200850 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
851 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100852 */
853
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200854 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200855 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200856 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100857 NULL, 0);
858 hdr->len += delta;
859 http_msg_move_end(msg, delta);
860 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200861 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100862 return ctx->idx;
863}
864
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100865/* This function handles a server error at the stream interface level. The
866 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100867 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100868 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100869 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200871static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200872 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873{
Willy Tarreau2019f952017-03-14 11:07:31 +0100874 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100875 channel_auto_read(si_oc(si));
876 channel_abort(si_oc(si));
877 channel_auto_close(si_oc(si));
878 channel_erase(si_oc(si));
879 channel_auto_close(si_ic(si));
880 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200881 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200882 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200883 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200884 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200885 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200886 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887}
888
Willy Tarreau87b09662015-04-03 00:22:06 +0200889/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100890 * and message.
891 */
892
Willy Tarreau83061a82018-07-13 11:56:34 +0200893struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100894{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200895 const int msgnum = http_get_status_idx(s->txn->status);
896
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200897 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200898 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200899 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200900 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100901 else
902 return &http_err_chunks[msgnum];
903}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200904
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100905void
Willy Tarreau83061a82018-07-13 11:56:34 +0200906http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100907{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200908 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100909 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100910 stream_int_retnclose(&s->si[0], msg);
911}
912
Willy Tarreau21d2af32008-02-14 20:25:24 +0100913/* Parse the URI from the given transaction (which is assumed to be in request
914 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
915 * It is returned otherwise.
916 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200917char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100918{
919 char *ptr, *end;
920
Willy Tarreauf37954d2018-06-15 18:31:02 +0200921 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100922 end = ptr + txn->req.sl.rq.u_l;
923
924 if (ptr >= end)
925 return NULL;
926
Lukas Tribus23953682017-04-28 13:24:30 +0000927 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100928 * Request-URI = "*" | absuri | abspath | authority
929 */
930
931 if (*ptr == '*')
932 return NULL;
933
934 if (isalpha((unsigned char)*ptr)) {
935 /* this is a scheme as described by RFC3986, par. 3.1 */
936 ptr++;
937 while (ptr < end &&
938 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
939 ptr++;
940 /* skip '://' */
941 if (ptr == end || *ptr++ != ':')
942 return NULL;
943 if (ptr == end || *ptr++ != '/')
944 return NULL;
945 if (ptr == end || *ptr++ != '/')
946 return NULL;
947 }
948 /* skip [user[:passwd]@]host[:[port]] */
949
950 while (ptr < end && *ptr != '/')
951 ptr++;
952
953 if (ptr == end)
954 return NULL;
955
956 /* OK, we got the '/' ! */
957 return ptr;
958}
959
William Lallemand65ad6e12014-01-31 15:08:02 +0100960/* Parse the URI from the given string and look for the "/" beginning the PATH.
961 * If not found, return NULL. It is returned otherwise.
962 */
963static char *
964http_get_path_from_string(char *str)
965{
966 char *ptr = str;
967
968 /* RFC2616, par. 5.1.2 :
969 * Request-URI = "*" | absuri | abspath | authority
970 */
971
972 if (*ptr == '*')
973 return NULL;
974
975 if (isalpha((unsigned char)*ptr)) {
976 /* this is a scheme as described by RFC3986, par. 3.1 */
977 ptr++;
978 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
979 ptr++;
980 /* skip '://' */
981 if (*ptr == '\0' || *ptr++ != ':')
982 return NULL;
983 if (*ptr == '\0' || *ptr++ != '/')
984 return NULL;
985 if (*ptr == '\0' || *ptr++ != '/')
986 return NULL;
987 }
988 /* skip [user[:passwd]@]host[:[port]] */
989
990 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
991 ptr++;
992
993 if (*ptr == '\0' || *ptr == ' ')
994 return NULL;
995
996 /* OK, we got the '/' ! */
997 return ptr;
998}
999
Willy Tarreau71241ab2012-12-27 11:30:54 +01001000/* Returns a 302 for a redirectable request that reaches a server working in
1001 * in redirect mode. This may only be called just after the stream interface
1002 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1003 * follow normal proxy processing. NOTE: this function is designed to support
1004 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001005 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001006void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007{
1008 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001009 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001011 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001012
1013 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001014 trash.data = strlen(HTTP_302);
1015 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001016
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001017 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001018
Willy Tarreauefb453c2008-10-26 20:49:47 +01001019 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001020 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021 return;
1022
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001023 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001024 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001025 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
1026 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001027 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001028
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001029 /* 3: add the request URI. Since it was already forwarded, we need
1030 * to temporarily rewind the buffer.
1031 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001032 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001033 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001034
Willy Tarreauefb453c2008-10-26 20:49:47 +01001035 path = http_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001036 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 +02001037
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001038 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001039
Willy Tarreauefb453c2008-10-26 20:49:47 +01001040 if (!path)
1041 return;
1042
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001043 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001044 return;
1045
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001046 memcpy(trash.area + trash.data, path, len);
1047 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001048
1049 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001050 memcpy(trash.area + trash.data,
1051 "\r\nProxy-Connection: close\r\n\r\n", 29);
1052 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001053 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001054 memcpy(trash.area + trash.data,
1055 "\r\nConnection: close\r\n\r\n", 23);
1056 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001057 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001058
1059 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001060 si_shutr(si);
1061 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001062 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001063 si->state = SI_ST_CLO;
1064
1065 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001066 txn->status = 302;
1067 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001068
1069 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001070 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001071 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001072}
1073
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001074/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075 * that the server side is closed. Note that err_type is actually a
1076 * bitmask, where almost only aborts may be cumulated with other
1077 * values. We consider that aborted operations are more important
1078 * than timeouts or errors due to the fact that nobody else in the
1079 * logs might explain incomplete retries. All others should avoid
1080 * being cumulated. It should normally not be possible to have multiple
1081 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001082 * Note that connection errors appearing on the second request of a keep-alive
1083 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001084 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001085void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001087 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001089 /* set s->txn->status for http_error_message(s) */
1090 s->txn->status = 503;
1091
Willy Tarreauefb453c2008-10-26 20:49:47 +01001092 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001093 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001094 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001095 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001096 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001097 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001098 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001099 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001100 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001101 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001102 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001103 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001104 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001105 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001106 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001107 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001108 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001109 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001110 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001111 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001112 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001113 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001114 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001115 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001116 http_error_message(s));
1117 else { /* SI_ET_CONN_OTHER and others */
1118 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001119 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001120 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001121 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001122}
1123
Willy Tarreau42250582007-04-01 01:30:43 +02001124extern const char sess_term_cond[8];
1125extern const char sess_fin_state[8];
1126extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001127struct pool_head *pool_head_http_txn;
1128struct pool_head *pool_head_requri;
1129struct pool_head *pool_head_capture = NULL;
1130struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001131
Willy Tarreau117f59e2007-03-04 18:17:17 +01001132/*
1133 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001134 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001135 */
1136void capture_headers(char *som, struct hdr_idx *idx,
1137 char **cap, struct cap_hdr *cap_hdr)
1138{
1139 char *eol, *sol, *col, *sov;
1140 int cur_idx;
1141 struct cap_hdr *h;
1142 int len;
1143
1144 sol = som + hdr_idx_first_pos(idx);
1145 cur_idx = hdr_idx_first_idx(idx);
1146
1147 while (cur_idx) {
1148 eol = sol + idx->v[cur_idx].len;
1149
1150 col = sol;
1151 while (col < eol && *col != ':')
1152 col++;
1153
1154 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001155 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001156 sov++;
1157
1158 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001159 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001160 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1161 if (cap[h->index] == NULL)
1162 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001163 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001164
1165 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001166 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001167 continue;
1168 }
1169
1170 len = eol - sov;
1171 if (len > h->len)
1172 len = h->len;
1173
1174 memcpy(cap[h->index], sov, len);
1175 cap[h->index][len]=0;
1176 }
1177 }
1178 sol = eol + idx->v[cur_idx].cr + 1;
1179 cur_idx = idx->v[cur_idx].next;
1180 }
1181}
1182
Willy Tarreaubaaee002006-06-26 02:48:02 +02001183/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001184 * Returns the data from Authorization header. Function may be called more
1185 * than once so data is stored in txn->auth_data. When no header is found
1186 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001187 * searching again for something we are unable to find anyway. However, if
1188 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001189 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001190 */
1191
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001192int
Willy Tarreau87b09662015-04-03 00:22:06 +02001193get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001194{
1195
Willy Tarreaueee5b512015-04-03 23:46:31 +02001196 struct http_txn *txn = s->txn;
Willy Tarreau83061a82018-07-13 11:56:34 +02001197 struct buffer auth_method;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001198 struct hdr_ctx ctx;
1199 char *h, *p;
1200 int len;
1201
1202#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001203 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001204#endif
1205
1206 if (txn->auth.method == HTTP_AUTH_WRONG)
1207 return 0;
1208
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001209 txn->auth.method = HTTP_AUTH_WRONG;
1210
1211 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001212
1213 if (txn->flags & TX_USE_PX_CONN) {
1214 h = "Proxy-Authorization";
1215 len = strlen(h);
1216 } else {
1217 h = "Authorization";
1218 len = strlen(h);
1219 }
1220
Willy Tarreauf37954d2018-06-15 18:31:02 +02001221 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001222 return 0;
1223
1224 h = ctx.line + ctx.val;
1225
1226 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001227 len = p - h;
1228 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001229 return 0;
1230
David Carlier7365f7d2016-04-04 11:54:42 +01001231 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1232 return 0;
1233
Willy Tarreau5c557d12016-03-13 08:17:02 +01001234 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001235
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001236 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001237 struct buffer *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001238
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001239 len = base64dec(txn->auth.method_data.area,
1240 txn->auth.method_data.data,
1241 http_auth->area, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001242
1243 if (len < 0)
1244 return 0;
1245
1246
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001247 http_auth->area[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001249 p = strchr(http_auth->area, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001250
1251 if (!p)
1252 return 0;
1253
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001254 txn->auth.user = http_auth->area;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001255 *p = '\0';
1256 txn->auth.pass = p+1;
1257
1258 txn->auth.method = HTTP_AUTH_BASIC;
1259 return 1;
1260 }
1261
1262 return 0;
1263}
1264
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001265
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001266/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1267 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1268 * nothing is done and 1 is returned.
1269 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001270static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001271{
1272 int delta;
1273 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001274 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001275
1276 if (msg->sl.rq.v_l != 0)
1277 return 1;
1278
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001279 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1280 if (txn->meth != HTTP_METH_GET)
1281 return 0;
1282
Willy Tarreauf37954d2018-06-15 18:31:02 +02001283 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001284
1285 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001286 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1287 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001288 }
1289 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001290 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001291 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001292 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001293 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001294 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001295 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001296 NULL, NULL);
1297 if (unlikely(!cur_end))
1298 return 0;
1299
1300 /* we have a full HTTP/1.0 request now and we know that
1301 * we have either a CR or an LF at <ptr>.
1302 */
1303 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1304 return 1;
1305}
1306
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001307/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001308 * and "keep-alive" values. If we already know that some headers may safely
1309 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001310 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1311 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001312 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001313 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1314 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1315 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001316 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001317 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001318void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001319{
Willy Tarreau5b154472009-12-21 20:11:07 +01001320 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001321 const char *hdr_val = "Connection";
1322 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001323
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001324 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001325 return;
1326
Willy Tarreau88d349d2010-01-25 12:15:43 +01001327 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1328 hdr_val = "Proxy-Connection";
1329 hdr_len = 16;
1330 }
1331
Willy Tarreau5b154472009-12-21 20:11:07 +01001332 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001333 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001334 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001335 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1336 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001337 if (to_del & 2)
1338 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001339 else
1340 txn->flags |= TX_CON_KAL_SET;
1341 }
1342 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1343 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001344 if (to_del & 1)
1345 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001346 else
1347 txn->flags |= TX_CON_CLO_SET;
1348 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001349 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1350 txn->flags |= TX_HDR_CONN_UPG;
1351 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001352 }
1353
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001354 txn->flags |= TX_HDR_CONN_PRS;
1355 return;
1356}
Willy Tarreau5b154472009-12-21 20:11:07 +01001357
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001358/* Apply desired changes on the Connection: header. Values may be removed and/or
1359 * added depending on the <wanted> flags, which are exclusively composed of
1360 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1361 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1362 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001363void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001364{
1365 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001366 const char *hdr_val = "Connection";
1367 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001368
1369 ctx.idx = 0;
1370
Willy Tarreau88d349d2010-01-25 12:15:43 +01001371
1372 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1373 hdr_val = "Proxy-Connection";
1374 hdr_len = 16;
1375 }
1376
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001377 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001378 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001379 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1380 if (wanted & TX_CON_KAL_SET)
1381 txn->flags |= TX_CON_KAL_SET;
1382 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001383 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001384 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001385 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1386 if (wanted & TX_CON_CLO_SET)
1387 txn->flags |= TX_CON_CLO_SET;
1388 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001389 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001390 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001391 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001392
1393 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1394 return;
1395
1396 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1397 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001398 hdr_val = "Connection: close";
1399 hdr_len = 17;
1400 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1401 hdr_val = "Proxy-Connection: close";
1402 hdr_len = 23;
1403 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001404 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001405 }
1406
1407 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1408 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001409 hdr_val = "Connection: keep-alive";
1410 hdr_len = 22;
1411 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1412 hdr_val = "Proxy-Connection: keep-alive";
1413 hdr_len = 28;
1414 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001415 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001416 }
1417 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001418}
1419
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001420/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1421 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1422 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1423 * Unparsable qvalues return 1000 as "q=1.000".
1424 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001425int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001426{
1427 int q = 1000;
1428
Willy Tarreau506c69a2014-07-08 00:59:48 +02001429 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001430 goto out;
1431 q = (*qvalue++ - '0') * 1000;
1432
1433 if (*qvalue++ != '.')
1434 goto out;
1435
Willy Tarreau506c69a2014-07-08 00:59:48 +02001436 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001437 goto out;
1438 q += (*qvalue++ - '0') * 100;
1439
Willy Tarreau506c69a2014-07-08 00:59:48 +02001440 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001441 goto out;
1442 q += (*qvalue++ - '0') * 10;
1443
Willy Tarreau506c69a2014-07-08 00:59:48 +02001444 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001445 goto out;
1446 q += (*qvalue++ - '0') * 1;
1447 out:
1448 if (q > 1000)
1449 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001450 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001451 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001452 return q;
1453}
William Lallemand82fe75c2012-10-23 10:25:10 +02001454
Willy Tarreau87b09662015-04-03 00:22:06 +02001455void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001456{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001457 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001458 int tmp = TX_CON_WANT_KAL;
1459
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001460 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1461 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001462 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1463 tmp = TX_CON_WANT_TUN;
1464
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001465 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001466 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1467 tmp = TX_CON_WANT_TUN;
1468 }
1469
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001470 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001471 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1472 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001473 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001474 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1475 tmp = TX_CON_WANT_CLO;
1476 else
1477 tmp = TX_CON_WANT_SCL;
1478 }
1479
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001480 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001481 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1482 tmp = TX_CON_WANT_CLO;
1483
1484 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1485 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1486
1487 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1488 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1489 /* parse the Connection header and possibly clean it */
1490 int to_del = 0;
1491 if ((msg->flags & HTTP_MSGF_VER_11) ||
1492 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001493 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001494 to_del |= 2; /* remove "keep-alive" */
1495 if (!(msg->flags & HTTP_MSGF_VER_11))
1496 to_del |= 1; /* remove "close" */
1497 http_parse_connection_header(txn, msg, to_del);
1498 }
1499
1500 /* check if client or config asks for explicit close in KAL/SCL */
1501 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1502 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1503 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1504 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1505 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001506 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001507 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1508}
William Lallemand82fe75c2012-10-23 10:25:10 +02001509
Willy Tarreaud787e662009-07-07 10:14:51 +02001510/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1511 * processing can continue on next analysers, or zero if it either needs more
1512 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001513 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001514 * when it has nothing left to do, and may remove any analyser when it wants to
1515 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001517int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518{
Willy Tarreau59234e92008-11-30 23:51:27 +01001519 /*
1520 * We will parse the partial (or complete) lines.
1521 * We will check the request syntax, and also join multi-line
1522 * headers. An index of all the lines will be elaborated while
1523 * parsing.
1524 *
1525 * For the parsing, we use a 28 states FSM.
1526 *
1527 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02001528 * ci_head(req) = beginning of request
1529 * ci_head(req) + msg->eoh = end of processed headers / start of current one
1530 * ci_tail(req) = end of input data
1531 * msg->eol = end of current header or line (LF or CRLF)
1532 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001533 *
1534 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001535 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001536 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1537 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001538 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau59234e92008-11-30 23:51:27 +01001540 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001541 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001542 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001543 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001544 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Christopher Faulet45073512018-07-20 10:16:29 +02001546 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 +01001547 now_ms, __FUNCTION__,
1548 s,
1549 req,
1550 req->rex, req->wex,
1551 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001552 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +01001553 req->analysers);
1554
Willy Tarreau52a0c602009-08-16 22:45:38 +02001555 /* we're speaking HTTP here, so let's speak HTTP to the client */
1556 s->srv_error = http_return_srv_error;
1557
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001558 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001559 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001560 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1561
Willy Tarreau83e3af02009-12-28 17:39:57 +01001562 /* There's a protected area at the end of the buffer for rewriting
1563 * purposes. We don't want to start to parse the request if the
1564 * protected area is affected, because we may have to move processed
1565 * data later, which is much more complicated.
1566 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001567 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001568 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001569 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001570 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001571 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001572 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001573 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001574 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001575 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001576 return 0;
1577 }
Willy Tarreau188e2302018-06-15 11:11:53 +02001578 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001579 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001580 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001581 }
1582
Willy Tarreauf37954d2018-06-15 18:31:02 +02001583 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001584 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001585 }
1586
Willy Tarreau59234e92008-11-30 23:51:27 +01001587 /* 1: we might have to print this header in debug mode */
1588 if (unlikely((global.mode & MODE_DEBUG) &&
1589 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001590 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001591 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001592
Willy Tarreauf37954d2018-06-15 18:31:02 +02001593 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +02001594 /* this is a bit complex : in case of error on the request line,
1595 * we know that rq.l is still zero, so we display only the part
1596 * up to the end of the line (truncated by debug_hdr).
1597 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001598 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +01001599 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001600
Willy Tarreau59234e92008-11-30 23:51:27 +01001601 sol += hdr_idx_first_pos(&txn->hdr_idx);
1602 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001603
Willy Tarreau59234e92008-11-30 23:51:27 +01001604 while (cur_idx) {
1605 eol = sol + txn->hdr_idx.v[cur_idx].len;
1606 debug_hdr("clihdr", s, sol, eol);
1607 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1608 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001610 }
1611
Willy Tarreau58f10d72006-12-04 02:26:12 +01001612
Willy Tarreau59234e92008-11-30 23:51:27 +01001613 /*
1614 * Now we quickly check if we have found a full valid request.
1615 * If not so, we check the FD and buffer states before leaving.
1616 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001617 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001618 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001619 * on a keep-alive stream, if we encounter and error, close, t/o,
1620 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001621 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001622 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001623 * Last, we may increase some tracked counters' http request errors on
1624 * the cases that are deliberately the client's fault. For instance,
1625 * a timeout or connection reset is not counted as an error. However
1626 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001627 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001628
Willy Tarreau655dce92009-11-08 13:10:58 +01001629 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001631 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001632 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001633 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001634 stream_inc_http_req_ctr(s);
1635 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001636 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001637 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001638 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001639
Willy Tarreau59234e92008-11-30 23:51:27 +01001640 /* 1: Since we are in header mode, if there's no space
1641 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001642 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001643 * must terminate it now.
1644 */
Willy Tarreau23752332018-06-15 14:54:53 +02001645 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001646 /* FIXME: check if URI is set and return Status
1647 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001648 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001649 stream_inc_http_req_ctr(s);
1650 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001651 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001652 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02001653 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01001654 goto return_bad_req;
1655 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001656
Willy Tarreau59234e92008-11-30 23:51:27 +01001657 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001658 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001659 if (!(s->flags & SF_ERR_MASK))
1660 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001661
Willy Tarreaufcffa692010-01-10 14:21:19 +01001662 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001663 goto failed_keep_alive;
1664
Willy Tarreau0f228a02015-05-01 15:37:53 +02001665 if (sess->fe->options & PR_O_IGNORE_PRB)
1666 goto failed_keep_alive;
1667
Willy Tarreau59234e92008-11-30 23:51:27 +01001668 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001669 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001670 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001671 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001672 }
1673
Willy Tarreaudc979f22012-12-04 10:39:01 +01001674 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001675 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001676 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001677 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001678 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001679 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001680 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001681 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001682 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001683 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001684
Willy Tarreaue7dff022015-04-03 01:14:29 +02001685 if (!(s->flags & SF_FINST_MASK))
1686 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001687 return 0;
1688 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001689
Willy Tarreau59234e92008-11-30 23:51:27 +01001690 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001691 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001692 if (!(s->flags & SF_ERR_MASK))
1693 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001694
Willy Tarreaufcffa692010-01-10 14:21:19 +01001695 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001696 goto failed_keep_alive;
1697
Willy Tarreau0f228a02015-05-01 15:37:53 +02001698 if (sess->fe->options & PR_O_IGNORE_PRB)
1699 goto failed_keep_alive;
1700
Willy Tarreau59234e92008-11-30 23:51:27 +01001701 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001702 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001703 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001704 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001705 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001706 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001707 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001708 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001709 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001710 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001711
Willy Tarreau87b09662015-04-03 00:22:06 +02001712 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001713 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001714 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001715 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001716 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001717
Willy Tarreaue7dff022015-04-03 01:14:29 +02001718 if (!(s->flags & SF_FINST_MASK))
1719 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001720 return 0;
1721 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001722
Willy Tarreau59234e92008-11-30 23:51:27 +01001723 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001724 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001725 if (!(s->flags & SF_ERR_MASK))
1726 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001727
Willy Tarreaufcffa692010-01-10 14:21:19 +01001728 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001729 goto failed_keep_alive;
1730
Willy Tarreau0f228a02015-05-01 15:37:53 +02001731 if (sess->fe->options & PR_O_IGNORE_PRB)
1732 goto failed_keep_alive;
1733
Willy Tarreau4076a152009-04-02 15:18:36 +02001734 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001735 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001736 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001737 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001738 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001739 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001740 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001741 stream_inc_http_err_ctr(s);
1742 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001743 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001744 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001745 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001746 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001747
Willy Tarreaue7dff022015-04-03 01:14:29 +02001748 if (!(s->flags & SF_FINST_MASK))
1749 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001750 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001751 }
1752
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001753 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001754 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001755 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001756#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001757 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001758 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001759 /* We need more data, we have to re-enable quick-ack in case we
1760 * previously disabled it, otherwise we might cause the client
1761 * to delay next data.
1762 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001763 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001764 }
1765#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001766
Willy Tarreaufcffa692010-01-10 14:21:19 +01001767 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1768 /* If the client starts to talk, let's fall back to
1769 * request timeout processing.
1770 */
1771 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001772 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001773 }
1774
Willy Tarreau59234e92008-11-30 23:51:27 +01001775 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001776 if (!tick_isset(req->analyse_exp)) {
1777 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1778 (txn->flags & TX_WAIT_NEXT_RQ) &&
1779 tick_isset(s->be->timeout.httpka))
1780 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1781 else
1782 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1783 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001784
Willy Tarreau59234e92008-11-30 23:51:27 +01001785 /* we're not ready yet */
1786 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001787
1788 failed_keep_alive:
1789 /* Here we process low-level errors for keep-alive requests. In
1790 * short, if the request is not the first one and it experiences
1791 * a timeout, read error or shutdown, we just silently close so
1792 * that the client can try again.
1793 */
1794 txn->status = 0;
1795 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001796 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001797 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001798 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001799 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001800 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001801 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001803
Willy Tarreaud787e662009-07-07 10:14:51 +02001804 /* OK now we have a complete HTTP request with indexed headers. Let's
1805 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001806 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001807 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001808 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001809 * byte after the last LF. msg->sov points to the first byte of data.
1810 * msg->eol cannot be trusted because it may have been left uninitialized
1811 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001812 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001813
Willy Tarreau87b09662015-04-03 00:22:06 +02001814 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001815 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001816
Willy Tarreaub16a5742010-01-10 14:46:16 +01001817 if (txn->flags & TX_WAIT_NEXT_RQ) {
1818 /* kill the pending keep-alive timeout */
1819 txn->flags &= ~TX_WAIT_NEXT_RQ;
1820 req->analyse_exp = TICK_ETERNITY;
1821 }
1822
1823
Willy Tarreaud787e662009-07-07 10:14:51 +02001824 /* Maybe we found in invalid header name while we were configured not
1825 * to block on that, so we have to capture it now.
1826 */
1827 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001828 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001829
Willy Tarreau59234e92008-11-30 23:51:27 +01001830 /*
1831 * 1: identify the method
1832 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001833 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001834
1835 /* we can make use of server redirect on GET and HEAD */
1836 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001837 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001838 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001839 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001840 /* PRI is reserved for the HTTP/2 preface */
1841 msg->err_pos = 0;
1842 goto return_bad_req;
1843 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001844
Willy Tarreau59234e92008-11-30 23:51:27 +01001845 /*
1846 * 2: check if the URI matches the monitor_uri.
1847 * We have to do this for every request which gets in, because
1848 * the monitor-uri is defined by the frontend.
1849 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001850 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1851 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001852 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001853 sess->fe->monitor_uri,
1854 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001855 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001856 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001857 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001858 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001859
Willy Tarreaue7dff022015-04-03 01:14:29 +02001860 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001861 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001862
Willy Tarreau59234e92008-11-30 23:51:27 +01001863 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001864 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001865 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001866
Willy Tarreau59234e92008-11-30 23:51:27 +01001867 ret = acl_pass(ret);
1868 if (cond->pol == ACL_COND_UNLESS)
1869 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001870
Willy Tarreau59234e92008-11-30 23:51:27 +01001871 if (ret) {
1872 /* we fail this request, let's return 503 service unavail */
1873 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001874 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001875 if (!(s->flags & SF_ERR_MASK))
1876 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001877 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001878 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001880
Willy Tarreau59234e92008-11-30 23:51:27 +01001881 /* nothing to fail, let's reply normaly */
1882 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001883 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001884 if (!(s->flags & SF_ERR_MASK))
1885 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001886 goto return_prx_cond;
1887 }
1888
1889 /*
1890 * 3: Maybe we have to copy the original REQURI for the logs ?
1891 * Note: we cannot log anymore if the request has been
1892 * classified as invalid.
1893 */
1894 if (unlikely(s->logs.logwait & LW_REQ)) {
1895 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001896 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001897 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001899 if (urilen >= global.tune.requri_len )
1900 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001901 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001902 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001904 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001905 s->do_log(s);
1906 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001907 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001908 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001909 }
Willy Tarreau06619262006-12-17 08:37:22 +01001910
Willy Tarreau91852eb2015-05-01 13:26:00 +02001911 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1912 * exactly one digit "." one digit. This check may be disabled using
1913 * option accept-invalid-http-request.
1914 */
1915 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1916 if (msg->sl.rq.v_l != 8) {
1917 msg->err_pos = msg->sl.rq.v;
1918 goto return_bad_req;
1919 }
1920
Willy Tarreauf37954d2018-06-15 18:31:02 +02001921 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1922 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1923 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1924 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001925 msg->err_pos = msg->sl.rq.v + 4;
1926 goto return_bad_req;
1927 }
1928 }
Willy Tarreau13317662015-05-01 13:47:08 +02001929 else {
1930 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1931 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1932 goto return_bad_req;
1933 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001934
Willy Tarreau5b154472009-12-21 20:11:07 +01001935 /* ... and check if the request is HTTP/1.1 or above */
1936 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001937 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1938 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1939 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001940 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001941
1942 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001943 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 +01001944
Willy Tarreau88d349d2010-01-25 12:15:43 +01001945 /* if the frontend has "option http-use-proxy-header", we'll check if
1946 * we have what looks like a proxied connection instead of a connection,
1947 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1948 * Note that this is *not* RFC-compliant, however browsers and proxies
1949 * happen to do that despite being non-standard :-(
1950 * We consider that a request not beginning with either '/' or '*' is
1951 * a proxied connection, which covers both "scheme://location" and
1952 * CONNECT ip:port.
1953 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001954 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001955 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001956 txn->flags |= TX_USE_PX_CONN;
1957
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001958 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001959 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001960
Willy Tarreau59234e92008-11-30 23:51:27 +01001961 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001962 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02001963 capture_headers(ci_head(req), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001964 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001965
Willy Tarreau557f1992015-05-01 10:05:17 +02001966 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1967 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001968 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001969 * The length of a message body is determined by one of the following
1970 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001971 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001972 * 1. Any response to a HEAD request and any response with a 1xx
1973 * (Informational), 204 (No Content), or 304 (Not Modified) status
1974 * code is always terminated by the first empty line after the
1975 * header fields, regardless of the header fields present in the
1976 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001977 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001978 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1979 * the connection will become a tunnel immediately after the empty
1980 * line that concludes the header fields. A client MUST ignore any
1981 * Content-Length or Transfer-Encoding header fields received in
1982 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001983 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001984 * 3. If a Transfer-Encoding header field is present and the chunked
1985 * transfer coding (Section 4.1) is the final encoding, the message
1986 * body length is determined by reading and decoding the chunked
1987 * data until the transfer coding indicates the data is complete.
1988 *
1989 * If a Transfer-Encoding header field is present in a response and
1990 * the chunked transfer coding is not the final encoding, the
1991 * message body length is determined by reading the connection until
1992 * it is closed by the server. If a Transfer-Encoding header field
1993 * is present in a request and the chunked transfer coding is not
1994 * the final encoding, the message body length cannot be determined
1995 * reliably; the server MUST respond with the 400 (Bad Request)
1996 * status code and then close the connection.
1997 *
1998 * If a message is received with both a Transfer-Encoding and a
1999 * Content-Length header field, the Transfer-Encoding overrides the
2000 * Content-Length. Such a message might indicate an attempt to
2001 * perform request smuggling (Section 9.5) or response splitting
2002 * (Section 9.4) and ought to be handled as an error. A sender MUST
2003 * remove the received Content-Length field prior to forwarding such
2004 * a message downstream.
2005 *
2006 * 4. If a message is received without Transfer-Encoding and with
2007 * either multiple Content-Length header fields having differing
2008 * field-values or a single Content-Length header field having an
2009 * invalid value, then the message framing is invalid and the
2010 * recipient MUST treat it as an unrecoverable error. If this is a
2011 * request message, the server MUST respond with a 400 (Bad Request)
2012 * status code and then close the connection. If this is a response
2013 * message received by a proxy, the proxy MUST close the connection
2014 * to the server, discard the received response, and send a 502 (Bad
2015 * Gateway) response to the client. If this is a response message
2016 * received by a user agent, the user agent MUST close the
2017 * connection to the server and discard the received response.
2018 *
2019 * 5. If a valid Content-Length header field is present without
2020 * Transfer-Encoding, its decimal value defines the expected message
2021 * body length in octets. If the sender closes the connection or
2022 * the recipient times out before the indicated number of octets are
2023 * received, the recipient MUST consider the message to be
2024 * incomplete and close the connection.
2025 *
2026 * 6. If this is a request message and none of the above are true, then
2027 * the message body length is zero (no message body is present).
2028 *
2029 * 7. Otherwise, this is a response message without a declared message
2030 * body length, so the message body length is determined by the
2031 * number of octets received prior to the server closing the
2032 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002033 */
2034
Willy Tarreau32b47f42009-10-18 20:55:02 +02002035 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002036 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02002037 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002038 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002039 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002040 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002041 /* chunked not last, return badreq */
2042 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002043 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002044 }
2045
Willy Tarreau1c913912015-04-30 10:57:51 +02002046 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002047 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002048 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002049 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02002050 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2051 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02002052 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002053 signed long long cl;
2054
Willy Tarreauad14f752011-09-02 20:33:27 +02002055 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002056 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002057 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002058 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002059
Willy Tarreauad14f752011-09-02 20:33:27 +02002060 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002061 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002062 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002063 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002064
Willy Tarreauad14f752011-09-02 20:33:27 +02002065 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002066 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002067 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002068 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002069
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002070 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002071 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002072 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002073 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002074
Christopher Fauletbe821b92017-03-30 11:21:53 +02002075 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002076 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002077 }
2078
Willy Tarreau34dfc602015-05-01 10:09:49 +02002079 /* even bodyless requests have a known length */
2080 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002081
Willy Tarreau179085c2014-04-28 16:48:56 +02002082 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2083 * only change if both the request and the config reference something else.
2084 * Option httpclose by itself sets tunnel mode where headers are mangled.
2085 * However, if another mode is set, it will affect it (eg: server-close/
2086 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2087 * if FE and BE have the same settings (common). The method consists in
2088 * checking if options changed between the two calls (implying that either
2089 * one is non-null, or one of them is non-null and we are there for the first
2090 * time.
2091 */
2092 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002093 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002094 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002095
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002096 /* we may have to wait for the request's body */
2097 if ((s->be->options & PR_O_WREQ_BODY) &&
2098 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2099 req->analysers |= AN_REQ_HTTP_BODY;
2100
Willy Tarreau83ece462017-12-21 15:13:09 +01002101 /*
2102 * RFC7234#4:
2103 * A cache MUST write through requests with methods
2104 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2105 * the origin server; i.e., a cache is not allowed
2106 * to generate a reply to such a request before
2107 * having forwarded the request and having received
2108 * a corresponding response.
2109 *
2110 * RFC7231#4.2.1:
2111 * Of the request methods defined by this
2112 * specification, the GET, HEAD, OPTIONS, and TRACE
2113 * methods are defined to be safe.
2114 */
2115 if (likely(txn->meth == HTTP_METH_GET ||
2116 txn->meth == HTTP_METH_HEAD ||
2117 txn->meth == HTTP_METH_OPTIONS ||
2118 txn->meth == HTTP_METH_TRACE))
2119 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2120
Willy Tarreaud787e662009-07-07 10:14:51 +02002121 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002122 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002123 req->analyse_exp = TICK_ETERNITY;
2124 return 1;
2125
2126 return_bad_req:
2127 /* We centralize bad requests processing here */
2128 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2129 /* we detected a parsing error. We want to archive this request
2130 * in the dedicated proxy area for later troubleshooting.
2131 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002132 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002133 }
2134
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002135 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002136 txn->req.msg_state = HTTP_MSG_ERROR;
2137 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002138 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002139
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002140 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002141 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002142 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002143
2144 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002145 if (!(s->flags & SF_ERR_MASK))
2146 s->flags |= SF_ERR_PRXCOND;
2147 if (!(s->flags & SF_FINST_MASK))
2148 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002149
Christopher Faulet0184ea72017-01-05 14:06:34 +01002150 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002151 req->analyse_exp = TICK_ETERNITY;
2152 return 0;
2153}
2154
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002155
Willy Tarreau347a35d2013-11-22 17:51:09 +01002156/* This function prepares an applet to handle the stats. It can deal with the
2157 * "100-continue" expectation, check that admin rules are met for POST requests,
2158 * and program a response message if something was unexpected. It cannot fail
2159 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002160 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002161 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002162 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002163 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002164int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002165{
2166 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002167 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002168 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002169 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002170 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002171 struct uri_auth *uri_auth = s->be->uri_auth;
2172 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002173 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002174
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002175 appctx = si_appctx(si);
2176 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2177 appctx->st1 = appctx->st2 = 0;
2178 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2179 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002180 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002181 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002182
Willy Tarreauf37954d2018-06-15 18:31:02 +02002183 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002184 lookup = uri + uri_auth->uri_len;
2185
2186 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2187 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002188 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002189 break;
2190 }
2191 }
2192
2193 if (uri_auth->refresh) {
2194 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2195 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002196 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002197 break;
2198 }
2199 }
2200 }
2201
2202 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2203 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002204 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002205 break;
2206 }
2207 }
2208
Willy Tarreau1e62df92016-01-11 18:57:53 +01002209 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2210 if (memcmp(h, ";typed", 6) == 0) {
2211 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2212 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2213 break;
2214 }
2215 }
2216
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002217 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2218 if (memcmp(h, ";st=", 4) == 0) {
2219 int i;
2220 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002221 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002222 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2223 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002224 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002225 break;
2226 }
2227 }
2228 break;
2229 }
2230 }
2231
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 appctx->ctx.stats.scope_str = 0;
2233 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002234 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2235 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2236 int itx = 0;
2237 const char *h2;
2238 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2239 const char *err;
2240
2241 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2242 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002243 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002244 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2245 itx++;
2246 h++;
2247 }
2248
2249 if (itx > STAT_SCOPE_TXT_MAXLEN)
2250 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002251 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002252
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002253 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002254 memcpy(scope_txt, h2, itx);
2255 scope_txt[itx] = '\0';
2256 err = invalid_char(scope_txt);
2257 if (err) {
2258 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002259 appctx->ctx.stats.scope_str = 0;
2260 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002261 }
2262 break;
2263 }
2264 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002265
2266 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002267 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002268 int ret = 1;
2269
2270 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002271 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 +01002272 ret = acl_pass(ret);
2273 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2274 ret = !ret;
2275 }
2276
2277 if (ret) {
2278 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002279 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002280 break;
2281 }
2282 }
2283
2284 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002285 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002286 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002287 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002288 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2289 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002290 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002291 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002292 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002293 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2294 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002295 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002296 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002297 else {
2298 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002299 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002300 }
2301
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002302 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002303 return 1;
2304}
2305
Lukas Tribus67db8df2013-06-23 17:37:13 +02002306/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2307 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2308 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002309void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002310{
2311#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002312 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002313 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2314#endif
2315#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002316 if (from->ss_family == AF_INET6) {
2317 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002318 /* v4-mapped addresses need IP_TOS */
2319 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2320 else
2321 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2322 }
2323#endif
2324}
2325
Willy Tarreau87b09662015-04-03 00:22:06 +02002326int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002327 const char* name, unsigned int name_len,
2328 const char *str, struct my_regex *re,
2329 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002330{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002331 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002332 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02002333 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002334 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2335 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02002336 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002337
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002338 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002339
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002340 /* Choose the header browsing function. */
2341 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002342 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002343 http_find_hdr_func = http_find_header2;
2344 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002345 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002346 http_find_hdr_func = http_find_full_header2;
2347 break;
2348 default: /* impossible */
2349 return -1;
2350 }
2351
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002352 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2353 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02002354 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002355 char *val = ctx.line + ctx.val;
2356 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002357
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002358 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2359 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002360
Willy Tarreau6e27be12018-08-22 04:46:47 +02002361 len = exp_replace(output->area, output->size, val, str, pmatch);
2362 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002363 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002364
Willy Tarreau6e27be12018-08-22 04:46:47 +02002365 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002366
2367 hdr->len += delta;
2368 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002369
2370 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002371 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002372 }
2373
2374 return 0;
2375}
2376
Willy Tarreau87b09662015-04-03 00:22:06 +02002377static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002378 const char* name, unsigned int name_len,
2379 struct list *fmt, struct my_regex *re,
2380 int action)
2381{
Willy Tarreau83061a82018-07-13 11:56:34 +02002382 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002383 int ret = -1;
2384
2385 replace = alloc_trash_chunk();
2386 if (!replace)
2387 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002388
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002389 replace->data = build_logline(s, replace->area, replace->size, fmt);
2390 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002391 goto leave;
2392
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002393 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
2394 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002395
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002396 leave:
2397 free_trash_chunk(replace);
2398 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002399}
2400
Willy Tarreau87b09662015-04-03 00:22:06 +02002401/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002402 * transaction <txn>. Returns the verdict of the first rule that prevents
2403 * further processing of the request (auth, deny, ...), and defaults to
2404 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2405 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002406 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2407 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2408 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002409 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002410enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002411http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002412{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002413 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002414 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002415 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002416 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002417 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002418 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002419 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002420 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002421
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002422 /* If "the current_rule_list" match the executed rule list, we are in
2423 * resume condition. If a resume is needed it is always in the action
2424 * and never in the ACL or converters. In this case, we initialise the
2425 * current rule, and go to the action execution point.
2426 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002427 if (s->current_rule) {
2428 rule = s->current_rule;
2429 s->current_rule = NULL;
2430 if (s->current_rule_list == rules)
2431 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002432 }
2433 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002434
Willy Tarreauff011f22011-01-06 17:51:27 +01002435 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002436
Willy Tarreau96257ec2012-12-27 10:46:37 +01002437 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002438 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002439 int ret;
2440
Willy Tarreau192252e2015-04-04 01:47:55 +02002441 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002442 ret = acl_pass(ret);
2443
Willy Tarreauff011f22011-01-06 17:51:27 +01002444 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002445 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002446
2447 if (!ret) /* condition not matched */
2448 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002449 }
2450
Willy Tarreauacc98002015-09-27 23:34:39 +02002451 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002452resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002453 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002454 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002455 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002456
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002457 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002458 if (deny_status)
2459 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002460 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002461
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002462 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002463 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002464 if (deny_status)
2465 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002466 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002467
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002468 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002469 /* Auth might be performed on regular http-req rules as well as on stats */
2470 auth_realm = rule->arg.auth.realm;
2471 if (!auth_realm) {
2472 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2473 auth_realm = STATS_DEFAULT_REALM;
2474 else
2475 auth_realm = px->id;
2476 }
2477 /* send 401/407 depending on whether we use a proxy or not. We still
2478 * count one error, because normal browsing won't significantly
2479 * increase the counter but brute force attempts will.
2480 */
2481 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2482 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002483 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002484 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002485 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002486
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002487 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002488 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2489 return HTTP_RULE_RES_BADREQ;
2490 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002491
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002492 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002493 s->task->nice = rule->arg.nice;
2494 break;
2495
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002496 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002497 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002498 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002499 break;
2500
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002501 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002502#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002503 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002504 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002505#endif
2506 break;
2507
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002508 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002509 s->logs.level = rule->arg.loglevel;
2510 break;
2511
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002512 case ACT_HTTP_REPLACE_HDR:
2513 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002514 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2515 rule->arg.hdr_add.name_len,
2516 &rule->arg.hdr_add.fmt,
2517 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002518 return HTTP_RULE_RES_BADREQ;
2519 break;
2520
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002521 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002522 ctx.idx = 0;
2523 /* remove all occurrences of the header */
2524 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002525 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002526 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002527 }
Willy Tarreau85603282015-01-21 20:39:27 +01002528 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002529
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002530 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002531 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002532 /* The scope of the trash buffer must be limited to this function. The
2533 * build_logline() function can execute a lot of other function which
2534 * can use the trash buffer. So for limiting the scope of this global
2535 * buffer, we build first the header value using build_logline, and
2536 * after we store the header name.
2537 */
Willy Tarreau83061a82018-07-13 11:56:34 +02002538 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002539
2540 replace = alloc_trash_chunk();
2541 if (!replace)
2542 return HTTP_RULE_RES_BADREQ;
2543
Thierry Fournier4b788f72016-06-01 13:35:36 +02002544 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002545 len += build_logline(s, replace->area + len,
2546 replace->size - len,
2547 &rule->arg.hdr_add.fmt);
2548 memcpy(replace->area, rule->arg.hdr_add.name,
2549 rule->arg.hdr_add.name_len);
2550 replace->area[rule->arg.hdr_add.name_len] = ':';
2551 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
2552 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002553
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002554 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002555 /* remove all occurrences of the header */
2556 ctx.idx = 0;
2557 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002558 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002559 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2560 }
2561 }
2562
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002563 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002564 static unsigned char rate_limit = 0;
2565
2566 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002567 replace->area[rule->arg.hdr_add.name_len] = 0;
2568 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,
2569 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002570 }
2571
2572 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2573 if (sess->fe != s->be)
2574 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2575 if (sess->listener->counters)
2576 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2577 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002578
2579 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002580 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002581 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002582
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002583 case ACT_HTTP_DEL_ACL:
2584 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002585 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002586 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002587
2588 /* collect reference */
2589 ref = pat_ref_lookup(rule->arg.map.ref);
2590 if (!ref)
2591 continue;
2592
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002593 /* allocate key */
2594 key = alloc_trash_chunk();
2595 if (!key)
2596 return HTTP_RULE_RES_BADREQ;
2597
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002598 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002599 key->data = build_logline(s, key->area, key->size,
2600 &rule->arg.map.key);
2601 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002602
2603 /* perform update */
2604 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002605 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002606 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002607 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002608
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002609 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002610 break;
2611 }
2612
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002613 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002614 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002615 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002616
2617 /* collect reference */
2618 ref = pat_ref_lookup(rule->arg.map.ref);
2619 if (!ref)
2620 continue;
2621
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002622 /* allocate key */
2623 key = alloc_trash_chunk();
2624 if (!key)
2625 return HTTP_RULE_RES_BADREQ;
2626
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002627 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002628 key->data = build_logline(s, key->area, key->size,
2629 &rule->arg.map.key);
2630 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002631
2632 /* perform update */
2633 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002634 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002635 if (pat_ref_find_elt(ref, key->area) == NULL)
2636 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002637 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002638
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002639 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002640 break;
2641 }
2642
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002643 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002644 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002645 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002646
2647 /* collect reference */
2648 ref = pat_ref_lookup(rule->arg.map.ref);
2649 if (!ref)
2650 continue;
2651
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002652 /* allocate key */
2653 key = alloc_trash_chunk();
2654 if (!key)
2655 return HTTP_RULE_RES_BADREQ;
2656
2657 /* allocate value */
2658 value = alloc_trash_chunk();
2659 if (!value) {
2660 free_trash_chunk(key);
2661 return HTTP_RULE_RES_BADREQ;
2662 }
2663
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002664 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002665 key->data = build_logline(s, key->area, key->size,
2666 &rule->arg.map.key);
2667 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002668
2669 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 value->data = build_logline(s, value->area,
2671 value->size,
2672 &rule->arg.map.value);
2673 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002674
2675 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002677 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002678 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002679 else
2680 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002681 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002682
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002683 free_trash_chunk(key);
2684 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002685 break;
2686 }
William Lallemand73025dd2014-04-24 14:38:37 +02002687
Thierry FOURNIER42148732015-09-02 17:17:33 +02002688 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002689 if ((s->req.flags & CF_READ_ERROR) ||
2690 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2691 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2692 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002693 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002694
Willy Tarreauacc98002015-09-27 23:34:39 +02002695 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002696 case ACT_RET_ERR:
2697 case ACT_RET_CONT:
2698 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002699 case ACT_RET_STOP:
2700 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002701 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002702 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002703 return HTTP_RULE_RES_YIELD;
2704 }
William Lallemand73025dd2014-04-24 14:38:37 +02002705 break;
2706
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002707 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002708 /* Note: only the first valid tracking parameter of each
2709 * applies.
2710 */
2711
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002712 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002713 struct stktable *t;
2714 struct stksess *ts;
2715 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002716 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002717
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002718 t = rule->arg.trk_ctr.table.t;
2719 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 +02002720
2721 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002722 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002723
2724 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002725 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2726 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2727 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002728 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002729
Emeric Brun819fc6f2017-06-13 19:37:32 +02002730 if (ptr1)
2731 stktable_data_cast(ptr1, http_req_cnt)++;
2732
2733 if (ptr2)
2734 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2735 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2736
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002737 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002738
2739 /* If data was modified, we need to touch to re-schedule sync */
2740 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002741 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002742
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002743 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002744 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002745 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002746 }
2747 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002748 break;
2749
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002750 /* other flags exists, but normaly, they never be matched. */
2751 default:
2752 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002753 }
2754 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002755
2756 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002757 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002758}
2759
Willy Tarreau71241ab2012-12-27 11:30:54 +01002760
Willy Tarreau51d861a2015-05-22 17:30:48 +02002761/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2762 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2763 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2764 * is returned, the process can continue the evaluation of next rule list. If
2765 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2766 * is returned, it means the operation could not be processed and a server error
2767 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2768 * deny rule. If *YIELD is returned, the caller must call again the function
2769 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002770 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002771static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002772http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002773{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002774 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002775 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002776 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002777 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002778 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002779 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002780
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002781 /* If "the current_rule_list" match the executed rule list, we are in
2782 * resume condition. If a resume is needed it is always in the action
2783 * and never in the ACL or converters. In this case, we initialise the
2784 * current rule, and go to the action execution point.
2785 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002786 if (s->current_rule) {
2787 rule = s->current_rule;
2788 s->current_rule = NULL;
2789 if (s->current_rule_list == rules)
2790 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002791 }
2792 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002793
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002794 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002795
2796 /* check optional condition */
2797 if (rule->cond) {
2798 int ret;
2799
Willy Tarreau192252e2015-04-04 01:47:55 +02002800 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002801 ret = acl_pass(ret);
2802
2803 if (rule->cond->pol == ACL_COND_UNLESS)
2804 ret = !ret;
2805
2806 if (!ret) /* condition not matched */
2807 continue;
2808 }
2809
Willy Tarreauacc98002015-09-27 23:34:39 +02002810 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002811resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002812 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002813 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002814 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002815
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002816 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002817 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002818 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002819
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002820 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002821 s->task->nice = rule->arg.nice;
2822 break;
2823
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002824 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002825 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002826 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002827 break;
2828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002829 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002830#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002831 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002832 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002833#endif
2834 break;
2835
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002836 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002837 s->logs.level = rule->arg.loglevel;
2838 break;
2839
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002840 case ACT_HTTP_REPLACE_HDR:
2841 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002842 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2843 rule->arg.hdr_add.name_len,
2844 &rule->arg.hdr_add.fmt,
2845 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002846 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002847 break;
2848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002849 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002850 ctx.idx = 0;
2851 /* remove all occurrences of the header */
2852 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002853 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002854 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2855 }
Willy Tarreau85603282015-01-21 20:39:27 +01002856 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002857
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002858 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002859 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002860 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002861
2862 replace = alloc_trash_chunk();
2863 if (!replace)
2864 return HTTP_RULE_RES_BADREQ;
2865
2866 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002867 memcpy(replace->area, rule->arg.hdr_add.name,
2868 rule->arg.hdr_add.name_len);
2869 replace->data = rule->arg.hdr_add.name_len;
2870 replace->area[replace->data++] = ':';
2871 replace->area[replace->data++] = ' ';
2872 replace->data += build_logline(s,
2873 replace->area + replace->data,
2874 replace->size - replace->data,
2875 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002876
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002877 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002878 /* remove all occurrences of the header */
2879 ctx.idx = 0;
2880 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002881 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002882 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2883 }
2884 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002885
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002886 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002887 static unsigned char rate_limit = 0;
2888
2889 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002890 replace->area[rule->arg.hdr_add.name_len] = 0;
2891 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,
2892 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002893 }
2894
2895 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2896 if (sess->fe != s->be)
2897 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2898 if (sess->listener->counters)
2899 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2900 if (objt_server(s->target))
2901 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2902 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002903
2904 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002905 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002906 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002907
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002908 case ACT_HTTP_DEL_ACL:
2909 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002910 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002911 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002912
2913 /* collect reference */
2914 ref = pat_ref_lookup(rule->arg.map.ref);
2915 if (!ref)
2916 continue;
2917
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002918 /* allocate key */
2919 key = alloc_trash_chunk();
2920 if (!key)
2921 return HTTP_RULE_RES_BADREQ;
2922
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002923 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002924 key->data = build_logline(s, key->area, key->size,
2925 &rule->arg.map.key);
2926 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002927
2928 /* perform update */
2929 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002930 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002931 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002932 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002933
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002934 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002935 break;
2936 }
2937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002938 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002939 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002940 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002941
2942 /* collect reference */
2943 ref = pat_ref_lookup(rule->arg.map.ref);
2944 if (!ref)
2945 continue;
2946
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002947 /* allocate key */
2948 key = alloc_trash_chunk();
2949 if (!key)
2950 return HTTP_RULE_RES_BADREQ;
2951
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002952 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002953 key->data = build_logline(s, key->area, key->size,
2954 &rule->arg.map.key);
2955 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002956
2957 /* perform update */
2958 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002959 if (pat_ref_find_elt(ref, key->area) == NULL)
2960 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002961
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002962 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002963 break;
2964 }
2965
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002966 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002967 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002968 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002969
2970 /* collect reference */
2971 ref = pat_ref_lookup(rule->arg.map.ref);
2972 if (!ref)
2973 continue;
2974
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002975 /* allocate key */
2976 key = alloc_trash_chunk();
2977 if (!key)
2978 return HTTP_RULE_RES_BADREQ;
2979
2980 /* allocate value */
2981 value = alloc_trash_chunk();
2982 if (!value) {
2983 free_trash_chunk(key);
2984 return HTTP_RULE_RES_BADREQ;
2985 }
2986
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002987 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002988 key->data = build_logline(s, key->area, key->size,
2989 &rule->arg.map.key);
2990 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002991
2992 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002993 value->data = build_logline(s, value->area,
2994 value->size,
2995 &rule->arg.map.value);
2996 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002997
2998 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002999 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003000 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003001 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003002 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003003 else
3004 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003006 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003007 free_trash_chunk(key);
3008 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003009 break;
3010 }
William Lallemand73025dd2014-04-24 14:38:37 +02003011
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003012 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003013 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3014 return HTTP_RULE_RES_BADREQ;
3015 return HTTP_RULE_RES_DONE;
3016
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003017 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3018 /* Note: only the first valid tracking parameter of each
3019 * applies.
3020 */
3021
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003022 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003023 struct stktable *t;
3024 struct stksess *ts;
3025 struct stktable_key *key;
3026 void *ptr;
3027
3028 t = rule->arg.trk_ctr.table.t;
3029 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3030
3031 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003032 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003033
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003034 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003035
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003036 /* let's count a new HTTP request as it's the first time we do it */
3037 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3038 if (ptr)
3039 stktable_data_cast(ptr, http_req_cnt)++;
3040
3041 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3042 if (ptr)
3043 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3044 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3045
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003046 /* When the client triggers a 4xx from the server, it's most often due
3047 * to a missing object or permission. These events should be tracked
3048 * because if they happen often, it may indicate a brute force or a
3049 * vulnerability scan. Normally this is done when receiving the response
3050 * but here we're tracking after this ought to have been done so we have
3051 * to do it on purpose.
3052 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003053 if ((unsigned)(txn->status - 400) < 100) {
3054 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3055 if (ptr)
3056 stktable_data_cast(ptr, http_err_cnt)++;
3057
3058 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3059 if (ptr)
3060 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3061 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3062 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003063
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003064 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003065
Emeric Brun0fed0b02017-11-29 16:15:07 +01003066 /* If data was modified, we need to touch to re-schedule sync */
3067 stktable_touch_local(t, ts, 0);
3068
Emeric Brun819fc6f2017-06-13 19:37:32 +02003069 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3070 if (sess->fe != s->be)
3071 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3072
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003073 }
3074 }
3075 break;
3076
Thierry FOURNIER42148732015-09-02 17:17:33 +02003077 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003078 if ((s->req.flags & CF_READ_ERROR) ||
3079 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3080 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3081 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003082 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003083
Willy Tarreauacc98002015-09-27 23:34:39 +02003084 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003085 case ACT_RET_ERR:
3086 case ACT_RET_CONT:
3087 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003088 case ACT_RET_STOP:
3089 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003090 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003091 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003092 return HTTP_RULE_RES_YIELD;
3093 }
William Lallemand73025dd2014-04-24 14:38:37 +02003094 break;
3095
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003096 /* other flags exists, but normaly, they never be matched. */
3097 default:
3098 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003099 }
3100 }
3101
3102 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003103 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003104}
3105
3106
Willy Tarreau71241ab2012-12-27 11:30:54 +01003107/* Perform an HTTP redirect based on the information in <rule>. The function
3108 * returns non-zero on success, or zero in case of a, irrecoverable error such
3109 * as too large a request to build a valid response.
3110 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003111static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003112{
Willy Tarreaub329a312015-05-22 16:27:37 +02003113 struct http_msg *req = &txn->req;
3114 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003115 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02003116 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003117 int ret = 0;
3118
3119 chunk = alloc_trash_chunk();
3120 if (!chunk)
3121 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003122
3123 /* build redirect message */
3124 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003125 case 308:
3126 msg_fmt = HTTP_308;
3127 break;
3128 case 307:
3129 msg_fmt = HTTP_307;
3130 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003131 case 303:
3132 msg_fmt = HTTP_303;
3133 break;
3134 case 301:
3135 msg_fmt = HTTP_301;
3136 break;
3137 case 302:
3138 default:
3139 msg_fmt = HTTP_302;
3140 break;
3141 }
3142
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003143 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3144 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003145
3146 switch(rule->type) {
3147 case REDIRECT_TYPE_SCHEME: {
3148 const char *path;
3149 const char *host;
3150 struct hdr_ctx ctx;
3151 int pathlen;
3152 int hostlen;
3153
3154 host = "";
3155 hostlen = 0;
3156 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003157 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003158 host = ctx.line + ctx.val;
3159 hostlen = ctx.vlen;
3160 }
3161
3162 path = http_get_path(txn);
3163 /* build message using path */
3164 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003165 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003166 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3167 int qs = 0;
3168 while (qs < pathlen) {
3169 if (path[qs] == '?') {
3170 pathlen = qs;
3171 break;
3172 }
3173 qs++;
3174 }
3175 }
3176 } else {
3177 path = "/";
3178 pathlen = 1;
3179 }
3180
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003181 if (rule->rdr_str) { /* this is an old "redirect" rule */
3182 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003183 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003184 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003185
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003186 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003187 memcpy(chunk->area + chunk->data, rule->rdr_str,
3188 rule->rdr_len);
3189 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003190 }
3191 else {
3192 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003193 chunk->data += build_logline(s,
3194 chunk->area + chunk->data,
3195 chunk->size - chunk->data,
3196 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003197
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003198 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003199 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003200 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003201 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003202 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003203 memcpy(chunk->area + chunk->data, "://", 3);
3204 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003205
3206 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003207 memcpy(chunk->area + chunk->data, host, hostlen);
3208 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003209
3210 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003211 memcpy(chunk->area + chunk->data, path, pathlen);
3212 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003213
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003214 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003215 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003216 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003217 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003218 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003219 chunk->area[chunk->data] = '/';
3220 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003221 }
3222
3223 break;
3224 }
3225 case REDIRECT_TYPE_PREFIX: {
3226 const char *path;
3227 int pathlen;
3228
3229 path = http_get_path(txn);
3230 /* build message using path */
3231 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003232 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003233 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3234 int qs = 0;
3235 while (qs < pathlen) {
3236 if (path[qs] == '?') {
3237 pathlen = qs;
3238 break;
3239 }
3240 qs++;
3241 }
3242 }
3243 } else {
3244 path = "/";
3245 pathlen = 1;
3246 }
3247
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003248 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003249 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003250 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003251
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003252 /* add prefix. Note that if prefix == "/", we don't want to
3253 * add anything, otherwise it makes it hard for the user to
3254 * configure a self-redirection.
3255 */
3256 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003257 memcpy(chunk->area + chunk->data,
3258 rule->rdr_str, rule->rdr_len);
3259 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003260 }
3261 }
3262 else {
3263 /* add prefix 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);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003268
3269 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003270 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003271 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003272 }
3273
3274 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003275 memcpy(chunk->area + chunk->data, path, pathlen);
3276 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003277
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003278 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003279 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003280 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003281 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003282 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003283 chunk->area[chunk->data] = '/';
3284 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003285 }
3286
3287 break;
3288 }
3289 case REDIRECT_TYPE_LOCATION:
3290 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003291 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003292 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003293 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003294
3295 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003296 memcpy(chunk->area + chunk->data, rule->rdr_str,
3297 rule->rdr_len);
3298 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003299 }
3300 else {
3301 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003302 chunk->data += build_logline(s,
3303 chunk->area + chunk->data,
3304 chunk->size - chunk->data,
3305 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003306
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003307 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003308 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003309 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003310 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003311 break;
3312 }
3313
3314 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003315 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
3316 chunk->data += 14;
3317 memcpy(chunk->area + chunk->data, rule->cookie_str,
3318 rule->cookie_len);
3319 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003320 }
3321
Willy Tarreau19b14122017-02-28 09:48:11 +01003322 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003323 txn->status = rule->code;
3324 /* let's log the request time */
3325 s->logs.tv_request = now;
3326
Christopher Fauletbe821b92017-03-30 11:21:53 +02003327 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003328 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3329 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3330 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003331 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003332 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003333 memcpy(chunk->area + chunk->data,
3334 "\r\nProxy-Connection: keep-alive", 30);
3335 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003337 memcpy(chunk->area + chunk->data,
3338 "\r\nConnection: keep-alive", 24);
3339 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003340 }
3341 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003342 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
3343 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003344 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003345 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003346 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003347 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02003348 req->next -= req->sov;
3349 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003350 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003351 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003352 req->msg_state = HTTP_MSG_CLOSED;
3353 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003354 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003355 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02003356 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003357 /* let the server side turn to SI_ST_CLO */
3358 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003359 } else {
3360 /* keep-alive not possible */
3361 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003362 memcpy(chunk->area + chunk->data,
3363 "\r\nProxy-Connection: close\r\n\r\n", 29);
3364 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003365 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003366 memcpy(chunk->area + chunk->data,
3367 "\r\nConnection: close\r\n\r\n", 23);
3368 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003369 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003370 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003371 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003372 }
3373
Willy Tarreaue7dff022015-04-03 01:14:29 +02003374 if (!(s->flags & SF_ERR_MASK))
3375 s->flags |= SF_ERR_LOCAL;
3376 if (!(s->flags & SF_FINST_MASK))
3377 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003378
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003379 ret = 1;
3380 leave:
3381 free_trash_chunk(chunk);
3382 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003383}
3384
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003385/* This stream analyser runs all HTTP request processing which is common to
3386 * frontends and backends, which means blocking ACLs, filters, connection-close,
3387 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003388 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003389 * either needs more data or wants to immediately abort the request (eg: deny,
3390 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003391 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003392int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003393{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003394 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003395 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003396 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003397 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003398 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003399 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003400 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003401 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003402
Willy Tarreau655dce92009-11-08 13:10:58 +01003403 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003404 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003405 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003406 }
3407
Christopher Faulet45073512018-07-20 10:16:29 +02003408 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 +02003409 now_ms, __FUNCTION__,
3410 s,
3411 req,
3412 req->rex, req->wex,
3413 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003414 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02003415 req->analysers);
3416
Willy Tarreau65410832014-04-28 21:25:43 +02003417 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003418 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003419
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003420 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003421 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003422 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003423
Willy Tarreau0b748332014-04-29 00:13:29 +02003424 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003425 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3426 goto return_prx_yield;
3427
Willy Tarreau0b748332014-04-29 00:13:29 +02003428 case HTTP_RULE_RES_CONT:
3429 case HTTP_RULE_RES_STOP: /* nothing to do */
3430 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003431
Willy Tarreau0b748332014-04-29 00:13:29 +02003432 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3433 if (txn->flags & TX_CLTARPIT)
3434 goto tarpit;
3435 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003436
Willy Tarreau0b748332014-04-29 00:13:29 +02003437 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3438 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003439
Willy Tarreau0b748332014-04-29 00:13:29 +02003440 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003441 goto done;
3442
Willy Tarreau0b748332014-04-29 00:13:29 +02003443 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3444 goto return_bad_req;
3445 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003446 }
3447
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003448 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3449 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003450 struct hdr_ctx ctx;
3451
3452 ctx.idx = 0;
3453 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02003454 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003455 if (unlikely(http_header_add_tail2(&txn->req,
3456 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02003457 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003458 goto return_bad_req;
3459 }
3460 }
3461
3462 }
3463
Willy Tarreau52542592014-04-28 18:33:26 +02003464 /* OK at this stage, we know that the request was accepted according to
3465 * the http-request rules, we can check for the stats. Note that the
3466 * URI is detected *before* the req* rules in order not to be affected
3467 * by a possible reqrep, while they are processed *after* so that a
3468 * reqdeny can still block them. This clearly needs to change in 1.6!
3469 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003470 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003471 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003472 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003473 txn->status = 500;
3474 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003475 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003476
Willy Tarreaue7dff022015-04-03 01:14:29 +02003477 if (!(s->flags & SF_ERR_MASK))
3478 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003479 goto return_prx_cond;
3480 }
3481
3482 /* parse the whole stats request and extract the relevant information */
3483 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003484 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003485 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003486
Willy Tarreau0b748332014-04-29 00:13:29 +02003487 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3488 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003489
Willy Tarreau0b748332014-04-29 00:13:29 +02003490 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3491 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003492 }
3493
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003494 /* evaluate the req* rules except reqadd */
3495 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003496 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003497 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003498
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003499 if (txn->flags & TX_CLDENY)
3500 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003501
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003502 if (txn->flags & TX_CLTARPIT) {
3503 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003504 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003505 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003506 }
Willy Tarreau06619262006-12-17 08:37:22 +01003507
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003508 /* add request headers from the rule sets in the same order */
3509 list_for_each_entry(wl, &px->req_add, list) {
3510 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003511 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003512 ret = acl_pass(ret);
3513 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3514 ret = !ret;
3515 if (!ret)
3516 continue;
3517 }
3518
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003519 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003520 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003521 }
3522
Willy Tarreau52542592014-04-28 18:33:26 +02003523
3524 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003525 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3526 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003527 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003528 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003529 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003530
Willy Tarreaue7dff022015-04-03 01:14:29 +02003531 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3532 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3533 if (!(s->flags & SF_FINST_MASK))
3534 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003535
Willy Tarreau70730dd2014-04-24 18:06:27 +02003536 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003537 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3538 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003539 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003540 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003541 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003542
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003543 /* check whether we have some ACLs set to redirect this request */
3544 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003545 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003546 int ret;
3547
Willy Tarreau192252e2015-04-04 01:47:55 +02003548 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003549 ret = acl_pass(ret);
3550 if (rule->cond->pol == ACL_COND_UNLESS)
3551 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003552 if (!ret)
3553 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003554 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003555 if (!http_apply_redirect_rule(rule, s, txn))
3556 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003557 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003558 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003559
Willy Tarreau2be39392010-01-03 17:24:51 +01003560 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3561 * If this happens, then the data will not come immediately, so we must
3562 * send all what we have without waiting. Note that due to the small gain
3563 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003564 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003565 * itself once used.
3566 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003567 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003568
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003569 done: /* done with this analyser, continue with next ones that the calling
3570 * points will have set, if any.
3571 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003572 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003573 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3574 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003575 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003576
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003577 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003578 /* Allow cookie logging
3579 */
3580 if (s->be->cookie_name || sess->fe->capture_name)
3581 manage_client_side_cookies(s, req);
3582
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003583 /* When a connection is tarpitted, we use the tarpit timeout,
3584 * which may be the same as the connect timeout if unspecified.
3585 * If unset, then set it to zero because we really want it to
3586 * eventually expire. We build the tarpit as an analyser.
3587 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003588 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003589
3590 /* wipe the request out so that we can drop the connection early
3591 * if the client closes first.
3592 */
3593 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003594
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003595 txn->status = http_err_codes[deny_status];
3596
Christopher Faulet0184ea72017-01-05 14:06:34 +01003597 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003598 req->analysers |= AN_REQ_HTTP_TARPIT;
3599 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3600 if (!req->analyse_exp)
3601 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003602 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003603 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003604 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003605 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003606 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003607 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003608 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003609
3610 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003611
3612 /* Allow cookie logging
3613 */
3614 if (s->be->cookie_name || sess->fe->capture_name)
3615 manage_client_side_cookies(s, req);
3616
Willy Tarreau0b748332014-04-29 00:13:29 +02003617 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003618 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003619 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003620 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003621 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003622 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003623 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003624 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003625 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003626 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003627 goto return_prx_cond;
3628
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003629 return_bad_req:
3630 /* We centralize bad requests processing here */
3631 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3632 /* we detected a parsing error. We want to archive this request
3633 * in the dedicated proxy area for later troubleshooting.
3634 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003635 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003636 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003637
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003638 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 txn->req.msg_state = HTTP_MSG_ERROR;
3640 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003641 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003642
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003643 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003644 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003645 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003646
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003647 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003648 if (!(s->flags & SF_ERR_MASK))
3649 s->flags |= SF_ERR_PRXCOND;
3650 if (!(s->flags & SF_FINST_MASK))
3651 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003652
Christopher Faulet0184ea72017-01-05 14:06:34 +01003653 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003654 req->analyse_exp = TICK_ETERNITY;
3655 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003656
3657 return_prx_yield:
3658 channel_dont_connect(req);
3659 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003660}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003661
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003662/* This function performs all the processing enabled for the current request.
3663 * It returns 1 if the processing can continue on next analysers, or zero if it
3664 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003665 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003666 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003667int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003668{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003669 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003670 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003671 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003672 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003673
Willy Tarreau655dce92009-11-08 13:10:58 +01003674 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003675 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003676 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003677 return 0;
3678 }
3679
Christopher Faulet45073512018-07-20 10:16:29 +02003680 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 +02003681 now_ms, __FUNCTION__,
3682 s,
3683 req,
3684 req->rex, req->wex,
3685 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003686 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003687 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003688
Willy Tarreau59234e92008-11-30 23:51:27 +01003689 /*
3690 * Right now, we know that we have processed the entire headers
3691 * and that unwanted requests have been filtered out. We can do
3692 * whatever we want with the remaining request. Also, now we
3693 * may have separate values for ->fe, ->be.
3694 */
Willy Tarreau06619262006-12-17 08:37:22 +01003695
Willy Tarreau59234e92008-11-30 23:51:27 +01003696 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003697 * If HTTP PROXY is set we simply get remote server address parsing
3698 * incoming request. Note that this requires that a connection is
3699 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003700 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003701 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003702 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003703 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003704
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003705 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003706 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003707 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003708 txn->req.msg_state = HTTP_MSG_ERROR;
3709 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003710 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003711 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003712
Willy Tarreaue7dff022015-04-03 01:14:29 +02003713 if (!(s->flags & SF_ERR_MASK))
3714 s->flags |= SF_ERR_RESOURCE;
3715 if (!(s->flags & SF_FINST_MASK))
3716 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003717
3718 return 0;
3719 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003720
3721 path = http_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003722 if (url2sa(ci_head(req) + msg->sl.rq.u,
3723 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003724 &conn->addr.to, NULL) == -1)
3725 goto return_bad_req;
3726
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003727 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003728 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3729 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003730 * u_l characters by a single "/".
3731 */
3732 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003733 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003734 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3735 int delta;
3736
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003737 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003738 http_msg_move_end(&txn->req, delta);
3739 cur_end += delta;
3740 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3741 goto return_bad_req;
3742 }
3743 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003744 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003745 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3746 int delta;
3747
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003748 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003749 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003750 http_msg_move_end(&txn->req, delta);
3751 cur_end += delta;
3752 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3753 goto return_bad_req;
3754 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003755 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003756
Willy Tarreau59234e92008-11-30 23:51:27 +01003757 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003758 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003759 * Note that doing so might move headers in the request, but
3760 * the fields will stay coherent and the URI will not move.
3761 * This should only be performed in the backend.
3762 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003763 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003764 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003765
William Lallemanda73203e2012-03-12 12:48:57 +01003766 /* add unique-id if "header-unique-id" is specified */
3767
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003768 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003769 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003770 goto return_bad_req;
3771 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003772 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003773 }
William Lallemanda73203e2012-03-12 12:48:57 +01003774
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003775 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003776 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003777 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003778 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003779 goto return_bad_req;
3780 }
3781
Cyril Bontéb21570a2009-11-29 20:04:48 +01003782 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003783 * 9: add X-Forwarded-For if either the frontend or the backend
3784 * asks for it.
3785 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003786 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003787 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003788 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3789 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3790 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003791 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003792 /* The header is set to be added only if none is present
3793 * and we found it, so don't do anything.
3794 */
3795 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003796 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003797 /* Add an X-Forwarded-For header unless the source IP is
3798 * in the 'except' network range.
3799 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003800 if ((!sess->fe->except_mask.s_addr ||
3801 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3802 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003804 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003806 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003807 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003808 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003809
3810 /* Note: we rely on the backend to get the header name to be used for
3811 * x-forwarded-for, because the header is really meant for the backends.
3812 * However, if the backend did not specify any option, we have to rely
3813 * on the frontend's header name.
3814 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003815 if (s->be->fwdfor_hdr_len) {
3816 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003817 memcpy(trash.area,
3818 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003819 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003820 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003821 memcpy(trash.area,
3822 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003823 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003824 len += snprintf(trash.area + len,
3825 trash.size - len,
3826 ": %d.%d.%d.%d", pn[0], pn[1],
3827 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003828
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003829 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003830 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003831 }
3832 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003833 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003834 /* FIXME: for the sake of completeness, we should also support
3835 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003836 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003837 int len;
3838 char pn[INET6_ADDRSTRLEN];
3839 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003840 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003841 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003842
Willy Tarreau59234e92008-11-30 23:51:27 +01003843 /* Note: we rely on the backend to get the header name to be used for
3844 * x-forwarded-for, because the header is really meant for the backends.
3845 * However, if the backend did not specify any option, we have to rely
3846 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003847 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003848 if (s->be->fwdfor_hdr_len) {
3849 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003850 memcpy(trash.area, s->be->fwdfor_hdr_name,
3851 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003852 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003853 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003854 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3855 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003856 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003857 len += snprintf(trash.area + len, trash.size - len,
3858 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003859
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003860 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003861 goto return_bad_req;
3862 }
3863 }
3864
3865 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003866 * 10: add X-Original-To if either the frontend or the backend
3867 * asks for it.
3868 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003869 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003870
3871 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003872 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003873 /* Add an X-Original-To header unless the destination IP is
3874 * in the 'except' network range.
3875 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003876 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003877
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003878 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003879 ((!sess->fe->except_mask_to.s_addr ||
3880 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3881 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003882 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003883 (((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 +02003884 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003885 int len;
3886 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003887 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003888
3889 /* Note: we rely on the backend to get the header name to be used for
3890 * x-original-to, because the header is really meant for the backends.
3891 * However, if the backend did not specify any option, we have to rely
3892 * on the frontend's header name.
3893 */
3894 if (s->be->orgto_hdr_len) {
3895 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003896 memcpy(trash.area,
3897 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003898 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003899 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 memcpy(trash.area,
3901 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003902 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003903 len += snprintf(trash.area + len,
3904 trash.size - len,
3905 ": %d.%d.%d.%d", pn[0], pn[1],
3906 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003907
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003908 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003909 goto return_bad_req;
3910 }
3911 }
3912 }
3913
Willy Tarreau50fc7772012-11-11 22:19:57 +01003914 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3915 * If an "Upgrade" token is found, the header is left untouched in order not to have
3916 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3917 * "Upgrade" is present in the Connection header.
3918 */
3919 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3920 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003921 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003922 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003923 unsigned int want_flags = 0;
3924
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003925 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003926 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003927 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003928 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003929 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003930 want_flags |= TX_CON_CLO_SET;
3931 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003932 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003933 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003934 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003935 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003936 want_flags |= TX_CON_KAL_SET;
3937 }
3938
3939 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003940 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003941 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003942
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003943
Willy Tarreau522d6c02009-12-06 18:49:18 +01003944 /* If we have no server assigned yet and we're balancing on url_param
3945 * with a POST request, we may be interested in checking the body for
3946 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003947 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003948 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003949 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003950 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003951 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003952 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003953 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003954
Christopher Fauletbe821b92017-03-30 11:21:53 +02003955 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3956 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003957#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003958 /* We expect some data from the client. Unless we know for sure
3959 * we already have a full request, we have to re-enable quick-ack
3960 * in case we previously disabled it, otherwise we might cause
3961 * the client to delay further data.
3962 */
3963 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3964 cli_conn && conn_ctrl_ready(cli_conn) &&
3965 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003966 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003967 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003968#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003969
Willy Tarreau59234e92008-11-30 23:51:27 +01003970 /*************************************************************
3971 * OK, that's finished for the headers. We have done what we *
3972 * could. Let's switch to the DATA state. *
3973 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003974 req->analyse_exp = TICK_ETERNITY;
3975 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003976
Willy Tarreau59234e92008-11-30 23:51:27 +01003977 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003978 /* OK let's go on with the BODY now */
3979 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003980
Willy Tarreau59234e92008-11-30 23:51:27 +01003981 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003982 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003983 /* we detected a parsing error. We want to archive this request
3984 * in the dedicated proxy area for later troubleshooting.
3985 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003986 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003987 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003988
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003989 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003990 txn->req.msg_state = HTTP_MSG_ERROR;
3991 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003992 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003993 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003994
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003995 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003996 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003997 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003998
Willy Tarreaue7dff022015-04-03 01:14:29 +02003999 if (!(s->flags & SF_ERR_MASK))
4000 s->flags |= SF_ERR_PRXCOND;
4001 if (!(s->flags & SF_FINST_MASK))
4002 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004003 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004004}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004005
Willy Tarreau60b85b02008-11-30 23:28:40 +01004006/* This function is an analyser which processes the HTTP tarpit. It always
4007 * returns zero, at the beginning because it prevents any other processing
4008 * from occurring, and at the end because it terminates the request.
4009 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004010int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004011{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004012 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004013
4014 /* This connection is being tarpitted. The CLIENT side has
4015 * already set the connect expiration date to the right
4016 * timeout. We just have to check that the client is still
4017 * there and that the timeout has not expired.
4018 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004019 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004020 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004021 !tick_is_expired(req->analyse_exp, now_ms))
4022 return 0;
4023
4024 /* We will set the queue timer to the time spent, just for
4025 * logging purposes. We fake a 500 server error, so that the
4026 * attacker will not suspect his connection has been tarpitted.
4027 * It will not cause trouble to the logs because we can exclude
4028 * the tarpitted connections by filtering on the 'PT' status flags.
4029 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004030 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4031
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004032 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004033 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004034
Christopher Faulet0184ea72017-01-05 14:06:34 +01004035 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004036 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004037
Willy Tarreaue7dff022015-04-03 01:14:29 +02004038 if (!(s->flags & SF_ERR_MASK))
4039 s->flags |= SF_ERR_PRXCOND;
4040 if (!(s->flags & SF_FINST_MASK))
4041 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004042 return 0;
4043}
4044
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004045/* This function is an analyser which waits for the HTTP request body. It waits
4046 * for either the buffer to be full, or the full advertised contents to have
4047 * reached the buffer. It must only be called after the standard HTTP request
4048 * processing has occurred, because it expects the request to be parsed and will
4049 * look for the Expect header. It may send a 100-Continue interim response. It
4050 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4051 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4052 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004053 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004054int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004055{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004056 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004057 struct http_txn *txn = s->txn;
4058 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004059
4060 /* We have to parse the HTTP request body to find any required data.
4061 * "balance url_param check_post" should have been the only way to get
4062 * into this. We were brought here after HTTP header analysis, so all
4063 * related structures are ready.
4064 */
4065
Willy Tarreau890988f2014-04-10 11:59:33 +02004066 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4067 /* This is the first call */
4068 if (msg->msg_state < HTTP_MSG_BODY)
4069 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004070
Willy Tarreau890988f2014-04-10 11:59:33 +02004071 if (msg->msg_state < HTTP_MSG_100_SENT) {
4072 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4073 * send an HTTP/1.1 100 Continue intermediate response.
4074 */
4075 if (msg->flags & HTTP_MSGF_VER_11) {
4076 struct hdr_ctx ctx;
4077 ctx.idx = 0;
4078 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004079 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02004080 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004081 co_inject(&s->res,
4082 http_100_chunk.area,
4083 http_100_chunk.data);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004084 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004085 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004086 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004087 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004088 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004089
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004090 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02004091 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02004092 * must save the body in msg->next because it survives buffer
4093 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004094 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004095 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004096
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004097 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004098 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4099 else
4100 msg->msg_state = HTTP_MSG_DATA;
4101 }
4102
Willy Tarreau890988f2014-04-10 11:59:33 +02004103 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4104 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004105 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004106 goto missing_data;
4107
4108 /* OK we have everything we need now */
4109 goto http_end;
4110 }
4111
4112 /* OK here we're parsing a chunked-encoded message */
4113
Willy Tarreau522d6c02009-12-06 18:49:18 +01004114 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004115 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004116 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004117 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004118 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004119 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004120 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004121
Willy Tarreau115acb92009-12-26 13:56:06 +01004122 if (!ret)
4123 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004124 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004125 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004126 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004127 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004128 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004129 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004130 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004131
4132 msg->chunk_len = chunk;
4133 msg->body_len += chunk;
4134
4135 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004136 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004137 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004138 }
4139
Willy Tarreaud98cf932009-12-27 22:54:55 +01004140 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004141 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4142 * for at least a whole chunk or the whole content length bytes after
4143 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004144 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004145 if (msg->msg_state == HTTP_MSG_TRAILERS)
4146 goto http_end;
4147
Willy Tarreaue115b492015-05-01 23:05:14 +02004148 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004149 goto http_end;
4150
4151 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004152 /* we get here if we need to wait for more data. If the buffer is full,
4153 * we have the maximum we can expect.
4154 */
Willy Tarreau23752332018-06-15 14:54:53 +02004155 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02004156 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004157
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004158 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004159 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004160 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004161
Willy Tarreaue7dff022015-04-03 01:14:29 +02004162 if (!(s->flags & SF_ERR_MASK))
4163 s->flags |= SF_ERR_CLITO;
4164 if (!(s->flags & SF_FINST_MASK))
4165 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004166 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004167 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004168
4169 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004170 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004171 /* Not enough data. We'll re-use the http-request
4172 * timeout here. Ideally, we should set the timeout
4173 * relative to the accept() date. We just set the
4174 * request timeout once at the beginning of the
4175 * request.
4176 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004177 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004178 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004179 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004180 return 0;
4181 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004182
4183 http_end:
4184 /* The situation will not evolve, so let's give up on the analysis. */
4185 s->logs.tv_request = now; /* update the request timer to reflect full request */
4186 req->analysers &= ~an_bit;
4187 req->analyse_exp = TICK_ETERNITY;
4188 return 1;
4189
4190 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004191 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004192 txn->req.msg_state = HTTP_MSG_ERROR;
4193 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004194 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004195
Willy Tarreaue7dff022015-04-03 01:14:29 +02004196 if (!(s->flags & SF_ERR_MASK))
4197 s->flags |= SF_ERR_PRXCOND;
4198 if (!(s->flags & SF_FINST_MASK))
4199 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004200
Willy Tarreau522d6c02009-12-06 18:49:18 +01004201 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004202 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004203 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004204 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004205 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004206 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004207}
4208
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004209/* send a server's name with an outgoing request over an established connection.
4210 * Note: this function is designed to be called once the request has been scheduled
4211 * for being forwarded. This is the reason why it rewinds the buffer before
4212 * proceeding.
4213 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004214int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004215
4216 struct hdr_ctx ctx;
4217
Mark Lamourinec2247f02012-01-04 13:02:01 -05004218 char *hdr_name = be->server_id_hdr_name;
4219 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004220 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004221 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004222 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004223
William Lallemandd9e90662012-01-30 17:27:17 +01004224 ctx.idx = 0;
4225
Willy Tarreau211cdec2014-04-17 20:18:08 +02004226 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004227 if (old_o) {
4228 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004229 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004230 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004231 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004232 }
4233
Willy Tarreauf37954d2018-06-15 18:31:02 +02004234 old_i = ci_data(chn);
4235 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 -05004236 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004237 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004238 }
4239
4240 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004241 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004242 memcpy(hdr_val, hdr_name, hdr_name_len);
4243 hdr_val += hdr_name_len;
4244 *hdr_val++ = ':';
4245 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004246 hdr_val += strlcpy2(hdr_val, srv_name,
4247 trash.area + trash.size - hdr_val);
4248 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
4249 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004250
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004251 if (old_o) {
4252 /* If this was a forwarded request, we must readjust the amount of
4253 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004254 * variations. Note that the current state is >= HTTP_MSG_BODY,
4255 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004256 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004257 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02004258 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004259 txn->req.next -= old_o;
4260 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004261 }
4262
Mark Lamourinec2247f02012-01-04 13:02:01 -05004263 return 0;
4264}
4265
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266/* Terminate current transaction and prepare a new one. This is very tricky
4267 * right now but it works.
4268 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004269void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004271 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004272 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004273 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004274 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004275 struct connection *srv_conn;
4276 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004277 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004278
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279 /* FIXME: We need a more portable way of releasing a backend's and a
4280 * server's connections. We need a safer way to reinitialize buffer
4281 * flags. We also need a more accurate method for computing per-request
4282 * data.
4283 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004284 /*
4285 * XXX cognet: This is probably wrong, this is killing a whole
4286 * connection, in the new world order, we probably want to just kill
4287 * the stream, this is to be revisited the day we handle multiple
4288 * streams in one server connection.
4289 */
4290 cs = objt_cs(s->si[1].end);
4291 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292
Willy Tarreau4213a112013-12-15 10:25:42 +01004293 /* unless we're doing keep-alive, we want to quickly close the connection
4294 * to the server.
4295 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004296 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004297 !si_conn_ready(&s->si[1])) {
4298 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4299 si_shutr(&s->si[1]);
4300 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004301 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302
Willy Tarreaue7dff022015-04-03 01:14:29 +02004303 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004304 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004305 if (unlikely(s->srv_conn))
4306 sess_change_server(s, NULL);
4307 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308
4309 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004310 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004311
Willy Tarreaueee5b512015-04-03 23:46:31 +02004312 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313 int n;
4314
Willy Tarreaueee5b512015-04-03 23:46:31 +02004315 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004316 if (n < 1 || n > 5)
4317 n = 0;
4318
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004319 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004320 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004321 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004322 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004323 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004324 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4325 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004326 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327 }
4328
4329 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004330 s->logs.bytes_in -= ci_data(&s->req);
4331 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332
Willy Tarreau66425e32018-07-25 06:55:12 +02004333 /* we may need to know the position in the queue */
4334 pendconn_free(s);
4335
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004337 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004338 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004339 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004340 s->do_log(s);
4341 }
4342
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004343 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004344 stream_stop_content_counters(s);
4345 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004346
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347 s->logs.accept_date = date; /* user-visible date for logging */
4348 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004349 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4350 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004351 tv_zero(&s->logs.tv_request);
4352 s->logs.t_queue = -1;
4353 s->logs.t_connect = -1;
4354 s->logs.t_data = -1;
4355 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04004356 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
4357 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358
Willy Tarreauf37954d2018-06-15 18:31:02 +02004359 s->logs.bytes_in = s->req.total = ci_data(&s->req);
4360 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004362 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004363 if (s->flags & SF_CURR_SESS) {
4364 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004365 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004367 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004368 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004369 }
4370
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004371 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004372
Willy Tarreau4213a112013-12-15 10:25:42 +01004373 /* only release our endpoint if we don't intend to reuse the
4374 * connection.
4375 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004376 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004377 !si_conn_ready(&s->si[1])) {
4378 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004379 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004380 }
4381
Willy Tarreau350f4872014-11-28 14:42:25 +01004382 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4383 s->si[1].err_type = SI_ET_NONE;
4384 s->si[1].conn_retries = 0; /* used for logging too */
4385 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004386 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 +01004387 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 +01004388 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 +02004389 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4390 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4391 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004392
Patrick Hemmere3faf022018-08-22 10:02:00 -04004393 hlua_ctx_destroy(s->hlua);
4394 s->hlua = NULL;
4395
Willy Tarreaueee5b512015-04-03 23:46:31 +02004396 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004398 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004399
4400 if (prev_status == 401 || prev_status == 407) {
4401 /* In HTTP keep-alive mode, if we receive a 401, we still have
4402 * a chance of being able to send the visitor again to the same
4403 * server over the same connection. This is required by some
4404 * broken protocols such as NTLM, and anyway whenever there is
4405 * an opportunity for sending the challenge to the proper place,
4406 * it's better to do it (at least it helps with debugging).
4407 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004408 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004409 if (srv_conn)
4410 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004411 }
4412
Willy Tarreau53f96852016-02-02 18:50:47 +01004413 /* Never ever allow to reuse a connection from a non-reuse backend */
4414 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4415 srv_conn->flags |= CO_FL_PRIVATE;
4416
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004417 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004418 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004419
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004420 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004421 s->req.flags |= CF_NEVER_WAIT;
4422 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004423 }
4424
Willy Tarreau714ea782015-11-25 20:11:11 +01004425 /* we're removing the analysers, we MUST re-enable events detection.
4426 * We don't enable close on the response channel since it's either
4427 * already closed, or in keep-alive with an idle connection handler.
4428 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004429 channel_auto_read(&s->req);
4430 channel_auto_close(&s->req);
4431 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004432
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004433 /* we're in keep-alive with an idle connection, monitor it if not already done */
4434 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004435 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004436 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004437 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004438 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004439 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004440 else if (prev_flags & TX_NOT_FIRST)
4441 /* note: we check the request, not the connection, but
4442 * this is valid for strategies SAFE and AGGR, and in
4443 * case of ALWS, we don't care anyway.
4444 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004445 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004446 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004447 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004448 }
Christopher Faulete6006242017-03-10 11:52:44 +01004449 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4450 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451}
4452
4453
4454/* This function updates the request state machine according to the response
4455 * state machine and buffer flags. It returns 1 if it changes anything (flag
4456 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4457 * it is only used to find when a request/response couple is complete. Both
4458 * this function and its equivalent should loop until both return zero. It
4459 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4460 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004461int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004463 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004464 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004465 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004466 unsigned int old_state = txn->req.msg_state;
4467
Christopher Faulet4be98032017-07-18 10:48:24 +02004468 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004469 return 0;
4470
4471 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004472 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004473 * We can shut the read side unless we want to abort_on_close,
4474 * or we have a POST request. The issue with POST requests is
4475 * that some browsers still send a CRLF after the request, and
4476 * this CRLF must be read so that it does not remain in the kernel
4477 * buffers, otherwise a close could cause an RST on some systems
4478 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004479 * Note that if we're using keep-alive on the client side, we'd
4480 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004481 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004482 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004483 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004484 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4485 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004486 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4487 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004488 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004489 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490
Willy Tarreau40f151a2012-12-20 12:10:09 +01004491 /* if the server closes the connection, we want to immediately react
4492 * and close the socket to save packets and syscalls.
4493 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004494 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004495
Willy Tarreau7f876a12015-11-18 11:59:55 +01004496 /* In any case we've finished parsing the request so we must
4497 * disable Nagle when sending data because 1) we're not going
4498 * to shut this side, and 2) the server is waiting for us to
4499 * send pending data.
4500 */
4501 chn->flags |= CF_NEVER_WAIT;
4502
Willy Tarreau610ecce2010-01-04 21:15:02 +01004503 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4504 goto wait_other_side;
4505
4506 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4507 /* The server has not finished to respond, so we
4508 * don't want to move in order not to upset it.
4509 */
4510 goto wait_other_side;
4511 }
4512
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513 /* When we get here, it means that both the request and the
4514 * response have finished receiving. Depending on the connection
4515 * mode, we'll have to wait for the last bytes to leave in either
4516 * direction, and sometimes for a close to be effective.
4517 */
4518
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004519 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4520 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004521 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4522 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004523 }
4524 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4525 /* Option forceclose is set, or either side wants to close,
4526 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004527 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004528 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004529 *
4530 * However, there is an exception if the response
4531 * length is undefined. In this case, we need to wait
4532 * the close from the server. The response will be
4533 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004534 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004535 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4536 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4537 goto check_channel_flags;
4538
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004539 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4540 channel_shutr_now(chn);
4541 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004543 }
4544 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004545 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4546 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004547 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004548 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4549 channel_auto_read(chn);
4550 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004551 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004552 }
4553
Christopher Faulet4be98032017-07-18 10:48:24 +02004554 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004555 }
4556
4557 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4558 http_msg_closing:
4559 /* nothing else to forward, just waiting for the output buffer
4560 * to be empty and for the shutw_now to take effect.
4561 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004562 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 txn->req.msg_state = HTTP_MSG_CLOSED;
4564 goto http_msg_closed;
4565 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004566 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004567 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004569 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004570 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 }
4572
4573 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4574 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004575 /* if we don't know whether the server will close, we need to hard close */
4576 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4577 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4578
Willy Tarreau3988d932013-12-27 23:03:08 +01004579 /* see above in MSG_DONE why we only do this in these states */
4580 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4581 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004582 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4583 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004584 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004585 goto wait_other_side;
4586 }
4587
Christopher Faulet4be98032017-07-18 10:48:24 +02004588 check_channel_flags:
4589 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4590 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4591 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004592 txn->req.msg_state = HTTP_MSG_CLOSING;
4593 goto http_msg_closing;
4594 }
4595
4596
Willy Tarreau610ecce2010-01-04 21:15:02 +01004597 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004598 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004599}
4600
4601
4602/* This function updates the response state machine according to the request
4603 * state machine and buffer flags. It returns 1 if it changes anything (flag
4604 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4605 * it is only used to find when a request/response couple is complete. Both
4606 * this function and its equivalent should loop until both return zero. It
4607 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4608 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004609int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004610{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004611 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004612 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004613 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004614 unsigned int old_state = txn->rsp.msg_state;
4615
Christopher Faulet4be98032017-07-18 10:48:24 +02004616 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617 return 0;
4618
4619 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4620 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004621 * still monitor the server connection for a possible close
4622 * while the request is being uploaded, so we don't disable
4623 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004624 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004625 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626
4627 if (txn->req.msg_state == HTTP_MSG_ERROR)
4628 goto wait_other_side;
4629
4630 if (txn->req.msg_state < HTTP_MSG_DONE) {
4631 /* The client seems to still be sending data, probably
4632 * because we got an error response during an upload.
4633 * We have the choice of either breaking the connection
4634 * or letting it pass through. Let's do the later.
4635 */
4636 goto wait_other_side;
4637 }
4638
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 /* When we get here, it means that both the request and the
4640 * response have finished receiving. Depending on the connection
4641 * mode, we'll have to wait for the last bytes to leave in either
4642 * direction, and sometimes for a close to be effective.
4643 */
4644
4645 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4646 /* Server-close mode : shut read and wait for the request
4647 * side to close its output buffer. The caller will detect
4648 * when we're in DONE and the other is in CLOSED and will
4649 * catch that for the final cleanup.
4650 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004651 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4652 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004653 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004654 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4655 /* Option forceclose is set, or either side wants to close,
4656 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004657 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004658 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004660 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004661 channel_shutr_now(chn);
4662 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004663 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664 }
4665 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004666 /* The last possible modes are keep-alive and tunnel. Tunnel will
4667 * need to forward remaining data. Keep-alive will need to monitor
4668 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004670 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004671 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004672 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4673 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674 }
4675
Christopher Faulet4be98032017-07-18 10:48:24 +02004676 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004677 }
4678
4679 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4680 http_msg_closing:
4681 /* nothing else to forward, just waiting for the output buffer
4682 * to be empty and for the shutw_now to take effect.
4683 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004684 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4686 goto http_msg_closed;
4687 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004688 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004689 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004690 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004691 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004692 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004693 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004694 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004695 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004696 }
4697
4698 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4699 http_msg_closed:
4700 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004701 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004702 channel_auto_close(chn);
4703 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704 goto wait_other_side;
4705 }
4706
Christopher Faulet4be98032017-07-18 10:48:24 +02004707 check_channel_flags:
4708 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4709 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4710 /* if we've just closed an output, let's switch */
4711 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4712 goto http_msg_closing;
4713 }
4714
Willy Tarreau610ecce2010-01-04 21:15:02 +01004715 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004716 /* We force the response to leave immediately if we're waiting for the
4717 * other side, since there is no pending shutdown to push it out.
4718 */
4719 if (!channel_is_empty(chn))
4720 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004721 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004722}
4723
4724
Christopher Faulet894da4c2017-07-18 11:29:07 +02004725/* Resync the request and response state machines. */
4726void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004727{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004728 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004729#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 int old_req_state = txn->req.msg_state;
4731 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004732#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004733
Willy Tarreau610ecce2010-01-04 21:15:02 +01004734 http_sync_req_state(s);
4735 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004736 if (!http_sync_res_state(s))
4737 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738 if (!http_sync_req_state(s))
4739 break;
4740 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004741
Christopher Faulet894da4c2017-07-18 11:29:07 +02004742 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4743 "req->analysers=0x%08x res->analysers=0x%08x\n",
4744 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004745 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4746 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004747 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004748
4749
Willy Tarreau610ecce2010-01-04 21:15:02 +01004750 /* OK, both state machines agree on a compatible state.
4751 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004752 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4753 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004754 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4755 * means we must abort the request.
4756 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4757 * corresponding channel.
4758 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4759 * on the response with server-close mode means we've completed one
4760 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004761 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004762 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4763 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004764 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004765 channel_auto_close(&s->req);
4766 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004767 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004768 channel_auto_close(&s->res);
4769 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004771 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4772 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004773 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004774 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004775 channel_auto_close(&s->res);
4776 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004777 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004778 channel_abort(&s->req);
4779 channel_auto_close(&s->req);
4780 channel_auto_read(&s->req);
4781 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004783 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4784 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4785 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4786 s->req.analysers &= AN_REQ_FLT_END;
4787 if (HAS_REQ_DATA_FILTERS(s))
4788 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4789 }
4790 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4791 s->res.analysers &= AN_RES_FLT_END;
4792 if (HAS_RSP_DATA_FILTERS(s))
4793 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4794 }
4795 channel_auto_close(&s->req);
4796 channel_auto_read(&s->req);
4797 channel_auto_close(&s->res);
4798 channel_auto_read(&s->res);
4799 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004800 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4801 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004803 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4804 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4805 /* server-close/keep-alive: terminate this transaction,
4806 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004807 * a fresh-new transaction, but only once we're sure there's
4808 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004809 * another request. They must not hold any pending output data
4810 * and the response buffer must realigned
4811 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004813 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004814 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004815 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004816 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004817 else {
4818 s->req.analysers = AN_REQ_FLT_END;
4819 s->res.analysers = AN_RES_FLT_END;
4820 txn->flags |= TX_WAIT_CLEANUP;
4821 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004822 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823}
4824
Willy Tarreaud98cf932009-12-27 22:54:55 +01004825/* This function is an analyser which forwards request body (including chunk
4826 * sizes if any). It is called as soon as we must forward, even if we forward
4827 * zero byte. The only situation where it must not be called is when we're in
4828 * tunnel mode and we want to forward till the close. It's used both to forward
4829 * remaining data and to resync after end of body. It expects the msg_state to
4830 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004831 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004832 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004833 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004834 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004835int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004836{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004837 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004838 struct http_txn *txn = s->txn;
4839 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004840 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004841
Christopher Faulet45073512018-07-20 10:16:29 +02004842 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 +02004843 now_ms, __FUNCTION__,
4844 s,
4845 req,
4846 req->rex, req->wex,
4847 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004848 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004849 req->analysers);
4850
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004851 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4852 return 0;
4853
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004854 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004855 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004856 /* Output closed while we were sending data. We must abort and
4857 * wake the other side up.
4858 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004859 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004860 msg->msg_state = HTTP_MSG_ERROR;
4861 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004862 return 1;
4863 }
4864
Willy Tarreaud98cf932009-12-27 22:54:55 +01004865 /* Note that we don't have to send 100-continue back because we don't
4866 * need the data to complete our job, and it's up to the server to
4867 * decide whether to return 100, 417 or anything else in return of
4868 * an "Expect: 100-continue" header.
4869 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004870 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004871 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4872 ? HTTP_MSG_CHUNK_SIZE
4873 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004874
4875 /* TODO/filters: when http-buffer-request option is set or if a
4876 * rule on url_param exists, the first chunk size could be
4877 * already parsed. In that case, msg->next is after the chunk
4878 * size (including the CRLF after the size). So this case should
4879 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004880 }
4881
Willy Tarreau7ba23542014-04-17 21:50:00 +02004882 /* Some post-connect processing might want us to refrain from starting to
4883 * forward data. Currently, the only reason for this is "balance url_param"
4884 * whichs need to parse/process the request after we've enabled forwarding.
4885 */
4886 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004887 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004888 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004889 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004890 channel_dont_close(req); /* don't fail on early shutr */
4891 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004892 }
4893 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4894 }
4895
Willy Tarreau80a92c02014-03-12 10:41:13 +01004896 /* in most states, we should abort in case of early close */
4897 channel_auto_close(req);
4898
Willy Tarreauefdf0942014-04-24 20:08:57 +02004899 if (req->to_forward) {
4900 /* We can't process the buffer's contents yet */
4901 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004902 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004903 }
4904
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004905 if (msg->msg_state < HTTP_MSG_DONE) {
4906 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4907 ? http_msg_forward_chunked_body(s, msg)
4908 : http_msg_forward_body(s, msg));
4909 if (!ret)
4910 goto missing_data_or_waiting;
4911 if (ret < 0)
4912 goto return_bad_req;
4913 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004914
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004915 /* other states, DONE...TUNNEL */
4916 /* we don't want to forward closes on DONE except in tunnel mode. */
4917 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4918 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004919
Christopher Faulet894da4c2017-07-18 11:29:07 +02004920 http_resync_states(s);
4921 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004922 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4923 if (req->flags & CF_SHUTW) {
4924 /* request errors are most likely due to the
4925 * server aborting the transfer. */
4926 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004927 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004928 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004929 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004930 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004931 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004932 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004933 }
4934
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004935 /* If "option abortonclose" is set on the backend, we want to monitor
4936 * the client's connection and forward any shutdown notification to the
4937 * server, which will decide whether to close or to go on processing the
4938 * request. We only do that in tunnel mode, and not in other modes since
4939 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004940 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004941 channel_auto_read(req);
4942 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4943 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4944 s->si[1].flags |= SI_FL_NOLINGER;
4945 channel_auto_close(req);
4946 }
4947 else if (s->txn->meth == HTTP_METH_POST) {
4948 /* POST requests may require to read extra CRLF sent by broken
4949 * browsers and which could cause an RST to be sent upon close
4950 * on some systems (eg: Linux). */
4951 channel_auto_read(req);
4952 }
4953 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004954
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004955 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004956 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004957 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004958 if (!(s->flags & SF_ERR_MASK))
4959 s->flags |= SF_ERR_CLICL;
4960 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004961 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004962 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004964 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004965 }
4966
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004967 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4968 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004969 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004970 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004971
4972 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004973 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004974
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004975 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004976 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004977 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004978 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004979
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004980 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004981 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004982 * And when content-length is used, we never want to let the possible
4983 * shutdown be forwarded to the other side, as the state machine will
4984 * take care of it once the client responds. It's also important to
4985 * prevent TIME_WAITs from accumulating on the backend side, and for
4986 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004987 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004988 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004989 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004990
Willy Tarreau5c620922011-05-11 19:56:11 +02004991 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004992 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004993 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004994 * modes are already handled by the stream sock layer. We must not do
4995 * this in content-length mode because it could present the MSG_MORE
4996 * flag with the last block of forwarded data, which would cause an
4997 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004998 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004999 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005000 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005001
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005002 return 0;
5003
Willy Tarreaud98cf932009-12-27 22:54:55 +01005004 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005005 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005006 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005007 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02005008
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005009 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005010 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005011 txn->req.msg_state = HTTP_MSG_ERROR;
5012 if (txn->status) {
5013 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005014 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005015 } else {
5016 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005017 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005018 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005019 req->analysers &= AN_REQ_FLT_END;
5020 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 +01005021
Willy Tarreaue7dff022015-04-03 01:14:29 +02005022 if (!(s->flags & SF_ERR_MASK))
5023 s->flags |= SF_ERR_PRXCOND;
5024 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005025 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005026 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005027 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005028 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005029 }
5030 return 0;
5031
5032 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005033 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005034 txn->req.msg_state = HTTP_MSG_ERROR;
5035 if (txn->status) {
5036 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005037 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005038 } else {
5039 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005040 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005041 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005042 req->analysers &= AN_REQ_FLT_END;
5043 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 +01005044
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005045 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5046 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005047 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005048 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005049
Willy Tarreaue7dff022015-04-03 01:14:29 +02005050 if (!(s->flags & SF_ERR_MASK))
5051 s->flags |= SF_ERR_SRVCL;
5052 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005053 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005054 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005055 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005056 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005057 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005058 return 0;
5059}
5060
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005061/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5062 * processing can continue on next analysers, or zero if it either needs more
5063 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005064 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005065 * when it has nothing left to do, and may remove any analyser when it wants to
5066 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005067 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005068int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005069{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005070 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005071 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005073 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005074 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005075 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005076 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005077
Christopher Faulet45073512018-07-20 10:16:29 +02005078 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 +02005079 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005080 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005081 rep,
5082 rep->rex, rep->wex,
5083 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005084 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005085 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005086
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005087 /*
5088 * Now parse the partial (or complete) lines.
5089 * We will check the response syntax, and also join multi-line
5090 * headers. An index of all the lines will be elaborated while
5091 * parsing.
5092 *
5093 * For the parsing, we use a 28 states FSM.
5094 *
5095 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02005096 * ci_head(rep) = beginning of response
5097 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
5098 * ci_tail(rep) = end of input data
5099 * msg->eol = end of current header or line (LF or CRLF)
5100 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005101 */
5102
Willy Tarreau628c40c2014-04-24 19:11:26 +02005103 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005104 /* There's a protected area at the end of the buffer for rewriting
5105 * purposes. We don't want to start to parse the request if the
5106 * protected area is affected, because we may have to move processed
5107 * data later, which is much more complicated.
5108 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02005109 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005110 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005111 /* some data has still not left the buffer, wake us once that's done */
5112 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5113 goto abort_response;
5114 channel_dont_close(rep);
5115 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005116 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005117 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005118 }
5119
Willy Tarreau188e2302018-06-15 11:11:53 +02005120 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005121 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005122 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02005123
Willy Tarreauf37954d2018-06-15 18:31:02 +02005124 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01005125 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005126 }
5127
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005128 /* 1: we might have to print this header in debug mode */
5129 if (unlikely((global.mode & MODE_DEBUG) &&
5130 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005131 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005133
Willy Tarreauf37954d2018-06-15 18:31:02 +02005134 sol = ci_head(rep);
5135 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005136 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005137
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138 sol += hdr_idx_first_pos(&txn->hdr_idx);
5139 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 while (cur_idx) {
5142 eol = sol + txn->hdr_idx.v[cur_idx].len;
5143 debug_hdr("srvhdr", s, sol, eol);
5144 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5145 cur_idx = txn->hdr_idx.v[cur_idx].next;
5146 }
5147 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005149 /*
5150 * Now we quickly check if we have found a full valid response.
5151 * If not so, we check the FD and buffer states before leaving.
5152 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005153 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005154 * responses are checked first.
5155 *
5156 * Depending on whether the client is still there or not, we
5157 * may send an error response back or not. Note that normally
5158 * we should only check for HTTP status there, and check I/O
5159 * errors somewhere else.
5160 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005161
Willy Tarreau655dce92009-11-08 13:10:58 +01005162 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005163 /* Invalid response */
5164 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5165 /* we detected a parsing error. We want to archive this response
5166 * in the dedicated proxy area for later troubleshooting.
5167 */
5168 hdr_response_bad:
5169 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005170 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005171
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005172 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005173 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005174 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005175 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005176 }
Willy Tarreau64648412010-03-05 10:41:54 +01005177 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005178 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005179 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005180 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005181 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005182 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005183 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005184
Willy Tarreaue7dff022015-04-03 01:14:29 +02005185 if (!(s->flags & SF_ERR_MASK))
5186 s->flags |= SF_ERR_PRXCOND;
5187 if (!(s->flags & SF_FINST_MASK))
5188 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005189
5190 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005191 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005192
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02005194 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005195 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02005196 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005197 goto hdr_response_bad;
5198 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005199
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005200 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005201 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005203 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005204 else if (txn->flags & TX_NOT_FIRST)
5205 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005206
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005207 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005208 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005209 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005210 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005211 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005212
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005213 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005214 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005215 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005216
5217 /* Check to see if the server refused the early data.
5218 * If so, just send a 425
5219 */
5220 if (objt_cs(s->si[1].end)) {
5221 struct connection *conn = objt_cs(s->si[1].end)->conn;
5222
5223 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5224 txn->status = 425;
5225 }
5226
Willy Tarreau350f4872014-11-28 14:42:25 +01005227 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005228 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005229 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005230
Willy Tarreaue7dff022015-04-03 01:14:29 +02005231 if (!(s->flags & SF_ERR_MASK))
5232 s->flags |= SF_ERR_SRVCL;
5233 if (!(s->flags & SF_FINST_MASK))
5234 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005235 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005236 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005237
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005238 /* read timeout : return a 504 to the client. */
5239 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005240 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005241 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005242
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_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005247 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005248
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 = 504;
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 Tarreau4076a152009-04-02 15:18:36 +02005255
Willy Tarreaue7dff022015-04-03 01:14:29 +02005256 if (!(s->flags & SF_ERR_MASK))
5257 s->flags |= SF_ERR_SRVTO;
5258 if (!(s->flags & SF_FINST_MASK))
5259 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005260 return 0;
5261 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005262
Willy Tarreauf003d372012-11-26 13:35:37 +01005263 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005264 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005265 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5266 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005267 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005268 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005269
Christopher Faulet0184ea72017-01-05 14:06:34 +01005270 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005271 channel_auto_close(rep);
5272
5273 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005274 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005275 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005276
Willy Tarreaue7dff022015-04-03 01:14:29 +02005277 if (!(s->flags & SF_ERR_MASK))
5278 s->flags |= SF_ERR_CLICL;
5279 if (!(s->flags & SF_FINST_MASK))
5280 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005281
Willy Tarreau87b09662015-04-03 00:22:06 +02005282 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005283 return 0;
5284 }
5285
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005286 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005287 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005288 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005289 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005290 else if (txn->flags & TX_NOT_FIRST)
5291 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005292
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005293 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005294 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005295 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005296 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005297 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005298
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005299 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005300 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005301 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005302 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005303 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005304 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005305
Willy Tarreaue7dff022015-04-03 01:14:29 +02005306 if (!(s->flags & SF_ERR_MASK))
5307 s->flags |= SF_ERR_SRVCL;
5308 if (!(s->flags & SF_FINST_MASK))
5309 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005310 return 0;
5311 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005312
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005314 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005315 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005316 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005317 else if (txn->flags & TX_NOT_FIRST)
5318 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005319
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005320 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005321 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005322 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005323
Willy Tarreaue7dff022015-04-03 01:14:29 +02005324 if (!(s->flags & SF_ERR_MASK))
5325 s->flags |= SF_ERR_CLICL;
5326 if (!(s->flags & SF_FINST_MASK))
5327 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328
Willy Tarreau87b09662015-04-03 00:22:06 +02005329 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005331 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005332
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005333 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005334 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335 return 0;
5336 }
5337
5338 /* More interesting part now : we know that we have a complete
5339 * response which at least looks like HTTP. We have an indicator
5340 * of each header's length, so we can parse them quickly.
5341 */
5342
5343 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005344 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005345
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005346 /*
5347 * 1: get the status code
5348 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005349 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005350 if (n < 1 || n > 5)
5351 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005352 /* when the client triggers a 4xx from the server, it's most often due
5353 * to a missing object or permission. These events should be tracked
5354 * because if they happen often, it may indicate a brute force or a
5355 * vulnerability scan.
5356 */
5357 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005358 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005359
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005360 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005361 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005362
Willy Tarreau91852eb2015-05-01 13:26:00 +02005363 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5364 * exactly one digit "." one digit. This check may be disabled using
5365 * option accept-invalid-http-response.
5366 */
5367 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5368 if (msg->sl.st.v_l != 8) {
5369 msg->err_pos = 0;
5370 goto hdr_response_bad;
5371 }
5372
Willy Tarreauf37954d2018-06-15 18:31:02 +02005373 if (ci_head(rep)[4] != '/' ||
5374 !isdigit((unsigned char)ci_head(rep)[5]) ||
5375 ci_head(rep)[6] != '.' ||
5376 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02005377 msg->err_pos = 4;
5378 goto hdr_response_bad;
5379 }
5380 }
5381
Willy Tarreau5b154472009-12-21 20:11:07 +01005382 /* check if the response is HTTP/1.1 or above */
5383 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02005384 ((ci_head(rep)[5] > '1') ||
5385 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005386 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005387
5388 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005389 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 +01005390
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005391 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005392 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005393
Willy Tarreauf37954d2018-06-15 18:31:02 +02005394 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005395
Willy Tarreau39650402010-03-15 19:44:39 +01005396 /* Adjust server's health based on status code. Note: status codes 501
5397 * and 505 are triggered on demand by client request, so we must not
5398 * count them as server failures.
5399 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005400 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005401 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005402 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005403 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005404 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005405 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005406
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005407 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005408 * We may be facing a 100-continue response, or any other informational
5409 * 1xx response which is non-final, in which case this is not the right
5410 * response, and we're waiting for the next one. Let's allow this response
5411 * to go to the client and wait for the next one. There's an exception for
5412 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005414 if (txn->status < 200 &&
5415 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005416 hdr_idx_init(&txn->hdr_idx);
5417 msg->next -= channel_forward(rep, msg->next);
5418 msg->msg_state = HTTP_MSG_RPBEFORE;
5419 txn->status = 0;
5420 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005421 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005422 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005423 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005424
Willy Tarreaua14ad722017-07-07 11:36:32 +02005425 /*
5426 * 2: check for cacheability.
5427 */
5428
5429 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005430 case 200:
5431 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005432 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433 case 206:
5434 case 300:
5435 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005436 case 404:
5437 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005438 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005439 case 414:
5440 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005441 break;
5442 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005443 /* RFC7231#6.1:
5444 * Responses with status codes that are defined as
5445 * cacheable by default (e.g., 200, 203, 204, 206,
5446 * 300, 301, 404, 405, 410, 414, and 501 in this
5447 * specification) can be reused by a cache with
5448 * heuristic expiration unless otherwise indicated
5449 * by the method definition or explicit cache
5450 * controls [RFC7234]; all other status codes are
5451 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005452 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005453 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005454 break;
5455 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005456
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005457 /*
5458 * 3: we may need to capture headers
5459 */
5460 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005461 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02005462 capture_headers(ci_head(rep), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005463 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005464
Willy Tarreau557f1992015-05-01 10:05:17 +02005465 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5466 * by RFC7230#3.3.3 :
5467 *
5468 * The length of a message body is determined by one of the following
5469 * (in order of precedence):
5470 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005471 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5472 * the connection will become a tunnel immediately after the empty
5473 * line that concludes the header fields. A client MUST ignore
5474 * any Content-Length or Transfer-Encoding header fields received
5475 * in such a message. Any 101 response (Switching Protocols) is
5476 * managed in the same manner.
5477 *
5478 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005479 * (Informational), 204 (No Content), or 304 (Not Modified) status
5480 * code is always terminated by the first empty line after the
5481 * header fields, regardless of the header fields present in the
5482 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005483 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005484 * 3. If a Transfer-Encoding header field is present and the chunked
5485 * transfer coding (Section 4.1) is the final encoding, the message
5486 * body length is determined by reading and decoding the chunked
5487 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005488 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005489 * If a Transfer-Encoding header field is present in a response and
5490 * the chunked transfer coding is not the final encoding, the
5491 * message body length is determined by reading the connection until
5492 * it is closed by the server. If a Transfer-Encoding header field
5493 * is present in a request and the chunked transfer coding is not
5494 * the final encoding, the message body length cannot be determined
5495 * reliably; the server MUST respond with the 400 (Bad Request)
5496 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005497 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005498 * If a message is received with both a Transfer-Encoding and a
5499 * Content-Length header field, the Transfer-Encoding overrides the
5500 * Content-Length. Such a message might indicate an attempt to
5501 * perform request smuggling (Section 9.5) or response splitting
5502 * (Section 9.4) and ought to be handled as an error. A sender MUST
5503 * remove the received Content-Length field prior to forwarding such
5504 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005505 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005506 * 4. If a message is received without Transfer-Encoding and with
5507 * either multiple Content-Length header fields having differing
5508 * field-values or a single Content-Length header field having an
5509 * invalid value, then the message framing is invalid and the
5510 * recipient MUST treat it as an unrecoverable error. If this is a
5511 * request message, the server MUST respond with a 400 (Bad Request)
5512 * status code and then close the connection. If this is a response
5513 * message received by a proxy, the proxy MUST close the connection
5514 * to the server, discard the received response, and send a 502 (Bad
5515 * Gateway) response to the client. If this is a response message
5516 * received by a user agent, the user agent MUST close the
5517 * connection to the server and discard the received response.
5518 *
5519 * 5. If a valid Content-Length header field is present without
5520 * Transfer-Encoding, its decimal value defines the expected message
5521 * body length in octets. If the sender closes the connection or
5522 * the recipient times out before the indicated number of octets are
5523 * received, the recipient MUST consider the message to be
5524 * incomplete and close the connection.
5525 *
5526 * 6. If this is a request message and none of the above are true, then
5527 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005528 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005529 * 7. Otherwise, this is a response message without a declared message
5530 * body length, so the message body length is determined by the
5531 * number of octets received prior to the server closing the
5532 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005533 */
5534
5535 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005536 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005537 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005538 * FIXME: should we parse anyway and return an error on chunked encoding ?
5539 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005540 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5541 txn->status == 101)) {
5542 /* Either we've established an explicit tunnel, or we're
5543 * switching the protocol. In both cases, we're very unlikely
5544 * to understand the next protocols. We have to switch to tunnel
5545 * mode, so that we transfer the request and responses then let
5546 * this protocol pass unmodified. When we later implement specific
5547 * parsers for such protocols, we'll want to check the Upgrade
5548 * header which contains information about that protocol for
5549 * responses with status 101 (eg: see RFC2817 about TLS).
5550 */
5551 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5552 msg->flags |= HTTP_MSGF_XFER_LEN;
5553 goto end;
5554 }
5555
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005556 if (txn->meth == HTTP_METH_HEAD ||
5557 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005558 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005559 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005560 goto skip_content_length;
5561 }
5562
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005563 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005564 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005565 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005566 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005567 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5568 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005569 /* bad transfer-encoding (chunked followed by something else) */
5570 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005571 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005572 break;
5573 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574 }
5575
Willy Tarreau1c913912015-04-30 10:57:51 +02005576 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005577 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005578 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005579 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005580 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5581 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005582 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005583 signed long long cl;
5584
Willy Tarreauad14f752011-09-02 20:33:27 +02005585 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005586 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005587 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005588 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005589
Willy Tarreauad14f752011-09-02 20:33:27 +02005590 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005591 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005592 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005593 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005594
Willy Tarreauad14f752011-09-02 20:33:27 +02005595 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005596 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005597 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005598 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005599
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005600 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005601 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005602 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005603 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005604
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005605 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005606 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005607 }
5608
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005609 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005610 /* Now we have to check if we need to modify the Connection header.
5611 * This is more difficult on the response than it is on the request,
5612 * because we can have two different HTTP versions and we don't know
5613 * how the client will interprete a response. For instance, let's say
5614 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5615 * HTTP/1.1 response without any header. Maybe it will bound itself to
5616 * HTTP/1.0 because it only knows about it, and will consider the lack
5617 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5618 * the lack of header as a keep-alive. Thus we will use two flags
5619 * indicating how a request MAY be understood by the client. In case
5620 * of multiple possibilities, we'll fix the header to be explicit. If
5621 * ambiguous cases such as both close and keepalive are seen, then we
5622 * will fall back to explicit close. Note that we won't take risks with
5623 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005624 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005625 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005626 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5627 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5628 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5629 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005630 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005631
Willy Tarreau70dffda2014-01-30 03:07:23 +01005632 /* this situation happens when combining pretend-keepalive with httpclose. */
5633 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005634 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005635 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005636 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5637
Willy Tarreau60466522010-01-18 19:08:45 +01005638 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005639 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005640 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5641 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005642
Willy Tarreau60466522010-01-18 19:08:45 +01005643 /* now adjust header transformations depending on current state */
5644 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5645 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5646 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005647 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005648 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005649 }
Willy Tarreau60466522010-01-18 19:08:45 +01005650 else { /* SCL / KAL */
5651 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005652 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005653 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005654 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005655
Willy Tarreau60466522010-01-18 19:08:45 +01005656 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005657 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005658
Willy Tarreau60466522010-01-18 19:08:45 +01005659 /* Some keep-alive responses are converted to Server-close if
5660 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005661 */
Willy Tarreau60466522010-01-18 19:08:45 +01005662 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5663 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005664 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005665 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005666 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005667 }
5668
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005669 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005670 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005671 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005672
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005673 /* end of job, return OK */
5674 rep->analysers &= ~an_bit;
5675 rep->analyse_exp = TICK_ETERNITY;
5676 channel_auto_close(rep);
5677 return 1;
5678
5679 abort_keep_alive:
5680 /* A keep-alive request to the server failed on a network error.
5681 * The client is required to retry. We need to close without returning
5682 * any other information so that the client retries.
5683 */
5684 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005685 rep->analysers &= AN_RES_FLT_END;
5686 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005687 channel_auto_close(rep);
5688 s->logs.logwait = 0;
5689 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005690 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005691 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005692 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005693 return 0;
5694}
5695
5696/* This function performs all the processing enabled for the current response.
5697 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005698 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005699 * other functions. It works like process_request (see indications above).
5700 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005701int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005702{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005703 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005704 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005705 struct http_msg *msg = &txn->rsp;
5706 struct proxy *cur_proxy;
5707 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005708 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005709
Christopher Faulet45073512018-07-20 10:16:29 +02005710 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 +02005711 now_ms, __FUNCTION__,
5712 s,
5713 rep,
5714 rep->rex, rep->wex,
5715 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005716 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005717 rep->analysers);
5718
5719 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5720 return 0;
5721
Willy Tarreau70730dd2014-04-24 18:06:27 +02005722 /* The stats applet needs to adjust the Connection header but we don't
5723 * apply any filter there.
5724 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005725 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5726 rep->analysers &= ~an_bit;
5727 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005728 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005729 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005730
Willy Tarreau58975672014-04-24 21:13:57 +02005731 /*
5732 * We will have to evaluate the filters.
5733 * As opposed to version 1.2, now they will be evaluated in the
5734 * filters order and not in the header order. This means that
5735 * each filter has to be validated among all headers.
5736 *
5737 * Filters are tried with ->be first, then with ->fe if it is
5738 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005739 *
5740 * Maybe we are in resume condiion. In this case I choose the
5741 * "struct proxy" which contains the rule list matching the resume
5742 * pointer. If none of theses "struct proxy" match, I initialise
5743 * the process with the first one.
5744 *
5745 * In fact, I check only correspondance betwwen the current list
5746 * pointer and the ->fe rule list. If it doesn't match, I initialize
5747 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005748 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005749 if (s->current_rule_list == &sess->fe->http_res_rules)
5750 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005751 else
5752 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005753 while (1) {
5754 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005755
Willy Tarreau58975672014-04-24 21:13:57 +02005756 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005757 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005758 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005759
Willy Tarreau51d861a2015-05-22 17:30:48 +02005760 if (ret == HTTP_RULE_RES_BADREQ)
5761 goto return_srv_prx_502;
5762
5763 if (ret == HTTP_RULE_RES_DONE) {
5764 rep->analysers &= ~an_bit;
5765 rep->analyse_exp = TICK_ETERNITY;
5766 return 1;
5767 }
5768 }
5769
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005770 /* we need to be called again. */
5771 if (ret == HTTP_RULE_RES_YIELD) {
5772 channel_dont_close(rep);
5773 return 0;
5774 }
5775
Willy Tarreau58975672014-04-24 21:13:57 +02005776 /* try headers filters */
5777 if (rule_set->rsp_exp != NULL) {
5778 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5779 return_bad_resp:
5780 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005781 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005782 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005783 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005784 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005785 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005786 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005787 txn->status = 502;
5788 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005789 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005790 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005791 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005792 if (!(s->flags & SF_ERR_MASK))
5793 s->flags |= SF_ERR_PRXCOND;
5794 if (!(s->flags & SF_FINST_MASK))
5795 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005796 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005797 }
Willy Tarreau58975672014-04-24 21:13:57 +02005798 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005799
Willy Tarreau58975672014-04-24 21:13:57 +02005800 /* has the response been denied ? */
5801 if (txn->flags & TX_SVDENY) {
5802 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005803 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005804
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005805 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5806 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005807 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005808 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005809
Willy Tarreau58975672014-04-24 21:13:57 +02005810 goto return_srv_prx_502;
5811 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005812
Willy Tarreau58975672014-04-24 21:13:57 +02005813 /* add response headers from the rule sets in the same order */
5814 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005815 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005816 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005817 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005818 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005819 ret = acl_pass(ret);
5820 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5821 ret = !ret;
5822 if (!ret)
5823 continue;
5824 }
5825 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5826 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005827 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005828
Willy Tarreau58975672014-04-24 21:13:57 +02005829 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005830 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005831 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005832 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005833 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005834
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005835 /* After this point, this anayzer can't return yield, so we can
5836 * remove the bit corresponding to this analyzer from the list.
5837 *
5838 * Note that the intermediate returns and goto found previously
5839 * reset the analyzers.
5840 */
5841 rep->analysers &= ~an_bit;
5842 rep->analyse_exp = TICK_ETERNITY;
5843
Willy Tarreau58975672014-04-24 21:13:57 +02005844 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005845 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005846 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005847
Willy Tarreau58975672014-04-24 21:13:57 +02005848 /*
5849 * Now check for a server cookie.
5850 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005851 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005852 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005853
Willy Tarreau58975672014-04-24 21:13:57 +02005854 /*
5855 * Check for cache-control or pragma headers if required.
5856 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005857 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005858 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005859
Willy Tarreau58975672014-04-24 21:13:57 +02005860 /*
5861 * Add server cookie in the response if needed
5862 */
5863 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5864 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005865 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005866 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5867 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5868 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5869 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5870 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005871 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005872 /* the server is known, it's not the one the client requested, or the
5873 * cookie's last seen date needs to be refreshed. We have to
5874 * insert a set-cookie here, except if we want to insert only on POST
5875 * requests and this one isn't. Note that servers which don't have cookies
5876 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877 */
Willy Tarreau58975672014-04-24 21:13:57 +02005878 if (!objt_server(s->target)->cookie) {
5879 chunk_printf(&trash,
5880 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5881 s->be->cookie_name);
5882 }
5883 else {
5884 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005885
Willy Tarreau58975672014-04-24 21:13:57 +02005886 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5887 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005888 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5889 s30tob64((date.tv_sec+3) >> 2,
5890 trash.area + trash.data);
5891 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005892
Willy Tarreau58975672014-04-24 21:13:57 +02005893 if (s->be->cookie_maxlife) {
5894 /* emit first_date, which is either the original one or
5895 * the current date.
5896 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005897 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005898 s30tob64(txn->cookie_first_date ?
5899 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005900 (date.tv_sec+3) >> 2,
5901 trash.area + trash.data);
5902 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005903 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005904 }
Willy Tarreau58975672014-04-24 21:13:57 +02005905 chunk_appendf(&trash, "; path=/");
5906 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005907
Willy Tarreau58975672014-04-24 21:13:57 +02005908 if (s->be->cookie_domain)
5909 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005910
Willy Tarreau58975672014-04-24 21:13:57 +02005911 if (s->be->ck_opts & PR_CK_HTTPONLY)
5912 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005913
Willy Tarreau58975672014-04-24 21:13:57 +02005914 if (s->be->ck_opts & PR_CK_SECURE)
5915 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005916
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005917 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005918 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005919
Willy Tarreau58975672014-04-24 21:13:57 +02005920 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005921 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005922 /* the server did not change, only the date was updated */
5923 txn->flags |= TX_SCK_UPDATED;
5924 else
5925 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005926
Willy Tarreau58975672014-04-24 21:13:57 +02005927 /* Here, we will tell an eventual cache on the client side that we don't
5928 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5929 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5930 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005931 */
Willy Tarreau58975672014-04-24 21:13:57 +02005932 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005933
Willy Tarreau58975672014-04-24 21:13:57 +02005934 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005935
Willy Tarreau58975672014-04-24 21:13:57 +02005936 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5937 "Cache-control: private", 22) < 0))
5938 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005939 }
Willy Tarreau58975672014-04-24 21:13:57 +02005940 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005941
Willy Tarreau58975672014-04-24 21:13:57 +02005942 /*
5943 * Check if result will be cacheable with a cookie.
5944 * We'll block the response if security checks have caught
5945 * nasty things such as a cacheable cookie.
5946 */
5947 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5948 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5949 (s->be->options & PR_O_CHK_CACHE)) {
5950 /* we're in presence of a cacheable response containing
5951 * a set-cookie header. We'll block it as requested by
5952 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005953 */
Willy Tarreau58975672014-04-24 21:13:57 +02005954 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005955 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005956
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005957 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5958 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005959 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005960 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005961
Christopher Faulet767a84b2017-11-24 16:50:31 +01005962 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5963 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005964 send_log(s->be, LOG_ALERT,
5965 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5966 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5967 goto return_srv_prx_502;
5968 }
Willy Tarreau03945942009-12-22 16:50:27 +01005969
Willy Tarreau70730dd2014-04-24 18:06:27 +02005970 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005971 /*
5972 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5973 * If an "Upgrade" token is found, the header is left untouched in order
5974 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005975 * if anything but "Upgrade" is present in the Connection header. We don't
5976 * want to touch any 101 response either since it's switching to another
5977 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005978 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005979 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005980 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005981 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005982 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5983 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005984
Willy Tarreau58975672014-04-24 21:13:57 +02005985 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5986 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5987 /* we want a keep-alive response here. Keep-alive header
5988 * required if either side is not 1.1.
5989 */
5990 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5991 want_flags |= TX_CON_KAL_SET;
5992 }
5993 else {
5994 /* we want a close response here. Close header required if
5995 * the server is 1.1, regardless of the client.
5996 */
5997 if (msg->flags & HTTP_MSGF_VER_11)
5998 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005999 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006000
Willy Tarreau58975672014-04-24 21:13:57 +02006001 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6002 http_change_connection_header(txn, msg, want_flags);
6003 }
6004
6005 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02006006 /* Always enter in the body analyzer */
6007 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
6008 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006009
Willy Tarreau58975672014-04-24 21:13:57 +02006010 /* if the user wants to log as soon as possible, without counting
6011 * bytes from the server, then this is the right moment. We have
6012 * to temporarily assign bytes_out to log what we currently have.
6013 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006014 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006015 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6016 s->logs.bytes_out = txn->rsp.eoh;
6017 s->do_log(s);
6018 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006019 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006020 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006021}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006022
Willy Tarreaud98cf932009-12-27 22:54:55 +01006023/* This function is an analyser which forwards response body (including chunk
6024 * sizes if any). It is called as soon as we must forward, even if we forward
6025 * zero byte. The only situation where it must not be called is when we're in
6026 * tunnel mode and we want to forward till the close. It's used both to forward
6027 * remaining data and to resync after end of body. It expects the msg_state to
6028 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006029 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006030 *
6031 * It is capable of compressing response data both in content-length mode and
6032 * in chunked mode. The state machines follows different flows depending on
6033 * whether content-length and chunked modes are used, since there are no
6034 * trailers in content-length :
6035 *
6036 * chk-mode cl-mode
6037 * ,----- BODY -----.
6038 * / \
6039 * V size > 0 V chk-mode
6040 * .--> SIZE -------------> DATA -------------> CRLF
6041 * | | size == 0 | last byte |
6042 * | v final crlf v inspected |
6043 * | TRAILERS -----------> DONE |
6044 * | |
6045 * `----------------------------------------------'
6046 *
6047 * Compression only happens in the DATA state, and must be flushed in final
6048 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6049 * is performed at once on final states for all bytes parsed, or when leaving
6050 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006051 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006052int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006053{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006054 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006055 struct http_txn *txn = s->txn;
6056 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006057 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006058
Christopher Faulet45073512018-07-20 10:16:29 +02006059 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 +02006060 now_ms, __FUNCTION__,
6061 s,
6062 res,
6063 res->rex, res->wex,
6064 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02006065 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02006066 res->analysers);
6067
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006068 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6069 return 0;
6070
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006071 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02006072 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006073 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006074 /* Output closed while we were sending data. We must abort and
6075 * wake the other side up.
6076 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006077 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006078 msg->msg_state = HTTP_MSG_ERROR;
6079 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006080 return 1;
6081 }
6082
Willy Tarreau4fe41902010-06-07 22:27:41 +02006083 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006084 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006085
Christopher Fauletd7c91962015-04-30 11:48:27 +02006086 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006087 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6088 ? HTTP_MSG_CHUNK_SIZE
6089 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006090 }
6091
Willy Tarreauefdf0942014-04-24 20:08:57 +02006092 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006093 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006094 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006095 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006096 }
6097
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006098 if (msg->msg_state < HTTP_MSG_DONE) {
6099 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6100 ? http_msg_forward_chunked_body(s, msg)
6101 : http_msg_forward_body(s, msg));
6102 if (!ret)
6103 goto missing_data_or_waiting;
6104 if (ret < 0)
6105 goto return_bad_res;
6106 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006107
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006108 /* other states, DONE...TUNNEL */
6109 /* for keep-alive we don't want to forward closes on DONE */
6110 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6111 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6112 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006113
Christopher Faulet894da4c2017-07-18 11:29:07 +02006114 http_resync_states(s);
6115 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006116 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6117 if (res->flags & CF_SHUTW) {
6118 /* response errors are most likely due to the
6119 * client aborting the transfer. */
6120 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006121 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006122 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02006123 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006124 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006125 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006126 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006127 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006128 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006129
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006130 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006131 if (res->flags & CF_SHUTW)
6132 goto aborted_xfer;
6133
6134 /* stop waiting for data if the input is closed before the end. If the
6135 * client side was already closed, it means that the client has aborted,
6136 * so we don't want to count this as a server abort. Otherwise it's a
6137 * server abort.
6138 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006139 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006140 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006141 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006142 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02006143 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006144 if (!(s->flags & SF_ERR_MASK))
6145 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006146 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006147 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006148 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006149 goto return_bad_res_stats_ok;
6150 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006151 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006152
Willy Tarreau40dba092010-03-04 18:14:51 +01006153 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006154 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006155 goto return_bad_res;
6156
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006157 /* When TE: chunked is used, we need to get there again to parse
6158 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006159 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6160 * or if there are filters registered on the stream, we don't want to
6161 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006162 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006163 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006164 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006165 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6166 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006167 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006168
Willy Tarreau5c620922011-05-11 19:56:11 +02006169 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006170 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006171 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006172 * modes are already handled by the stream sock layer. We must not do
6173 * this in content-length mode because it could present the MSG_MORE
6174 * flag with the last block of forwarded data, which would cause an
6175 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006176 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006177 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006178 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006179
Willy Tarreau87b09662015-04-03 00:22:06 +02006180 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006181 return 0;
6182
Willy Tarreau40dba092010-03-04 18:14:51 +01006183 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006184 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006185 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006186 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006187
6188 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006189 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006190 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006191 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006192 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006193 res->analysers &= AN_RES_FLT_END;
6194 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 +01006195 if (objt_server(s->target))
6196 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006197
Willy Tarreaue7dff022015-04-03 01:14:29 +02006198 if (!(s->flags & SF_ERR_MASK))
6199 s->flags |= SF_ERR_PRXCOND;
6200 if (!(s->flags & SF_FINST_MASK))
6201 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006202 return 0;
6203
6204 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006205 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006206 txn->rsp.msg_state = HTTP_MSG_ERROR;
6207 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006208 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006209 res->analysers &= AN_RES_FLT_END;
6210 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 +01006211
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006212 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6213 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006214 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006215 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006216
Willy Tarreaue7dff022015-04-03 01:14:29 +02006217 if (!(s->flags & SF_ERR_MASK))
6218 s->flags |= SF_ERR_CLICL;
6219 if (!(s->flags & SF_FINST_MASK))
6220 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006221 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006222}
6223
6224
6225static inline int
6226http_msg_forward_body(struct stream *s, struct http_msg *msg)
6227{
6228 struct channel *chn = msg->chn;
6229 int ret;
6230
6231 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6232
6233 if (msg->msg_state == HTTP_MSG_ENDING)
6234 goto ending;
6235
6236 /* Neither content-length, nor transfer-encoding was found, so we must
6237 * read the body until the server connection is closed. In that case, we
6238 * eat data as they come. Of course, this happens for response only. */
6239 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006240 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006241 msg->chunk_len += len;
6242 msg->body_len += len;
6243 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006244 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006245 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006246 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006247 msg->next += ret;
6248 msg->chunk_len -= ret;
6249 if (msg->chunk_len) {
6250 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006251 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006252 chn->flags |= CF_WAKE_WRITE;
6253 goto missing_data_or_waiting;
6254 }
6255
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006256 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6257 * always set for a request. */
6258 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6259 /* The server still sending data that should be filtered */
6260 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6261 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006262 msg->msg_state = HTTP_MSG_TUNNEL;
6263 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006264 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006265
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006266 msg->msg_state = HTTP_MSG_ENDING;
6267
6268 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006269 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006270 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006271 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6272 /* default_ret */ msg->next,
6273 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006274 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006275 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006276 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6277 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006278 if (msg->next)
6279 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006280
Christopher Fauletda02e172015-12-04 09:25:05 +01006281 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6282 /* default_ret */ 1,
6283 /* on_error */ goto error,
6284 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006285 if (msg->msg_state == HTTP_MSG_ENDING)
6286 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006287 return 1;
6288
6289 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006290 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006291 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6292 /* default_ret */ msg->next,
6293 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006294 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006295 msg->next -= ret;
6296 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6297 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006298 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006299 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006300 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006301 return 0;
6302 error:
6303 return -1;
6304}
6305
6306static inline int
6307http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6308{
6309 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006310 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006311 int ret;
6312
6313 /* Here we have the guarantee to be in one of the following state:
6314 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6315 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6316
6317 switch_states:
6318 switch (msg->msg_state) {
6319 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006320 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02006321 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01006322 /* on_error */ goto error);
6323 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006324 msg->chunk_len -= ret;
6325 if (msg->chunk_len) {
6326 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006327 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006328 chn->flags |= CF_WAKE_WRITE;
6329 goto missing_data_or_waiting;
6330 }
6331
6332 /* nothing left to forward for this chunk*/
6333 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6334 /* fall through for HTTP_MSG_CHUNK_CRLF */
6335
6336 case HTTP_MSG_CHUNK_CRLF:
6337 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006338 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006339 if (ret == 0)
6340 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006341 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006342 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02006343 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006344 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006345 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006346 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006347 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006348 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6349 /* fall through for HTTP_MSG_CHUNK_SIZE */
6350
6351 case HTTP_MSG_CHUNK_SIZE:
6352 /* read the chunk size and assign it to ->chunk_len,
6353 * then set ->next to point to the body and switch to
6354 * DATA or TRAILERS state.
6355 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006356 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006357 if (ret == 0)
6358 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006359 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02006360 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006361 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006362 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006363 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006364 }
6365
6366 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006367 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006368 msg->chunk_len = chunk;
6369 msg->body_len += chunk;
6370
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006371 if (msg->chunk_len) {
6372 msg->msg_state = HTTP_MSG_DATA;
6373 goto switch_states;
6374 }
6375 msg->msg_state = HTTP_MSG_TRAILERS;
6376 /* fall through for HTTP_MSG_TRAILERS */
6377
6378 case HTTP_MSG_TRAILERS:
6379 ret = http_forward_trailers(msg);
6380 if (ret < 0)
6381 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006382 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6383 /* default_ret */ 1,
6384 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006385 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006386 if (!ret)
6387 goto missing_data_or_waiting;
6388 break;
6389
6390 case HTTP_MSG_ENDING:
6391 goto ending;
6392
6393 default:
6394 /* This should no happen in this function */
6395 goto error;
6396 }
6397
6398 msg->msg_state = HTTP_MSG_ENDING;
6399 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006400 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006401 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006402 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006403 /* default_ret */ msg->next,
6404 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006405 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006406 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006407 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6408 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006409 if (msg->next)
6410 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006411
Christopher Fauletda02e172015-12-04 09:25:05 +01006412 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006413 /* default_ret */ 1,
6414 /* on_error */ goto error,
6415 /* on_wait */ goto waiting);
6416 msg->msg_state = HTTP_MSG_DONE;
6417 return 1;
6418
6419 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006420 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006421 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006422 /* default_ret */ msg->next,
6423 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006424 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006425 msg->next -= ret;
6426 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6427 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006428 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006429 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006430 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006431 return 0;
6432
6433 chunk_parsing_error:
6434 if (msg->err_pos >= 0) {
6435 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02006436 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006437 msg->msg_state, strm_fe(s));
6438 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02006439 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006440 msg, msg->msg_state, s->be);
6441 }
6442 error:
6443 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006444}
6445
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447/* Iterate the same filter through all request headers.
6448 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006449 * Since it can manage the switch to another backend, it updates the per-proxy
6450 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006452int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006453{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 char *cur_ptr, *cur_end, *cur_next;
6455 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006456 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006457 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006458 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006460 last_hdr = 0;
6461
Willy Tarreauf37954d2018-06-15 18:31:02 +02006462 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 old_idx = 0;
6464
6465 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006466 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006468 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 (exp->action == ACT_ALLOW ||
6470 exp->action == ACT_DENY ||
6471 exp->action == ACT_TARPIT))
6472 return 0;
6473
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006474 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 if (!cur_idx)
6476 break;
6477
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006478 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006479 cur_ptr = cur_next;
6480 cur_end = cur_ptr + cur_hdr->len;
6481 cur_next = cur_end + cur_hdr->cr + 1;
6482
6483 /* Now we have one header between cur_ptr and cur_end,
6484 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006485 */
6486
Willy Tarreau15a53a42015-01-21 13:39:42 +01006487 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006489 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006490 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491 last_hdr = 1;
6492 break;
6493
6494 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006495 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 break;
6498
6499 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006500 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006501 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 break;
6503
6504 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006505 len = exp_replace(trash.area,
6506 trash.size, cur_ptr,
6507 exp->replace, pmatch);
6508 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006509 return -1;
6510
Willy Tarreau6e27be12018-08-22 04:46:47 +02006511 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006513 /* FIXME: if the user adds a newline in the replacement, the
6514 * index will not be recalculated for now, and the new line
6515 * will not be counted as a new header.
6516 */
6517
6518 cur_end += delta;
6519 cur_next += delta;
6520 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006521 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 break;
6523
6524 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006525 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 cur_next += delta;
6527
Willy Tarreaufa355d42009-11-29 18:12:29 +01006528 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006529 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6530 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 cur_hdr->len = 0;
6532 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006533 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006534 break;
6535
6536 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006537 }
6538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 /* keep the link from this header to next one in case of later
6540 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006541 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006542 old_idx = cur_idx;
6543 }
6544 return 0;
6545}
6546
6547
6548/* Apply the filter to the request line.
6549 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6550 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551 * Since it can manage the switch to another backend, it updates the per-proxy
6552 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006553 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006554int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 char *cur_ptr, *cur_end;
6557 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006558 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006559 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560
Willy Tarreau3d300592007-03-18 18:34:41 +01006561 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006563 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 (exp->action == ACT_ALLOW ||
6565 exp->action == ACT_DENY ||
6566 exp->action == ACT_TARPIT))
6567 return 0;
6568 else if (exp->action == ACT_REMOVE)
6569 return 0;
6570
6571 done = 0;
6572
Willy Tarreauf37954d2018-06-15 18:31:02 +02006573 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006574 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006575
6576 /* Now we have the request line between cur_ptr and cur_end */
6577
Willy Tarreau15a53a42015-01-21 13:39:42 +01006578 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006581 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006582 done = 1;
6583 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006585 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006586 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006587 done = 1;
6588 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006590 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006591 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006592 done = 1;
6593 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006594
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006596 len = exp_replace(trash.area, trash.size,
6597 cur_ptr, exp->replace, pmatch);
6598 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006599 return -1;
6600
Willy Tarreau6e27be12018-08-22 04:46:47 +02006601 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006603 /* FIXME: if the user adds a newline in the replacement, the
6604 * index will not be recalculated for now, and the new line
6605 * will not be counted as a new header.
6606 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006607
Willy Tarreaufa355d42009-11-29 18:12:29 +01006608 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006609 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006610 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006611 HTTP_MSG_RQMETH,
6612 cur_ptr, cur_end + 1,
6613 NULL, NULL);
6614 if (unlikely(!cur_end))
6615 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006617 /* we have a full request and we know that we have either a CR
6618 * or an LF at <ptr>.
6619 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006620 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6621 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006622 /* there is no point trying this regex on headers */
6623 return 1;
6624 }
6625 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006626 return done;
6627}
Willy Tarreau97de6242006-12-27 17:18:38 +01006628
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629
Willy Tarreau58f10d72006-12-04 02:26:12 +01006630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006631/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006632 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006633 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634 * unparsable request. Since it can manage the switch to another backend, it
6635 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006636 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006637int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006638{
Willy Tarreau192252e2015-04-04 01:47:55 +02006639 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006640 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006641 struct hdr_exp *exp;
6642
6643 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006644 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 /*
6647 * The interleaving of transformations and verdicts
6648 * makes it difficult to decide to continue or stop
6649 * the evaluation.
6650 */
6651
Willy Tarreau6c123b12010-01-28 20:22:06 +01006652 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6653 break;
6654
Willy Tarreau3d300592007-03-18 18:34:41 +01006655 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006656 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006657 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006658 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006659
6660 /* if this filter had a condition, evaluate it now and skip to
6661 * next filter if the condition does not match.
6662 */
6663 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006664 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006665 ret = acl_pass(ret);
6666 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6667 ret = !ret;
6668
6669 if (!ret)
6670 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006671 }
6672
6673 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006674 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006675 if (unlikely(ret < 0))
6676 return -1;
6677
6678 if (likely(ret == 0)) {
6679 /* The filter did not match the request, it can be
6680 * iterated through all headers.
6681 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006682 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6683 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006684 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006685 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006686 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006687}
6688
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006689
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006690/* Find the end of a cookie value contained between <s> and <e>. It works the
6691 * same way as with headers above except that the semi-colon also ends a token.
6692 * See RFC2965 for more information. Note that it requires a valid header to
6693 * return a valid result.
6694 */
6695char *find_cookie_value_end(char *s, const char *e)
6696{
6697 int quoted, qdpair;
6698
6699 quoted = qdpair = 0;
6700 for (; s < e; s++) {
6701 if (qdpair) qdpair = 0;
6702 else if (quoted) {
6703 if (*s == '\\') qdpair = 1;
6704 else if (*s == '"') quoted = 0;
6705 }
6706 else if (*s == '"') quoted = 1;
6707 else if (*s == ',' || *s == ';') return s;
6708 }
6709 return s;
6710}
6711
6712/* Delete a value in a header between delimiters <from> and <next> in buffer
6713 * <buf>. The number of characters displaced is returned, and the pointer to
6714 * the first delimiter is updated if required. The function tries as much as
6715 * possible to respect the following principles :
6716 * - replace <from> delimiter by the <next> one unless <from> points to a
6717 * colon, in which case <next> is simply removed
6718 * - set exactly one space character after the new first delimiter, unless
6719 * there are not enough characters in the block being moved to do so.
6720 * - remove unneeded spaces before the previous delimiter and after the new
6721 * one.
6722 *
6723 * It is the caller's responsibility to ensure that :
6724 * - <from> points to a valid delimiter or the colon ;
6725 * - <next> points to a valid delimiter or the final CR/LF ;
6726 * - there are non-space chars before <from> ;
6727 * - there is a CR/LF at or after <next>.
6728 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006729int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006730{
6731 char *prev = *from;
6732
6733 if (*prev == ':') {
6734 /* We're removing the first value, preserve the colon and add a
6735 * space if possible.
6736 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006737 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006738 next++;
6739 prev++;
6740 if (prev < next)
6741 *prev++ = ' ';
6742
Willy Tarreau2235b262016-11-05 15:50:20 +01006743 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006744 next++;
6745 } else {
6746 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006747 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006748 prev--;
6749 *from = prev;
6750
6751 /* copy the delimiter and if possible a space if we're
6752 * not at the end of the line.
6753 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006754 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006755 *prev++ = *next++;
6756 if (prev + 1 < next)
6757 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006758 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006759 next++;
6760 }
6761 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006762 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006763}
6764
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006765/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006766 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 * desirable to call it only when needed. This code is quite complex because
6768 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6769 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006771void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006772{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006773 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006774 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006775 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006776 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006777 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6778 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006779
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006781 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006782 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006783
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006784 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006785 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006786 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006787
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006788 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006789 hdr_beg = hdr_next;
6790 hdr_end = hdr_beg + cur_hdr->len;
6791 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006792
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 /* We have one full header between hdr_beg and hdr_end, and the
6794 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006795 * "Cookie:" headers.
6796 */
6797
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006798 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006799 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006800 old_idx = cur_idx;
6801 continue;
6802 }
6803
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006804 del_from = NULL; /* nothing to be deleted */
6805 preserve_hdr = 0; /* assume we may kill the whole header */
6806
Willy Tarreau58f10d72006-12-04 02:26:12 +01006807 /* Now look for cookies. Conforming to RFC2109, we have to support
6808 * attributes whose name begin with a '$', and associate them with
6809 * the right cookie, if we want to delete this cookie.
6810 * So there are 3 cases for each cookie read :
6811 * 1) it's a special attribute, beginning with a '$' : ignore it.
6812 * 2) it's a server id cookie that we *MAY* want to delete : save
6813 * some pointers on it (last semi-colon, beginning of cookie...)
6814 * 3) it's an application cookie : we *MAY* have to delete a previous
6815 * "special" cookie.
6816 * At the end of loop, if a "special" cookie remains, we may have to
6817 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006818 * *MUST* delete it.
6819 *
6820 * Note: RFC2965 is unclear about the processing of spaces around
6821 * the equal sign in the ATTR=VALUE form. A careful inspection of
6822 * the RFC explicitly allows spaces before it, and not within the
6823 * tokens (attrs or values). An inspection of RFC2109 allows that
6824 * too but section 10.1.3 lets one think that spaces may be allowed
6825 * after the equal sign too, resulting in some (rare) buggy
6826 * implementations trying to do that. So let's do what servers do.
6827 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6828 * allowed quoted strings in values, with any possible character
6829 * after a backslash, including control chars and delimitors, which
6830 * causes parsing to become ambiguous. Browsers also allow spaces
6831 * within values even without quotes.
6832 *
6833 * We have to keep multiple pointers in order to support cookie
6834 * removal at the beginning, middle or end of header without
6835 * corrupting the header. All of these headers are valid :
6836 *
6837 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6838 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6839 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6840 * | | | | | | | | |
6841 * | | | | | | | | hdr_end <--+
6842 * | | | | | | | +--> next
6843 * | | | | | | +----> val_end
6844 * | | | | | +-----------> val_beg
6845 * | | | | +--------------> equal
6846 * | | | +----------------> att_end
6847 * | | +---------------------> att_beg
6848 * | +--------------------------> prev
6849 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006850 */
6851
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006852 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6853 /* Iterate through all cookies on this line */
6854
6855 /* find att_beg */
6856 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006857 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006858 att_beg++;
6859
6860 /* find att_end : this is the first character after the last non
6861 * space before the equal. It may be equal to hdr_end.
6862 */
6863 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 while (equal < hdr_end) {
6866 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006867 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006868 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006869 continue;
6870 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871 }
6872
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6874 * is between <att_beg> and <equal>, both may be identical.
6875 */
6876
6877 /* look for end of cookie if there is an equal sign */
6878 if (equal < hdr_end && *equal == '=') {
6879 /* look for the beginning of the value */
6880 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006881 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006882 val_beg++;
6883
6884 /* find the end of the value, respecting quotes */
6885 next = find_cookie_value_end(val_beg, hdr_end);
6886
6887 /* make val_end point to the first white space or delimitor after the value */
6888 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006889 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 val_end--;
6891 } else {
6892 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006893 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 /* We have nothing to do with attributes beginning with '$'. However,
6896 * they will automatically be removed if a header before them is removed,
6897 * since they're supposed to be linked together.
6898 */
6899 if (*att_beg == '$')
6900 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006901
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006902 /* Ignore cookies with no equal sign */
6903 if (equal == next) {
6904 /* This is not our cookie, so we must preserve it. But if we already
6905 * scheduled another cookie for removal, we cannot remove the
6906 * complete header, but we can remove the previous block itself.
6907 */
6908 preserve_hdr = 1;
6909 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006910 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006911 val_end += delta;
6912 next += delta;
6913 hdr_end += delta;
6914 hdr_next += delta;
6915 cur_hdr->len += delta;
6916 http_msg_move_end(&txn->req, delta);
6917 prev = del_from;
6918 del_from = NULL;
6919 }
6920 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006921 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006922
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006923 /* if there are spaces around the equal sign, we need to
6924 * strip them otherwise we'll get trouble for cookie captures,
6925 * or even for rewrites. Since this happens extremely rarely,
6926 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006928 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6929 int stripped_before = 0;
6930 int stripped_after = 0;
6931
6932 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006933 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006934 equal += stripped_before;
6935 val_beg += stripped_before;
6936 }
6937
6938 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006939 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006940 val_beg += stripped_after;
6941 stripped_before += stripped_after;
6942 }
6943
6944 val_end += stripped_before;
6945 next += stripped_before;
6946 hdr_end += stripped_before;
6947 hdr_next += stripped_before;
6948 cur_hdr->len += stripped_before;
6949 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006950 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006951 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006952
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 /* First, let's see if we want to capture this cookie. We check
6954 * that we don't already have a client side cookie, because we
6955 * can only capture one. Also as an optimisation, we ignore
6956 * cookies shorter than the declared name.
6957 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006958 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6959 (val_end - att_beg >= sess->fe->capture_namelen) &&
6960 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006961 int log_len = val_end - att_beg;
6962
Willy Tarreaubafbe012017-11-24 17:34:44 +01006963 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006964 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006965 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006966 if (log_len > sess->fe->capture_len)
6967 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006968 memcpy(txn->cli_cookie, att_beg, log_len);
6969 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006970 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006971 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972
Willy Tarreaubca99692010-10-06 19:25:55 +02006973 /* Persistence cookies in passive, rewrite or insert mode have the
6974 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006975 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006976 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006977 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006978 * For cookies in prefix mode, the form is :
6979 *
6980 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006981 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006982 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6983 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6984 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006985 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006986
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006987 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6988 * have the server ID between val_beg and delim, and the original cookie between
6989 * delim+1 and val_end. Otherwise, delim==val_end :
6990 *
6991 * Cookie: NAME=SRV; # in all but prefix modes
6992 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6993 * | || || | |+-> next
6994 * | || || | +--> val_end
6995 * | || || +---------> delim
6996 * | || |+------------> val_beg
6997 * | || +-------------> att_end = equal
6998 * | |+-----------------> att_beg
6999 * | +------------------> prev
7000 * +-------------------------> hdr_beg
7001 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007002
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007003 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007004 for (delim = val_beg; delim < val_end; delim++)
7005 if (*delim == COOKIE_DELIM)
7006 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007007 } else {
7008 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007009 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007010 /* Now check if the cookie contains a date field, which would
7011 * appear after a vertical bar ('|') just after the server name
7012 * and before the delimiter.
7013 */
7014 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7015 if (vbar1) {
7016 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007017 * right is the last seen date. It is a base64 encoded
7018 * 30-bit value representing the UNIX date since the
7019 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007020 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007021 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007022 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007023 if (val_end - vbar1 >= 5) {
7024 val = b64tos30(vbar1);
7025 if (val > 0)
7026 txn->cookie_last_date = val << 2;
7027 }
7028 /* look for a second vertical bar */
7029 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7030 if (vbar1 && (val_end - vbar1 > 5)) {
7031 val = b64tos30(vbar1 + 1);
7032 if (val > 0)
7033 txn->cookie_first_date = val << 2;
7034 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007035 }
7036 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037
Willy Tarreauf64d1412010-10-07 20:06:11 +02007038 /* if the cookie has an expiration date and the proxy wants to check
7039 * it, then we do that now. We first check if the cookie is too old,
7040 * then only if it has expired. We detect strict overflow because the
7041 * time resolution here is not great (4 seconds). Cookies with dates
7042 * in the future are ignored if their offset is beyond one day. This
7043 * allows an admin to fix timezone issues without expiring everyone
7044 * and at the same time avoids keeping unwanted side effects for too
7045 * long.
7046 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007047 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7048 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007049 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007050 txn->flags &= ~TX_CK_MASK;
7051 txn->flags |= TX_CK_OLD;
7052 delim = val_beg; // let's pretend we have not found the cookie
7053 txn->cookie_first_date = 0;
7054 txn->cookie_last_date = 0;
7055 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007056 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7057 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007058 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007059 txn->flags &= ~TX_CK_MASK;
7060 txn->flags |= TX_CK_EXPIRED;
7061 delim = val_beg; // let's pretend we have not found the cookie
7062 txn->cookie_first_date = 0;
7063 txn->cookie_last_date = 0;
7064 }
7065
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007066 /* Here, we'll look for the first running server which supports the cookie.
7067 * This allows to share a same cookie between several servers, for example
7068 * to dedicate backup servers to specific servers only.
7069 * However, to prevent clients from sticking to cookie-less backup server
7070 * when they have incidentely learned an empty cookie, we simply ignore
7071 * empty cookies and mark them as invalid.
7072 * The same behaviour is applied when persistence must be ignored.
7073 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007074 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007075 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007077 while (srv) {
7078 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7079 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007080 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007081 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007082 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007083 /* we found the server and we can use it */
7084 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007085 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007086 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007087 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 break;
7089 } else {
7090 /* we found a server, but it's down,
7091 * mark it as such and go on in case
7092 * another one is available.
7093 */
7094 txn->flags &= ~TX_CK_MASK;
7095 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007097 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007098 srv = srv->next;
7099 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100
Willy Tarreauf64d1412010-10-07 20:06:11 +02007101 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007102 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007104 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007105 txn->flags |= TX_CK_UNUSED;
7106 else
7107 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007110 /* depending on the cookie mode, we may have to either :
7111 * - delete the complete cookie if we're in insert+indirect mode, so that
7112 * the server never sees it ;
7113 * - remove the server id from the cookie value, and tag the cookie as an
7114 * application cookie so that it does not get accidentely removed later,
7115 * if we're in cookie prefix mode
7116 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007117 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007118 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007120 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007121 val_end += delta;
7122 next += delta;
7123 hdr_end += delta;
7124 hdr_next += delta;
7125 cur_hdr->len += delta;
7126 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007127
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007128 del_from = NULL;
7129 preserve_hdr = 1; /* we want to keep this cookie */
7130 }
7131 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007132 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 del_from = prev;
7134 }
7135 } else {
7136 /* This is not our cookie, so we must preserve it. But if we already
7137 * scheduled another cookie for removal, we cannot remove the
7138 * complete header, but we can remove the previous block itself.
7139 */
7140 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007141
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007143 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007144 if (att_beg >= del_from)
7145 att_beg += delta;
7146 if (att_end >= del_from)
7147 att_end += delta;
7148 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007149 val_end += delta;
7150 next += delta;
7151 hdr_end += delta;
7152 hdr_next += delta;
7153 cur_hdr->len += delta;
7154 http_msg_move_end(&txn->req, delta);
7155 prev = del_from;
7156 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007157 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007158 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007160 /* continue with next cookie on this header line */
7161 att_beg = next;
7162 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007164 /* There are no more cookies on this line.
7165 * We may still have one (or several) marked for deletion at the
7166 * end of the line. We must do this now in two ways :
7167 * - if some cookies must be preserved, we only delete from the
7168 * mark to the end of line ;
7169 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007171 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007173 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007174 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007175 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176 cur_hdr->len += delta;
7177 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007178 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007179
7180 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007181 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7182 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007183 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007184 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007185 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007186 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007187 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007188 }
7189
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007190 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007191 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007192 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007193}
7194
7195
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196/* Iterate the same filter through all response headers contained in <rtr>.
7197 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7198 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007199int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007201 char *cur_ptr, *cur_end, *cur_next;
7202 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007203 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02007205 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206
7207 last_hdr = 0;
7208
Willy Tarreauf37954d2018-06-15 18:31:02 +02007209 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007210 old_idx = 0;
7211
7212 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007213 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007215 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007216 (exp->action == ACT_ALLOW ||
7217 exp->action == ACT_DENY))
7218 return 0;
7219
7220 cur_idx = txn->hdr_idx.v[old_idx].next;
7221 if (!cur_idx)
7222 break;
7223
7224 cur_hdr = &txn->hdr_idx.v[cur_idx];
7225 cur_ptr = cur_next;
7226 cur_end = cur_ptr + cur_hdr->len;
7227 cur_next = cur_end + cur_hdr->cr + 1;
7228
7229 /* Now we have one header between cur_ptr and cur_end,
7230 * and the next header starts at cur_next.
7231 */
7232
Willy Tarreau15a53a42015-01-21 13:39:42 +01007233 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007234 switch (exp->action) {
7235 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007236 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 last_hdr = 1;
7238 break;
7239
7240 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007241 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 last_hdr = 1;
7243 break;
7244
7245 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02007246 len = exp_replace(trash.area,
7247 trash.size, cur_ptr,
7248 exp->replace, pmatch);
7249 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06007250 return -1;
7251
Willy Tarreau6e27be12018-08-22 04:46:47 +02007252 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
7253
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 /* FIXME: if the user adds a newline in the replacement, the
7255 * index will not be recalculated for now, and the new line
7256 * will not be counted as a new header.
7257 */
7258
7259 cur_end += delta;
7260 cur_next += delta;
7261 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007262 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263 break;
7264
7265 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007266 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267 cur_next += delta;
7268
Willy Tarreaufa355d42009-11-29 18:12:29 +01007269 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7271 txn->hdr_idx.used--;
7272 cur_hdr->len = 0;
7273 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007274 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 break;
7276
7277 }
7278 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279
7280 /* keep the link from this header to next one in case of later
7281 * removal of next header.
7282 */
7283 old_idx = cur_idx;
7284 }
7285 return 0;
7286}
7287
7288
7289/* Apply the filter to the status line in the response buffer <rtr>.
7290 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7291 * or -1 if a replacement resulted in an invalid status line.
7292 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007293int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295 char *cur_ptr, *cur_end;
7296 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007297 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02007298 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299
Willy Tarreau3d300592007-03-18 18:34:41 +01007300 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007302 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 (exp->action == ACT_ALLOW ||
7304 exp->action == ACT_DENY))
7305 return 0;
7306 else if (exp->action == ACT_REMOVE)
7307 return 0;
7308
7309 done = 0;
7310
Willy Tarreauf37954d2018-06-15 18:31:02 +02007311 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007312 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313
7314 /* Now we have the status line between cur_ptr and cur_end */
7315
Willy Tarreau15a53a42015-01-21 13:39:42 +01007316 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007317 switch (exp->action) {
7318 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007319 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320 done = 1;
7321 break;
7322
7323 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007324 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007325 done = 1;
7326 break;
7327
7328 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02007329 len = exp_replace(trash.area, trash.size,
7330 cur_ptr, exp->replace, pmatch);
7331 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06007332 return -1;
7333
Willy Tarreau6e27be12018-08-22 04:46:47 +02007334 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
7335
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 /* FIXME: if the user adds a newline in the replacement, the
7337 * index will not be recalculated for now, and the new line
7338 * will not be counted as a new header.
7339 */
7340
Willy Tarreaufa355d42009-11-29 18:12:29 +01007341 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007343 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007344 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345 cur_ptr, cur_end + 1,
7346 NULL, NULL);
7347 if (unlikely(!cur_end))
7348 return -1;
7349
7350 /* we have a full respnse and we know that we have either a CR
7351 * or an LF at <ptr>.
7352 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007353 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007354 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 /* there is no point trying this regex on headers */
7356 return 1;
7357 }
7358 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007359 return done;
7360}
7361
7362
7363
7364/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007365 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7367 * unparsable response.
7368 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007369int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370{
Willy Tarreau192252e2015-04-04 01:47:55 +02007371 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007372 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007373 struct hdr_exp *exp;
7374
7375 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 int ret;
7377
7378 /*
7379 * The interleaving of transformations and verdicts
7380 * makes it difficult to decide to continue or stop
7381 * the evaluation.
7382 */
7383
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007384 if (txn->flags & TX_SVDENY)
7385 break;
7386
Willy Tarreau3d300592007-03-18 18:34:41 +01007387 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7389 exp->action == ACT_PASS)) {
7390 exp = exp->next;
7391 continue;
7392 }
7393
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007394 /* if this filter had a condition, evaluate it now and skip to
7395 * next filter if the condition does not match.
7396 */
7397 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007398 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007399 ret = acl_pass(ret);
7400 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7401 ret = !ret;
7402 if (!ret)
7403 continue;
7404 }
7405
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007407 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 if (unlikely(ret < 0))
7409 return -1;
7410
7411 if (likely(ret == 0)) {
7412 /* The filter did not match the response, it can be
7413 * iterated through all headers.
7414 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007415 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7416 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 }
7419 return 0;
7420}
7421
7422
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007424 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007425 * desirable to call it only when needed. This function is also used when we
7426 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007428void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007429{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007430 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007431 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007432 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007433 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007434 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007435 char *hdr_beg, *hdr_end, *hdr_next;
7436 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007437
Willy Tarreaua15645d2007-03-18 16:22:39 +01007438 /* Iterate through the headers.
7439 * we start with the start line.
7440 */
7441 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007442 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443
7444 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7445 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007446 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447
7448 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007449 hdr_beg = hdr_next;
7450 hdr_end = hdr_beg + cur_hdr->len;
7451 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452
Willy Tarreau24581ba2010-08-31 22:39:35 +02007453 /* We have one full header between hdr_beg and hdr_end, and the
7454 * next header starts at hdr_next. We're only interested in
7455 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456 */
7457
Willy Tarreau24581ba2010-08-31 22:39:35 +02007458 is_cookie2 = 0;
7459 prev = hdr_beg + 10;
7460 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007461 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007462 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7463 if (!val) {
7464 old_idx = cur_idx;
7465 continue;
7466 }
7467 is_cookie2 = 1;
7468 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007469 }
7470
Willy Tarreau24581ba2010-08-31 22:39:35 +02007471 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7472 * <prev> points to the colon.
7473 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007474 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475
Willy Tarreau24581ba2010-08-31 22:39:35 +02007476 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7477 * check-cache is enabled) and we are not interested in checking
7478 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007479 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007480 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007481 return;
7482
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 /* OK so now we know we have to process this response cookie.
7484 * The format of the Set-Cookie header is slightly different
7485 * from the format of the Cookie header in that it does not
7486 * support the comma as a cookie delimiter (thus the header
7487 * cannot be folded) because the Expires attribute described in
7488 * the original Netscape's spec may contain an unquoted date
7489 * with a comma inside. We have to live with this because
7490 * many browsers don't support Max-Age and some browsers don't
7491 * support quoted strings. However the Set-Cookie2 header is
7492 * clean.
7493 *
7494 * We have to keep multiple pointers in order to support cookie
7495 * removal at the beginning, middle or end of header without
7496 * corrupting the header (in case of set-cookie2). A special
7497 * pointer, <scav> points to the beginning of the set-cookie-av
7498 * fields after the first semi-colon. The <next> pointer points
7499 * either to the end of line (set-cookie) or next unquoted comma
7500 * (set-cookie2). All of these headers are valid :
7501 *
7502 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7503 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7504 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7505 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7506 * | | | | | | | | | |
7507 * | | | | | | | | +-> next hdr_end <--+
7508 * | | | | | | | +------------> scav
7509 * | | | | | | +--------------> val_end
7510 * | | | | | +--------------------> val_beg
7511 * | | | | +----------------------> equal
7512 * | | | +------------------------> att_end
7513 * | | +----------------------------> att_beg
7514 * | +------------------------------> prev
7515 * +-----------------------------------------> hdr_beg
7516 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007517
Willy Tarreau24581ba2010-08-31 22:39:35 +02007518 for (; prev < hdr_end; prev = next) {
7519 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 /* find att_beg */
7522 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007523 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007524 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525
Willy Tarreau24581ba2010-08-31 22:39:35 +02007526 /* find att_end : this is the first character after the last non
7527 * space before the equal. It may be equal to hdr_end.
7528 */
7529 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530
Willy Tarreau24581ba2010-08-31 22:39:35 +02007531 while (equal < hdr_end) {
7532 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7533 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007534 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007535 continue;
7536 att_end = equal;
7537 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007538
Willy Tarreau24581ba2010-08-31 22:39:35 +02007539 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7540 * is between <att_beg> and <equal>, both may be identical.
7541 */
7542
7543 /* look for end of cookie if there is an equal sign */
7544 if (equal < hdr_end && *equal == '=') {
7545 /* look for the beginning of the value */
7546 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007547 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007548 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549
Willy Tarreau24581ba2010-08-31 22:39:35 +02007550 /* find the end of the value, respecting quotes */
7551 next = find_cookie_value_end(val_beg, hdr_end);
7552
7553 /* make val_end point to the first white space or delimitor after the value */
7554 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007555 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007556 val_end--;
7557 } else {
7558 /* <equal> points to next comma, semi-colon or EOL */
7559 val_beg = val_end = next = equal;
7560 }
7561
7562 if (next < hdr_end) {
7563 /* Set-Cookie2 supports multiple cookies, and <next> points to
7564 * a colon or semi-colon before the end. So skip all attr-value
7565 * pairs and look for the next comma. For Set-Cookie, since
7566 * commas are permitted in values, skip to the end.
7567 */
7568 if (is_cookie2)
7569 next = find_hdr_value_end(next, hdr_end);
7570 else
7571 next = hdr_end;
7572 }
7573
7574 /* Now everything is as on the diagram above */
7575
7576 /* Ignore cookies with no equal sign */
7577 if (equal == val_end)
7578 continue;
7579
7580 /* If there are spaces around the equal sign, we need to
7581 * strip them otherwise we'll get trouble for cookie captures,
7582 * or even for rewrites. Since this happens extremely rarely,
7583 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7586 int stripped_before = 0;
7587 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588
Willy Tarreau24581ba2010-08-31 22:39:35 +02007589 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007590 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007591 equal += stripped_before;
7592 val_beg += stripped_before;
7593 }
7594
7595 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007596 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007597 val_beg += stripped_after;
7598 stripped_before += stripped_after;
7599 }
7600
7601 val_end += stripped_before;
7602 next += stripped_before;
7603 hdr_end += stripped_before;
7604 hdr_next += stripped_before;
7605 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007606 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007607 }
7608
7609 /* First, let's see if we want to capture this cookie. We check
7610 * that we don't already have a server side cookie, because we
7611 * can only capture one. Also as an optimisation, we ignore
7612 * cookies shorter than the declared name.
7613 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007614 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007615 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007616 (val_end - att_beg >= sess->fe->capture_namelen) &&
7617 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007618 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007619 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007620 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007621 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007622 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007623 if (log_len > sess->fe->capture_len)
7624 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007625 memcpy(txn->srv_cookie, att_beg, log_len);
7626 txn->srv_cookie[log_len] = 0;
7627 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628 }
7629
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007630 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007632 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007633 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7634 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007635 /* assume passive cookie by default */
7636 txn->flags &= ~TX_SCK_MASK;
7637 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638
7639 /* If the cookie is in insert mode on a known server, we'll delete
7640 * this occurrence because we'll insert another one later.
7641 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 * a direct access.
7643 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007644 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007645 /* The "preserve" flag was set, we don't want to touch the
7646 * server's cookie.
7647 */
7648 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007649 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007650 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007651 /* this cookie must be deleted */
7652 if (*prev == ':' && next == hdr_end) {
7653 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007654 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007655 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7656 txn->hdr_idx.used--;
7657 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007658 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007659 hdr_next += delta;
7660 http_msg_move_end(&txn->rsp, delta);
7661 /* note: while both invalid now, <next> and <hdr_end>
7662 * are still equal, so the for() will stop as expected.
7663 */
7664 } else {
7665 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007666 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007667 next = prev;
7668 hdr_end += delta;
7669 hdr_next += delta;
7670 cur_hdr->len += delta;
7671 http_msg_move_end(&txn->rsp, delta);
7672 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007673 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007674 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007675 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007677 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007678 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 * with this server since we know it.
7680 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007681 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007682 next += delta;
7683 hdr_end += delta;
7684 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007686 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687
Willy Tarreauf1348312010-10-07 15:54:11 +02007688 txn->flags &= ~TX_SCK_MASK;
7689 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007691 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007693 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007695 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007696 next += delta;
7697 hdr_end += delta;
7698 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007699 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007700 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701
Willy Tarreau827aee92011-03-10 16:55:02 +01007702 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007703 txn->flags &= ~TX_SCK_MASK;
7704 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705 }
7706 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007707 /* that's done for this cookie, check the next one on the same
7708 * line when next != hdr_end (only if is_cookie2).
7709 */
7710 }
7711 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007713 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007714}
7715
7716
Willy Tarreaua15645d2007-03-18 16:22:39 +01007717/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007718 * Parses the Cache-Control and Pragma request header fields to determine if
7719 * the request may be served from the cache and/or if it is cacheable. Updates
7720 * s->txn->flags.
7721 */
7722void check_request_for_cacheability(struct stream *s, struct channel *chn)
7723{
7724 struct http_txn *txn = s->txn;
7725 char *p1, *p2;
7726 char *cur_ptr, *cur_end, *cur_next;
7727 int pragma_found;
7728 int cc_found;
7729 int cur_idx;
7730
7731 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7732 return; /* nothing more to do here */
7733
7734 cur_idx = 0;
7735 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007736 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007737
7738 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7739 struct hdr_idx_elem *cur_hdr;
7740 int val;
7741
7742 cur_hdr = &txn->hdr_idx.v[cur_idx];
7743 cur_ptr = cur_next;
7744 cur_end = cur_ptr + cur_hdr->len;
7745 cur_next = cur_end + cur_hdr->cr + 1;
7746
7747 /* We have one full header between cur_ptr and cur_end, and the
7748 * next header starts at cur_next.
7749 */
7750
7751 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7752 if (val) {
7753 if ((cur_end - (cur_ptr + val) >= 8) &&
7754 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7755 pragma_found = 1;
7756 continue;
7757 }
7758 }
7759
William Lallemand8a16fe02018-05-22 11:04:33 +02007760 /* Don't use the cache and don't try to store if we found the
7761 * Authorization header */
7762 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7763 if (val) {
7764 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7765 txn->flags |= TX_CACHE_IGNORE;
7766 continue;
7767 }
7768
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007769 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7770 if (!val)
7771 continue;
7772
7773 /* OK, right now we know we have a cache-control header at cur_ptr */
7774 cc_found = 1;
7775 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7776
7777 if (p1 >= cur_end) /* no more info */
7778 continue;
7779
7780 /* p1 is at the beginning of the value */
7781 p2 = p1;
7782 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7783 p2++;
7784
7785 /* we have a complete value between p1 and p2. We don't check the
7786 * values after max-age, max-stale nor min-fresh, we simply don't
7787 * use the cache when they're specified.
7788 */
7789 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7790 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7791 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7792 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7793 txn->flags |= TX_CACHE_IGNORE;
7794 continue;
7795 }
7796
7797 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7798 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7799 continue;
7800 }
7801 }
7802
7803 /* RFC7234#5.4:
7804 * When the Cache-Control header field is also present and
7805 * understood in a request, Pragma is ignored.
7806 * When the Cache-Control header field is not present in a
7807 * request, caches MUST consider the no-cache request
7808 * pragma-directive as having the same effect as if
7809 * "Cache-Control: no-cache" were present.
7810 */
7811 if (!cc_found && pragma_found)
7812 txn->flags |= TX_CACHE_IGNORE;
7813}
7814
7815/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007816 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007818void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007819{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007820 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007821 char *p1, *p2;
7822
7823 char *cur_ptr, *cur_end, *cur_next;
7824 int cur_idx;
7825
Willy Tarreau12b32f22017-12-21 16:08:09 +01007826 if (txn->status < 200) {
7827 /* do not try to cache interim responses! */
7828 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007830 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831
7832 /* Iterate through the headers.
7833 * we start with the start line.
7834 */
7835 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007836 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837
7838 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7839 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007840 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841
7842 cur_hdr = &txn->hdr_idx.v[cur_idx];
7843 cur_ptr = cur_next;
7844 cur_end = cur_ptr + cur_hdr->len;
7845 cur_next = cur_end + cur_hdr->cr + 1;
7846
7847 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007848 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 */
7850
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007851 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7852 if (val) {
7853 if ((cur_end - (cur_ptr + val) >= 8) &&
7854 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7855 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7856 return;
7857 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 }
7859
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007860 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7861 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 continue;
7863
7864 /* OK, right now we know we have a cache-control header at cur_ptr */
7865
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007866 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867
7868 if (p1 >= cur_end) /* no more info */
7869 continue;
7870
7871 /* p1 is at the beginning of the value */
7872 p2 = p1;
7873
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007874 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007875 p2++;
7876
7877 /* we have a complete value between p1 and p2 */
7878 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007879 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7880 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7881 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7882 continue;
7883 }
7884
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885 /* we have something of the form no-cache="set-cookie" */
7886 if ((cur_end - p1 >= 21) &&
7887 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7888 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007889 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 continue;
7891 }
7892
7893 /* OK, so we know that either p2 points to the end of string or to a comma */
7894 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007895 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007896 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007897 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898 return;
7899 }
7900
7901 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007902 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007903 continue;
7904 }
7905 }
7906}
7907
Willy Tarreau58f10d72006-12-04 02:26:12 +01007908
Willy Tarreaub2513902006-12-17 14:52:38 +01007909/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007910 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007911 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007912 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007913 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007914 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007915 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007916 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007917 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007918int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007919{
7920 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007921 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007922 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007923
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007924 if (!uri_auth)
7925 return 0;
7926
Cyril Bonté70be45d2010-10-12 00:14:35 +02007927 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007928 return 0;
7929
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007930 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007931 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007932 return 0;
7933
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007934 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007935 return 0;
7936
Willy Tarreaub2513902006-12-17 14:52:38 +01007937 return 1;
7938}
7939
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007940/* Append the description of what is present in error snapshot <es> into <out>.
7941 * The description must be small enough to always fit in a trash. The output
7942 * buffer may be the trash so the trash must not be used inside this function.
7943 */
7944void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7945{
7946 chunk_appendf(&trash,
7947 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7948 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7949 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7950 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7951 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7952 es->ctx.http.m_flags, es->ctx.http.m_clen,
7953 es->ctx.http.m_blen, es->ctx.http.b_flags);
7954}
7955
Willy Tarreau4076a152009-04-02 15:18:36 +02007956/*
7957 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007958 * By default it tries to report the error position as msg->err_pos. However if
7959 * this one is not set, it will then report msg->next, which is the last known
7960 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007961 * displays buffers as a contiguous area starting at buf->p. The direction is
7962 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007963 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007964void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007965 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007966 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007967{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007968 union error_snapshot_ctx ctx;
7969 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007970
Willy Tarreauef3ca732018-09-07 15:47:35 +02007971 /* http-specific part now */
7972 ctx.http.sid = s->uniq_id;
7973 ctx.http.state = state;
7974 ctx.http.b_flags = msg->chn->flags;
7975 ctx.http.s_flags = s->flags;
7976 ctx.http.t_flags = s->txn->flags;
7977 ctx.http.m_flags = msg->flags;
7978 ctx.http.m_clen = msg->chunk_len;
7979 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007980
Willy Tarreauef3ca732018-09-07 15:47:35 +02007981 ofs = msg->chn->total - ci_data(msg->chn);
7982 if (ofs < 0)
7983 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007984
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007985 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007986 other_end, s->target,
7987 strm_sess(s), &msg->chn->buf,
7988 ofs, co_data(msg->chn),
7989 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7990 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007991}
Willy Tarreaub2513902006-12-17 14:52:38 +01007992
Willy Tarreau294c4732011-12-16 21:35:50 +01007993/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7994 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7995 * performed over the whole headers. Otherwise it must contain a valid header
7996 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7997 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7998 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7999 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008000 * -1. The value fetch stops at commas, so this function is suited for use with
8001 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008002 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008003 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008004unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008005 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008006 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008007{
Willy Tarreau294c4732011-12-16 21:35:50 +01008008 struct hdr_ctx local_ctx;
8009 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008010 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008011 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008012 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008013
Willy Tarreau294c4732011-12-16 21:35:50 +01008014 if (!ctx) {
8015 local_ctx.idx = 0;
8016 ctx = &local_ctx;
8017 }
8018
Willy Tarreaubce70882009-09-07 11:51:47 +02008019 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008020 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008021 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008022 occ--;
8023 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008024 *vptr = ctx->line + ctx->val;
8025 *vlen = ctx->vlen;
8026 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008027 }
8028 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008029 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008030 }
8031
8032 /* negative occurrence, we scan all the list then walk back */
8033 if (-occ > MAX_HDR_HISTORY)
8034 return 0;
8035
Willy Tarreau294c4732011-12-16 21:35:50 +01008036 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008037 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008038 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8039 len_hist[hist_ptr] = ctx->vlen;
8040 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008041 hist_ptr = 0;
8042 found++;
8043 }
8044 if (-occ > found)
8045 return 0;
8046 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008047 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8048 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8049 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008050 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008051 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008052 if (hist_ptr >= MAX_HDR_HISTORY)
8053 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008054 *vptr = ptr_hist[hist_ptr];
8055 *vlen = len_hist[hist_ptr];
8056 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008057}
8058
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008059/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8060 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8061 * performed over the whole headers. Otherwise it must contain a valid header
8062 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8063 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8064 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8065 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8066 * -1. This function differs from http_get_hdr() in that it only returns full
8067 * line header values and does not stop at commas.
8068 * The return value is 0 if nothing was found, or non-zero otherwise.
8069 */
8070unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8071 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008072 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008073{
8074 struct hdr_ctx local_ctx;
8075 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008076 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008077 unsigned int hist_ptr;
8078 int found;
8079
8080 if (!ctx) {
8081 local_ctx.idx = 0;
8082 ctx = &local_ctx;
8083 }
8084
8085 if (occ >= 0) {
8086 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008087 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008088 occ--;
8089 if (occ <= 0) {
8090 *vptr = ctx->line + ctx->val;
8091 *vlen = ctx->vlen;
8092 return 1;
8093 }
8094 }
8095 return 0;
8096 }
8097
8098 /* negative occurrence, we scan all the list then walk back */
8099 if (-occ > MAX_HDR_HISTORY)
8100 return 0;
8101
8102 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02008103 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008104 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8105 len_hist[hist_ptr] = ctx->vlen;
8106 if (++hist_ptr >= MAX_HDR_HISTORY)
8107 hist_ptr = 0;
8108 found++;
8109 }
8110 if (-occ > found)
8111 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008112
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008113 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008114 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8115 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8116 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008117 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008118 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008119 if (hist_ptr >= MAX_HDR_HISTORY)
8120 hist_ptr -= MAX_HDR_HISTORY;
8121 *vptr = ptr_hist[hist_ptr];
8122 *vlen = len_hist[hist_ptr];
8123 return 1;
8124}
8125
Willy Tarreaubaaee002006-06-26 02:48:02 +02008126/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008127 * Print a debug line with a header. Always stop at the first CR or LF char,
8128 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8129 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008130 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008131void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008132{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008133 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008134 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008135
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008136 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008137 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008138 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008139 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 +02008140
8141 for (max = 0; start + max < end; max++)
8142 if (start[max] == '\r' || start[max] == '\n')
8143 break;
8144
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008145 UBOUND(max, trash.size - trash.data - 3);
8146 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
8147 trash.area[trash.data++] = '\n';
8148 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008149}
8150
Willy Tarreaueee5b512015-04-03 23:46:31 +02008151
8152/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8153 * The hdr_idx is allocated as well. In case of allocation failure, everything
8154 * allocated is freed and NULL is returned. Otherwise the new transaction is
8155 * assigned to the stream and returned.
8156 */
8157struct http_txn *http_alloc_txn(struct stream *s)
8158{
8159 struct http_txn *txn = s->txn;
8160
8161 if (txn)
8162 return txn;
8163
Willy Tarreaubafbe012017-11-24 17:34:44 +01008164 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008165 if (!txn)
8166 return txn;
8167
8168 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008169 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008170 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008171 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008172 return NULL;
8173 }
8174
8175 s->txn = txn;
8176 return txn;
8177}
8178
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008179void http_txn_reset_req(struct http_txn *txn)
8180{
8181 txn->req.flags = 0;
8182 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8183 txn->req.next = 0;
8184 txn->req.chunk_len = 0LL;
8185 txn->req.body_len = 0LL;
8186 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8187}
8188
8189void http_txn_reset_res(struct http_txn *txn)
8190{
8191 txn->rsp.flags = 0;
8192 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8193 txn->rsp.next = 0;
8194 txn->rsp.chunk_len = 0LL;
8195 txn->rsp.body_len = 0LL;
8196 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8197}
8198
Willy Tarreau0937bc42009-12-22 15:03:09 +01008199/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008200 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008201 * the required fields are properly allocated and that we only need to (re)init
8202 * them. This should be used before processing any new request.
8203 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008204void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008205{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008206 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008207 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008208
8209 txn->flags = 0;
8210 txn->status = -1;
8211
Willy Tarreauf64d1412010-10-07 20:06:11 +02008212 txn->cookie_first_date = 0;
8213 txn->cookie_last_date = 0;
8214
Willy Tarreaueee5b512015-04-03 23:46:31 +02008215 txn->srv_cookie = NULL;
8216 txn->cli_cookie = NULL;
8217 txn->uri = NULL;
8218
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008219 http_txn_reset_req(txn);
8220 http_txn_reset_res(txn);
8221
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008222 txn->req.chn = &s->req;
8223 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008224
8225 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008226
8227 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8228 if (fe->options2 & PR_O2_REQBUG_OK)
8229 txn->req.err_pos = -1; /* let buggy requests pass */
8230
Willy Tarreau0937bc42009-12-22 15:03:09 +01008231 if (txn->hdr_idx.v)
8232 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008233
8234 vars_init(&s->vars_txn, SCOPE_TXN);
8235 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008236}
8237
8238/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008239void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008240{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008241 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008242 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008243
8244 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008245 pool_free(pool_head_requri, txn->uri);
8246 pool_free(pool_head_capture, txn->cli_cookie);
8247 pool_free(pool_head_capture, txn->srv_cookie);
8248 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008249
William Lallemanda73203e2012-03-12 12:48:57 +01008250 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008251 txn->uri = NULL;
8252 txn->srv_cookie = NULL;
8253 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008254
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008255 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008256 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008257 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008258 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008259 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008260 }
8261
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008262 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008263 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008264 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008265 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008266 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008267 }
8268
Willy Tarreau6204cd92016-03-10 16:33:04 +01008269 vars_prune(&s->vars_txn, s->sess, s);
8270 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008271}
8272
8273/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008274void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008275{
8276 http_end_txn(s);
8277 http_init_txn(s);
8278
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008279 /* reinitialise the current rule list pointer to NULL. We are sure that
8280 * any rulelist match the NULL pointer.
8281 */
8282 s->current_rule_list = NULL;
8283
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008284 s->be = strm_fe(s);
8285 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008286 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008287 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008288 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008289 /* re-init store persistence */
8290 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02008291 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01008292
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008293 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008294
Willy Tarreau739cfba2010-01-25 23:11:14 +01008295 /* We must trim any excess data from the response buffer, because we
8296 * may have blocked an invalid response from a server that we don't
8297 * want to accidentely forward once we disable the analysers, nor do
8298 * we want those data to come along with next response. A typical
8299 * example of such data would be from a buggy server responding to
8300 * a HEAD with some data, or sending more than the advertised
8301 * content-length.
8302 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008303 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008304 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01008305
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008306 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02008307 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008308
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008309 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008310 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008311
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008312 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008313 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008314
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008315 s->req.rex = TICK_ETERNITY;
8316 s->req.wex = TICK_ETERNITY;
8317 s->req.analyse_exp = TICK_ETERNITY;
8318 s->res.rex = TICK_ETERNITY;
8319 s->res.wex = TICK_ETERNITY;
8320 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008321 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008322}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008323
Sasha Pachev218f0642014-06-16 12:05:59 -06008324void free_http_res_rules(struct list *r)
8325{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008326 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008327
8328 list_for_each_entry_safe(pr, tr, r, list) {
8329 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008330 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008331 free(pr);
8332 }
8333}
8334
8335void free_http_req_rules(struct list *r)
8336{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008337 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008338
8339 list_for_each_entry_safe(pr, tr, r, list) {
8340 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008341 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008342 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008343
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008344 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008345 free(pr);
8346 }
8347}
8348
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008349/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008350struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008351{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008352 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008353 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008354 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008355 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008356
Vincent Bernat02779b62016-04-03 13:48:43 +02008357 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008358 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008359 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008360 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008361 }
8362
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008363 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008364 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008365 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008366 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008367 int code;
8368 int hc;
8369
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008370 if (!strcmp(args[0], "tarpit")) {
8371 rule->action = ACT_HTTP_REQ_TARPIT;
8372 rule->deny_status = HTTP_ERR_500;
8373 }
8374 else {
8375 rule->action = ACT_ACTION_DENY;
8376 rule->deny_status = HTTP_ERR_403;
8377 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008378 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008379 if (strcmp(args[cur_arg], "deny_status") == 0) {
8380 cur_arg++;
8381 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008382 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8383 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008384 goto out_err;
8385 }
8386
8387 code = atol(args[cur_arg]);
8388 cur_arg++;
8389 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8390 if (http_err_codes[hc] == code) {
8391 rule->deny_status = hc;
8392 break;
8393 }
8394 }
8395
8396 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008397 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8398 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008399 }
8400 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008401 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008402 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008403 cur_arg = 1;
8404
8405 while(*args[cur_arg]) {
8406 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008407 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008408 cur_arg+=2;
8409 continue;
8410 } else
8411 break;
8412 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008413 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008414 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008415 cur_arg = 1;
8416
8417 if (!*args[cur_arg] ||
8418 (*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 +01008419 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8420 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008421 goto out_err;
8422 }
8423 rule->arg.nice = atoi(args[cur_arg]);
8424 if (rule->arg.nice < -1024)
8425 rule->arg.nice = -1024;
8426 else if (rule->arg.nice > 1024)
8427 rule->arg.nice = 1024;
8428 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008429 } else if (!strcmp(args[0], "set-tos")) {
8430#ifdef IP_TOS
8431 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008432 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008433 cur_arg = 1;
8434
8435 if (!*args[cur_arg] ||
8436 (*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 +01008437 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8438 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008439 goto out_err;
8440 }
8441
8442 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8443 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008444 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8445 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008446 goto out_err;
8447 }
8448 cur_arg++;
8449#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008450 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 +02008451 goto out_err;
8452#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008453 } else if (!strcmp(args[0], "set-mark")) {
8454#ifdef SO_MARK
8455 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008456 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008457 cur_arg = 1;
8458
8459 if (!*args[cur_arg] ||
8460 (*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 +01008461 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8462 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008463 goto out_err;
8464 }
8465
8466 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8467 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008468 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8469 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008470 goto out_err;
8471 }
8472 cur_arg++;
8473 global.last_checks |= LSTCHK_NETADM;
8474#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008475 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 +02008476 goto out_err;
8477#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008478 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008479 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008480 cur_arg = 1;
8481
8482 if (!*args[cur_arg] ||
8483 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8484 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008485 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8486 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008487 goto out_err;
8488 }
8489 if (strcmp(args[cur_arg], "silent") == 0)
8490 rule->arg.loglevel = -1;
8491 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8492 goto bad_log_level;
8493 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008494 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008495 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008496 cur_arg = 1;
8497
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008498 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8499 (*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 +01008500 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8501 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008502 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008503 }
8504
8505 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8506 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8507 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008508
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008509 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008510 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008511 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 +01008512 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008513 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8514 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008515 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008516 goto out_err;
8517 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008518 free(proxy->conf.lfs_file);
8519 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8520 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008521 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008522 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008523 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008524 cur_arg = 1;
8525
8526 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008527 (*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 +01008528 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8529 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008530 goto out_err;
8531 }
8532
8533 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8534 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8535 LIST_INIT(&rule->arg.hdr_add.fmt);
8536
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008537 error = NULL;
8538 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008539 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8540 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008541 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008542 goto out_err;
8543 }
8544
8545 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008546 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008547 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 +01008548 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008549 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8550 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008551 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008552 goto out_err;
8553 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008554
8555 free(proxy->conf.lfs_file);
8556 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8557 proxy->conf.lfs_line = proxy->conf.args.line;
8558 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008559 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008560 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008561 cur_arg = 1;
8562
8563 if (!*args[cur_arg] ||
8564 (*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 +01008565 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8566 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008567 goto out_err;
8568 }
8569
8570 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8571 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8572
8573 proxy->conf.args.ctx = ARGC_HRQ;
8574 free(proxy->conf.lfs_file);
8575 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8576 proxy->conf.lfs_line = proxy->conf.args.line;
8577 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008578 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008579 struct sample_expr *expr;
8580 unsigned int where;
8581 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008582 unsigned int tsc_num;
8583 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008584
8585 cur_arg = 1;
8586 proxy->conf.args.ctx = ARGC_TRK;
8587
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008588 tsc_num_str = &args[0][8];
8589 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8590 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8591 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8592 free(err);
8593 goto out_err;
8594 }
8595
Willy Tarreau09448f72014-06-25 18:12:15 +02008596 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8597 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008598 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8599 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008600 free(err);
8601 goto out_err;
8602 }
8603
8604 where = 0;
8605 if (proxy->cap & PR_CAP_FE)
8606 where |= SMP_VAL_FE_HRQ_HDR;
8607 if (proxy->cap & PR_CAP_BE)
8608 where |= SMP_VAL_BE_HRQ_HDR;
8609
8610 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008611 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8612 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8613 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8614 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008615 free(expr);
8616 goto out_err;
8617 }
8618
8619 if (strcmp(args[cur_arg], "table") == 0) {
8620 cur_arg++;
8621 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008622 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8623 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008624 free(expr);
8625 goto out_err;
8626 }
8627 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008628 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008629 cur_arg++;
8630 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008631 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008632 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008633 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008634 } else if (strcmp(args[0], "redirect") == 0) {
8635 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008636 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008637
Willy Tarreaube4653b2015-05-28 15:26:58 +02008638 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008639 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8640 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008641 goto out_err;
8642 }
8643
8644 /* this redirect rule might already contain a parsed condition which
8645 * we'll pass to the http-request rule.
8646 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008647 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008648 rule->arg.redir = redir;
8649 rule->cond = redir->cond;
8650 redir->cond = NULL;
8651 cur_arg = 2;
8652 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008653 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8654 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008655 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008656 /*
8657 * '+ 8' for 'add-acl('
8658 * '- 9' for 'add-acl(' + trailing ')'
8659 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008660 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008661
8662 cur_arg = 1;
8663
8664 if (!*args[cur_arg] ||
8665 (*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 +01008666 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8667 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008668 goto out_err;
8669 }
8670
8671 LIST_INIT(&rule->arg.map.key);
8672 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008673 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008674 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008675 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008676 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8677 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008678 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008679 goto out_err;
8680 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008681 free(proxy->conf.lfs_file);
8682 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8683 proxy->conf.lfs_line = proxy->conf.args.line;
8684 cur_arg += 1;
8685 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8686 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008687 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008688 /*
8689 * '+ 8' for 'del-acl('
8690 * '- 9' for 'del-acl(' + trailing ')'
8691 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008692 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008693
8694 cur_arg = 1;
8695
8696 if (!*args[cur_arg] ||
8697 (*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 +01008698 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8699 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008700 goto out_err;
8701 }
8702
8703 LIST_INIT(&rule->arg.map.key);
8704 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008705 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008706 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008707 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008708 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8709 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008710 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008711 goto out_err;
8712 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008713 free(proxy->conf.lfs_file);
8714 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8715 proxy->conf.lfs_line = proxy->conf.args.line;
8716 cur_arg += 1;
8717 } else if (strncmp(args[0], "del-map", 7) == 0) {
8718 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008719 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008720 /*
8721 * '+ 8' for 'del-map('
8722 * '- 9' for 'del-map(' + trailing ')'
8723 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008724 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008725
8726 cur_arg = 1;
8727
8728 if (!*args[cur_arg] ||
8729 (*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 +01008730 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8731 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008732 goto out_err;
8733 }
8734
8735 LIST_INIT(&rule->arg.map.key);
8736 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008737 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008738 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008739 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008740 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8741 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008742 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008743 goto out_err;
8744 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008745 free(proxy->conf.lfs_file);
8746 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8747 proxy->conf.lfs_line = proxy->conf.args.line;
8748 cur_arg += 1;
8749 } else if (strncmp(args[0], "set-map", 7) == 0) {
8750 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008751 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008752 /*
8753 * '+ 8' for 'set-map('
8754 * '- 9' for 'set-map(' + trailing ')'
8755 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008756 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008757
8758 cur_arg = 1;
8759
8760 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8761 (*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 +01008762 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8763 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008764 goto out_err;
8765 }
8766
8767 LIST_INIT(&rule->arg.map.key);
8768 LIST_INIT(&rule->arg.map.value);
8769 proxy->conf.args.ctx = ARGC_HRQ;
8770
8771 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008772 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008773 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008774 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008775 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8776 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008777 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008778 goto out_err;
8779 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008780
8781 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008782 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008783 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 +01008784 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008785 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8786 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008787 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008788 goto out_err;
8789 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008790 free(proxy->conf.lfs_file);
8791 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8792 proxy->conf.lfs_line = proxy->conf.args.line;
8793
8794 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008795 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8796 char *errmsg = NULL;
8797 cur_arg = 1;
8798 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008799 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008800 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008801 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008802 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8803 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008804 free(errmsg);
8805 goto out_err;
8806 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008807 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008808 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008809 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8810 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8811 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8812 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008813 file, linenum, *trash.area ? ", " : "", trash.area,
8814 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008815 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008816 }
8817
8818 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8819 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008820 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008821
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008822 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008823 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8824 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008825 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008826 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008827 }
8828 rule->cond = cond;
8829 }
8830 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008831 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8832 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8833 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008834 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008835 }
8836
8837 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008838 out_err:
8839 free(rule);
8840 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008841}
8842
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008843/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008844struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008845{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008846 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008847 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008848 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008849 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008850
8851 rule = calloc(1, sizeof(*rule));
8852 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008853 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008854 goto out_err;
8855 }
8856
8857 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008858 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008859 cur_arg = 1;
8860 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008861 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008862 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008863 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008864 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008865 cur_arg = 1;
8866
8867 if (!*args[cur_arg] ||
8868 (*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 +01008869 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8870 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008871 goto out_err;
8872 }
8873 rule->arg.nice = atoi(args[cur_arg]);
8874 if (rule->arg.nice < -1024)
8875 rule->arg.nice = -1024;
8876 else if (rule->arg.nice > 1024)
8877 rule->arg.nice = 1024;
8878 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008879 } else if (!strcmp(args[0], "set-tos")) {
8880#ifdef IP_TOS
8881 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008882 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008883 cur_arg = 1;
8884
8885 if (!*args[cur_arg] ||
8886 (*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 +01008887 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8888 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008889 goto out_err;
8890 }
8891
8892 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8893 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008894 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8895 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008896 goto out_err;
8897 }
8898 cur_arg++;
8899#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008900 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 +02008901 goto out_err;
8902#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008903 } else if (!strcmp(args[0], "set-mark")) {
8904#ifdef SO_MARK
8905 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008906 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008907 cur_arg = 1;
8908
8909 if (!*args[cur_arg] ||
8910 (*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 +01008911 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8912 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008913 goto out_err;
8914 }
8915
8916 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8917 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008918 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8919 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008920 goto out_err;
8921 }
8922 cur_arg++;
8923 global.last_checks |= LSTCHK_NETADM;
8924#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008925 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 +02008926 goto out_err;
8927#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008928 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008929 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008930 cur_arg = 1;
8931
8932 if (!*args[cur_arg] ||
8933 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8934 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008935 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8936 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008937 goto out_err;
8938 }
8939 if (strcmp(args[cur_arg], "silent") == 0)
8940 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008941 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008942 goto bad_log_level;
8943 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008944 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008945 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008946 cur_arg = 1;
8947
8948 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8949 (*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 +01008950 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8951 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008952 goto out_err;
8953 }
8954
8955 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8956 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8957 LIST_INIT(&rule->arg.hdr_add.fmt);
8958
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008959 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008960 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008961 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 +01008962 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008963 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8964 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008965 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008966 goto out_err;
8967 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008968 free(proxy->conf.lfs_file);
8969 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8970 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008971 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008972 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008973 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008974 cur_arg = 1;
8975
8976 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008977 (*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 +01008978 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8979 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008980 goto out_err;
8981 }
8982
8983 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8984 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8985 LIST_INIT(&rule->arg.hdr_add.fmt);
8986
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008987 error = NULL;
8988 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008989 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8990 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008991 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008992 goto out_err;
8993 }
8994
8995 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008996 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008997 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 +01008998 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008999 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9000 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009001 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009002 goto out_err;
9003 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009004
9005 free(proxy->conf.lfs_file);
9006 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9007 proxy->conf.lfs_line = proxy->conf.args.line;
9008 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009009 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009010 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009011 cur_arg = 1;
9012
9013 if (!*args[cur_arg] ||
9014 (*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 +01009015 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9016 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009017 goto out_err;
9018 }
9019
9020 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9021 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9022
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009023 proxy->conf.args.ctx = ARGC_HRS;
9024 free(proxy->conf.lfs_file);
9025 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9026 proxy->conf.lfs_line = proxy->conf.args.line;
9027 cur_arg += 1;
9028 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9029 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009030 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009031 /*
9032 * '+ 8' for 'add-acl('
9033 * '- 9' for 'add-acl(' + trailing ')'
9034 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009035 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009036
9037 cur_arg = 1;
9038
9039 if (!*args[cur_arg] ||
9040 (*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 +01009041 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9042 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009043 goto out_err;
9044 }
9045
9046 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009047 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009048 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009049 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009050 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009051 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9052 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009053 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009054 goto out_err;
9055 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009056 free(proxy->conf.lfs_file);
9057 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9058 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009059
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009060 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009061 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9062 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009063 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009064 /*
9065 * '+ 8' for 'del-acl('
9066 * '- 9' for 'del-acl(' + trailing ')'
9067 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009068 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009069
9070 cur_arg = 1;
9071
9072 if (!*args[cur_arg] ||
9073 (*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 +01009074 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9075 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009076 goto out_err;
9077 }
9078
9079 LIST_INIT(&rule->arg.map.key);
9080 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009081 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009082 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009083 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009084 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9085 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009086 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009087 goto out_err;
9088 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009089 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 += 1;
9093 } else if (strncmp(args[0], "del-map", 7) == 0) {
9094 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009095 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009096 /*
9097 * '+ 8' for 'del-map('
9098 * '- 9' for 'del-map(' + trailing ')'
9099 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009100 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009101
9102 cur_arg = 1;
9103
9104 if (!*args[cur_arg] ||
9105 (*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 +01009106 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9107 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009108 goto out_err;
9109 }
9110
9111 LIST_INIT(&rule->arg.map.key);
9112 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009113 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009114 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009115 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009116 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9117 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009118 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009119 goto out_err;
9120 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009121 free(proxy->conf.lfs_file);
9122 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9123 proxy->conf.lfs_line = proxy->conf.args.line;
9124 cur_arg += 1;
9125 } else if (strncmp(args[0], "set-map", 7) == 0) {
9126 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009127 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009128 /*
9129 * '+ 8' for 'set-map('
9130 * '- 9' for 'set-map(' + trailing ')'
9131 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009132 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009133
9134 cur_arg = 1;
9135
9136 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9137 (*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 +01009138 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9139 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009140 goto out_err;
9141 }
9142
9143 LIST_INIT(&rule->arg.map.key);
9144 LIST_INIT(&rule->arg.map.value);
9145
9146 proxy->conf.args.ctx = ARGC_HRS;
9147
9148 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009149 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009150 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009151 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009152 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9153 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009154 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009155 goto out_err;
9156 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009157
9158 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009159 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009160 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 +01009161 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009162 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9163 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009164 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009165 goto out_err;
9166 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009167
9168 free(proxy->conf.lfs_file);
9169 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9170 proxy->conf.lfs_line = proxy->conf.args.line;
9171
9172 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009173 } else if (strcmp(args[0], "redirect") == 0) {
9174 struct redirect_rule *redir;
9175 char *errmsg = NULL;
9176
9177 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009178 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9179 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009180 goto out_err;
9181 }
9182
9183 /* this redirect rule might already contain a parsed condition which
9184 * we'll pass to the http-request rule.
9185 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009186 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009187 rule->arg.redir = redir;
9188 rule->cond = redir->cond;
9189 redir->cond = NULL;
9190 cur_arg = 2;
9191 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009192 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009193 struct sample_expr *expr;
9194 unsigned int where;
9195 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009196 unsigned int tsc_num;
9197 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009198
9199 cur_arg = 1;
9200 proxy->conf.args.ctx = ARGC_TRK;
9201
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009202 tsc_num_str = &args[0][8];
9203 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9204 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9205 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9206 free(err);
9207 goto out_err;
9208 }
9209
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009210 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9211 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009212 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9213 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009214 free(err);
9215 goto out_err;
9216 }
9217
9218 where = 0;
9219 if (proxy->cap & PR_CAP_FE)
9220 where |= SMP_VAL_FE_HRS_HDR;
9221 if (proxy->cap & PR_CAP_BE)
9222 where |= SMP_VAL_BE_HRS_HDR;
9223
9224 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009225 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9226 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9227 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9228 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009229 free(expr);
9230 goto out_err;
9231 }
9232
9233 if (strcmp(args[cur_arg], "table") == 0) {
9234 cur_arg++;
9235 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009236 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9237 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009238 free(expr);
9239 goto out_err;
9240 }
9241 /* we copy the table name for now, it will be resolved later */
9242 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9243 cur_arg++;
9244 }
9245 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009246 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009247 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009248 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9249 char *errmsg = NULL;
9250 cur_arg = 1;
9251 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009252 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009253 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009254 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009255 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9256 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009257 free(errmsg);
9258 goto out_err;
9259 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009260 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009261 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009262 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9263 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9264 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9265 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009266 file, linenum, *trash.area ? ", " : "", trash.area,
9267 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009268 goto out_err;
9269 }
9270
9271 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9272 struct acl_cond *cond;
9273 char *errmsg = NULL;
9274
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009275 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009276 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9277 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009278 free(errmsg);
9279 goto out_err;
9280 }
9281 rule->cond = cond;
9282 }
9283 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009284 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9285 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9286 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009287 goto out_err;
9288 }
9289
9290 return rule;
9291 out_err:
9292 free(rule);
9293 return NULL;
9294}
9295
Willy Tarreau4baae242012-12-27 12:00:31 +01009296/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009297 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009298 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9299 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009300 */
9301struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009302 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009303{
9304 struct redirect_rule *rule;
9305 int cur_arg;
9306 int type = REDIRECT_TYPE_NONE;
9307 int code = 302;
9308 const char *destination = NULL;
9309 const char *cookie = NULL;
9310 int cookie_set = 0;
9311 unsigned int flags = REDIRECT_FLAG_NONE;
9312 struct acl_cond *cond = NULL;
9313
9314 cur_arg = 0;
9315 while (*(args[cur_arg])) {
9316 if (strcmp(args[cur_arg], "location") == 0) {
9317 if (!*args[cur_arg + 1])
9318 goto missing_arg;
9319
9320 type = REDIRECT_TYPE_LOCATION;
9321 cur_arg++;
9322 destination = args[cur_arg];
9323 }
9324 else if (strcmp(args[cur_arg], "prefix") == 0) {
9325 if (!*args[cur_arg + 1])
9326 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009327 type = REDIRECT_TYPE_PREFIX;
9328 cur_arg++;
9329 destination = args[cur_arg];
9330 }
9331 else if (strcmp(args[cur_arg], "scheme") == 0) {
9332 if (!*args[cur_arg + 1])
9333 goto missing_arg;
9334
9335 type = REDIRECT_TYPE_SCHEME;
9336 cur_arg++;
9337 destination = args[cur_arg];
9338 }
9339 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9340 if (!*args[cur_arg + 1])
9341 goto missing_arg;
9342
9343 cur_arg++;
9344 cookie = args[cur_arg];
9345 cookie_set = 1;
9346 }
9347 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9348 if (!*args[cur_arg + 1])
9349 goto missing_arg;
9350
9351 cur_arg++;
9352 cookie = args[cur_arg];
9353 cookie_set = 0;
9354 }
9355 else if (strcmp(args[cur_arg], "code") == 0) {
9356 if (!*args[cur_arg + 1])
9357 goto missing_arg;
9358
9359 cur_arg++;
9360 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009361 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009362 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009363 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009364 args[cur_arg - 1], args[cur_arg]);
9365 return NULL;
9366 }
9367 }
9368 else if (!strcmp(args[cur_arg],"drop-query")) {
9369 flags |= REDIRECT_FLAG_DROP_QS;
9370 }
9371 else if (!strcmp(args[cur_arg],"append-slash")) {
9372 flags |= REDIRECT_FLAG_APPEND_SLASH;
9373 }
9374 else if (strcmp(args[cur_arg], "if") == 0 ||
9375 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009376 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009377 if (!cond) {
9378 memprintf(errmsg, "error in condition: %s", *errmsg);
9379 return NULL;
9380 }
9381 break;
9382 }
9383 else {
9384 memprintf(errmsg,
9385 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9386 args[cur_arg]);
9387 return NULL;
9388 }
9389 cur_arg++;
9390 }
9391
9392 if (type == REDIRECT_TYPE_NONE) {
9393 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9394 return NULL;
9395 }
9396
Willy Tarreaube4653b2015-05-28 15:26:58 +02009397 if (dir && type != REDIRECT_TYPE_LOCATION) {
9398 memprintf(errmsg, "response only supports redirect type 'location'");
9399 return NULL;
9400 }
9401
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009402 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009403 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009404 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009405
9406 if (!use_fmt) {
9407 /* old-style static redirect rule */
9408 rule->rdr_str = strdup(destination);
9409 rule->rdr_len = strlen(destination);
9410 }
9411 else {
9412 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009413
9414 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9415 * if prefix == "/", we don't want to add anything, otherwise it
9416 * makes it hard for the user to configure a self-redirection.
9417 */
Godbachd9722032014-12-18 15:44:58 +08009418 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009419 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009420 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9421 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 +01009422 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9423 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009424 return NULL;
9425 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009426 free(curproxy->conf.lfs_file);
9427 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9428 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009429 }
9430 }
9431
Willy Tarreau4baae242012-12-27 12:00:31 +01009432 if (cookie) {
9433 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9434 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9435 */
9436 rule->cookie_len = strlen(cookie);
9437 if (cookie_set) {
9438 rule->cookie_str = malloc(rule->cookie_len + 10);
9439 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9440 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9441 rule->cookie_len += 9;
9442 } else {
9443 rule->cookie_str = malloc(rule->cookie_len + 21);
9444 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9445 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9446 rule->cookie_len += 20;
9447 }
9448 }
9449 rule->type = type;
9450 rule->code = code;
9451 rule->flags = flags;
9452 LIST_INIT(&rule->list);
9453 return rule;
9454
9455 missing_arg:
9456 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9457 return NULL;
9458}
9459
Willy Tarreau8797c062007-05-07 00:55:35 +02009460/************************************************************************/
9461/* The code below is dedicated to ACL parsing and matching */
9462/************************************************************************/
9463
9464
Willy Tarreau14174bc2012-04-16 14:34:04 +02009465/* This function ensures that the prerequisites for an L7 fetch are ready,
9466 * which means that a request or response is ready. If some data is missing,
9467 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009468 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9469 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009470 *
9471 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009472 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9473 * decide whether or not an HTTP message is present ;
9474 * 0 if the requested data cannot be fetched or if it is certain that
9475 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009476 * 1 if an HTTP message is ready
9477 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009478int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009479 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009480{
Willy Tarreau192252e2015-04-04 01:47:55 +02009481 struct http_txn *txn;
9482 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009483
Willy Tarreaube508f12016-03-10 11:47:01 +01009484 /* Note: it is possible that <s> is NULL when called before stream
9485 * initialization (eg: tcp-request connection), so this function is the
9486 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009487 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009488 if (!s)
9489 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009490
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009491 if (!s->txn) {
9492 if (unlikely(!http_alloc_txn(s)))
9493 return 0; /* not enough memory */
9494 http_init_txn(s);
9495 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009496 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009497 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009498
9499 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009500 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009501
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009502 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009503 /* If the buffer does not leave enough free space at the end,
9504 * we must first realign it.
9505 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009506 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
9507 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009508 channel_slow_realign(&s->req, trash.area);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009509
Willy Tarreau14174bc2012-04-16 14:34:04 +02009510 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009511 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009512 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009513
9514 /* Try to decode HTTP request */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009515 if (likely(msg->next < ci_data(&s->req)))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009516 http_msg_analyzer(msg, &txn->hdr_idx);
9517
9518 /* Still no valid request ? */
9519 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009520 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02009521 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009522 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009523 }
9524 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009525 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009526 return 0;
9527 }
9528
9529 /* OK we just got a valid HTTP request. We have some minor
9530 * preparation to perform so that further checks can rely
9531 * on HTTP tests.
9532 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009533
9534 /* If the request was parsed but was too large, we must absolutely
9535 * return an error so that it is not processed. At the moment this
9536 * cannot happen, but if the parsers are to change in the future,
9537 * we want this check to be maintained.
9538 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009539 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009540 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009541 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009542 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009543 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009544 return 1;
9545 }
9546
Willy Tarreauf37954d2018-06-15 18:31:02 +02009547 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009548 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009549 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009550
Willy Tarreau506d0502013-07-06 13:29:24 +02009551 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9552 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009553 }
9554
Willy Tarreau506d0502013-07-06 13:29:24 +02009555 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009556 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009557 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009558
9559 /* otherwise everything's ready for the request */
9560 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009561 else {
9562 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009563 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9564 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009565 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009566 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009567 }
9568
9569 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009570 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009571 return 1;
9572}
Willy Tarreau8797c062007-05-07 00:55:35 +02009573
Willy Tarreau8797c062007-05-07 00:55:35 +02009574/* 1. Check on METHOD
9575 * We use the pre-parsed method if it is known, and store its number as an
9576 * integer. If it is unknown, we use the pointer and the length.
9577 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009578static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009579{
9580 int len, meth;
9581
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009582 len = strlen(text);
9583 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009584
9585 pattern->val.i = meth;
9586 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009587 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009588 pattern->len = len;
9589 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009590 else {
9591 pattern->ptr.str = NULL;
9592 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009593 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009594 return 1;
9595}
9596
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009597/* This function fetches the method of current HTTP request and stores
9598 * it in the global pattern struct as a chunk. There are two possibilities :
9599 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9600 * in <len> and <ptr> is NULL ;
9601 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9602 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009603 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009604 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009605static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009606smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009607{
9608 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009609 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009610
Willy Tarreau24e32d82012-04-23 23:55:44 +02009611 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009612
Willy Tarreaube508f12016-03-10 11:47:01 +01009613 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009614 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009615 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009616 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009617 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009618 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9619 /* ensure the indexes are not affected */
9620 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009621 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009622 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
9623 smp->data.u.meth.str.area = ci_head(txn->req.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009624 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009625 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009626 return 1;
9627}
9628
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009629/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009630static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009631{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009632 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009633 struct pattern_list *lst;
9634 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009635
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009636 list_for_each_entry(lst, &expr->patterns, list) {
9637 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009638
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009639 /* well-known method */
9640 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009641 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009642 return pattern;
9643 else
9644 continue;
9645 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009646
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009647 /* Other method, we must compare the strings */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009648 if (pattern->len != smp->data.u.meth.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009649 continue;
9650
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009651 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009652 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) ||
9653 (!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 +01009654 return pattern;
9655 }
9656 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009657}
9658
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009659static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009660smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009661{
Willy Tarreaube508f12016-03-10 11:47:01 +01009662 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009663 char *ptr;
9664 int len;
9665
Willy Tarreauc0239e02012-04-16 14:42:55 +02009666 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009667
Willy Tarreaube508f12016-03-10 11:47:01 +01009668 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009669 len = txn->req.sl.rq.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009670 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009671
9672 while ((len-- > 0) && (*ptr++ != '/'));
9673 if (len <= 0)
9674 return 0;
9675
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009676 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009677 smp->data.u.str.area = ptr;
9678 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009679
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009680 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009681 return 1;
9682}
9683
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009684static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009685smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009686{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009687 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009688 char *ptr;
9689 int len;
9690
Willy Tarreauc0239e02012-04-16 14:42:55 +02009691 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009692
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009693 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009694 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9695 return 0;
9696
Willy Tarreau8797c062007-05-07 00:55:35 +02009697 len = txn->rsp.sl.st.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009698 ptr = ci_head(txn->rsp.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009699
9700 while ((len-- > 0) && (*ptr++ != '/'));
9701 if (len <= 0)
9702 return 0;
9703
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009704 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009705 smp->data.u.str.area = ptr;
9706 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009707
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009708 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009709 return 1;
9710}
9711
9712/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009713static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009714smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009715{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009716 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009717 char *ptr;
9718 int len;
9719
Willy Tarreauc0239e02012-04-16 14:42:55 +02009720 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009721
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009722 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009723 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9724 return 0;
9725
Willy Tarreau8797c062007-05-07 00:55:35 +02009726 len = txn->rsp.sl.st.c_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009727 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009728
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009729 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009730 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009731 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009732 return 1;
9733}
9734
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009735static int
9736smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9737{
9738 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9739 return 0;
9740
9741 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009742 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009743 return 0;
9744 smp->strm->unique_id[0] = '\0';
9745 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009746 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009747 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9748
9749 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009750 smp->data.u.str.area = smp->strm->unique_id;
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009751 smp->flags = SMP_F_CONST;
9752 return 1;
9753}
9754
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009755/* Returns a string block containing all headers including the
9756 * empty line wich separes headers from the body. This is useful
9757 * form some headers analysis.
9758 */
9759static int
9760smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9761{
9762 struct http_msg *msg;
9763 struct hdr_idx *idx;
9764 struct http_txn *txn;
9765
9766 CHECK_HTTP_MESSAGE_FIRST();
9767
9768 txn = smp->strm->txn;
9769 idx = &txn->hdr_idx;
9770 msg = &txn->req;
9771
9772 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009773 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
9774 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Willy Tarreauf37954d2018-06-15 18:31:02 +02009775 (ci_head(msg->chn)[msg->eoh] == '\r');
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009776
9777 return 1;
9778}
9779
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009780/* Returns the header request in a length/value encoded format.
9781 * This is useful for exchanges with the SPOE.
9782 *
9783 * A "length value" is a multibyte code encoding numbers. It uses the
9784 * SPOE format. The encoding is the following:
9785 *
9786 * Each couple "header name" / "header value" is composed
9787 * like this:
9788 * "length value" "header name bytes"
9789 * "length value" "header value bytes"
9790 * When the last header is reached, the header name and the header
9791 * value are empty. Their length are 0
9792 */
9793static int
9794smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9795{
9796 struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009797 struct buffer *temp;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009798 struct hdr_idx *idx;
9799 const char *cur_ptr, *cur_next, *p;
9800 int old_idx, cur_idx;
9801 struct hdr_idx_elem *cur_hdr;
9802 const char *hn, *hv;
9803 int hnl, hvl;
9804 int ret;
9805 struct http_txn *txn;
9806 char *buf;
9807 char *end;
9808
9809 CHECK_HTTP_MESSAGE_FIRST();
9810
9811 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009812 buf = temp->area;
9813 end = temp->area + temp->size;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009814
9815 txn = smp->strm->txn;
9816 idx = &txn->hdr_idx;
9817 msg = &txn->req;
9818
9819 /* Build array of headers. */
9820 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009821 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009822 while (1) {
9823 cur_idx = idx->v[old_idx].next;
9824 if (!cur_idx)
9825 break;
9826 old_idx = cur_idx;
9827
9828 cur_hdr = &idx->v[cur_idx];
9829 cur_ptr = cur_next;
9830 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9831
9832 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9833 * and the next header starts at cur_next. We'll check
9834 * this header in the list as well as against the default
9835 * rule.
9836 */
9837
9838 /* look for ': *'. */
9839 hn = cur_ptr;
9840 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9841 if (p >= cur_ptr+cur_hdr->len)
9842 continue;
9843 hnl = p - hn;
9844 p++;
9845 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9846 p++;
9847 if (p >= cur_ptr + cur_hdr->len)
9848 continue;
9849 hv = p;
9850 hvl = cur_ptr + cur_hdr->len-p;
9851
9852 /* encode the header name. */
9853 ret = encode_varint(hnl, &buf, end);
9854 if (ret == -1)
9855 return 0;
9856 if (buf + hnl > end)
9857 return 0;
9858 memcpy(buf, hn, hnl);
9859 buf += hnl;
9860
9861 /* encode and copy the value. */
9862 ret = encode_varint(hvl, &buf, end);
9863 if (ret == -1)
9864 return 0;
9865 if (buf + hvl > end)
9866 return 0;
9867 memcpy(buf, hv, hvl);
9868 buf += hvl;
9869 }
9870
9871 /* encode the end of the header list with empty
9872 * header name and header value.
9873 */
9874 ret = encode_varint(0, &buf, end);
9875 if (ret == -1)
9876 return 0;
9877 ret = encode_varint(0, &buf, end);
9878 if (ret == -1)
9879 return 0;
9880
9881 /* Initialise sample data which will be filled. */
9882 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009883 smp->data.u.str.area = temp->area;
9884 smp->data.u.str.data = buf - temp->area;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009885 smp->data.u.str.size = temp->size;
9886
9887 return 1;
9888}
9889
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009890/* returns the longest available part of the body. This requires that the body
9891 * has been waited for using http-buffer-request.
9892 */
9893static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009894smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009895{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009896 struct http_msg *msg;
9897 unsigned long len;
9898 unsigned long block1;
9899 char *body;
Willy Tarreau83061a82018-07-13 11:56:34 +02009900 struct buffer *temp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009901
9902 CHECK_HTTP_MESSAGE_FIRST();
9903
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009904 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009905 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009906 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009907 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009908
9909 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009910 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009911
9912 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009913 if (block1 > b_wrap(&msg->chn->buf) - body)
9914 block1 = b_wrap(&msg->chn->buf) - body;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009915
9916 if (block1 == len) {
9917 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009918 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009919 smp->data.u.str.area = body;
9920 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009921 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9922 }
9923 else {
9924 /* buffer is wrapped, we need to defragment it */
9925 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009926 memcpy(temp->area, body, block1);
9927 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
9928 len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009929 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009930 smp->data.u.str.area = temp->area;
9931 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009932 smp->flags = SMP_F_VOL_TEST;
9933 }
9934 return 1;
9935}
9936
9937
9938/* returns the available length of the body. This requires that the body
9939 * has been waited for using http-buffer-request.
9940 */
9941static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009942smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009943{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009944 struct http_msg *msg;
9945
9946 CHECK_HTTP_MESSAGE_FIRST();
9947
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009948 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009949 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009950 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009951 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009952
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009953 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009954 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009955
9956 smp->flags = SMP_F_VOL_TEST;
9957 return 1;
9958}
9959
9960
9961/* returns the advertised length of the body, or the advertised size of the
9962 * chunks available in the buffer. This requires that the body has been waited
9963 * for using http-buffer-request.
9964 */
9965static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009966smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009967{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009968 struct http_msg *msg;
9969
9970 CHECK_HTTP_MESSAGE_FIRST();
9971
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009972 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009973 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009974 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009975 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009976
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009977 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009978 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009979
9980 smp->flags = SMP_F_VOL_TEST;
9981 return 1;
9982}
9983
9984
Willy Tarreau8797c062007-05-07 00:55:35 +02009985/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009986static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009987smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009988{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009989 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009990
Willy Tarreauc0239e02012-04-16 14:42:55 +02009991 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009992
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009993 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009994 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009995 smp->data.u.str.data = txn->req.sl.rq.u_l;
9996 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009997 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009998 return 1;
9999}
10000
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010001static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010002smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010003{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010004 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010005 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010006
Willy Tarreauc0239e02012-04-16 14:42:55 +020010007 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010008
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010009 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010010 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 +020010011 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010012 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010013
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010014 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010015 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010016 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010017 return 1;
10018}
10019
10020static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010021smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010022{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010023 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010024 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010025
Willy Tarreauc0239e02012-04-16 14:42:55 +020010026 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010027
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010028 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010029 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 +020010030 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10031 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010032
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010033 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010034 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010035 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010036 return 1;
10037}
10038
Willy Tarreau185b5c42012-04-26 15:11:51 +020010039/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10040 * Accepts an optional argument of type string containing the header field name,
10041 * and an optional argument of type signed or unsigned integer to request an
10042 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010043 * headers are considered from the first one. It does not stop on commas and
10044 * returns full lines instead (useful for User-Agent or Date for example).
10045 */
10046static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010047smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010048{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010049 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010050 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010051 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010052 int occ = 0;
10053 const char *name_str = NULL;
10054 int name_len = 0;
10055
10056 if (!ctx) {
10057 /* first call */
10058 ctx = &static_hdr_ctx;
10059 ctx->idx = 0;
10060 smp->ctx.a[0] = ctx;
10061 }
10062
10063 if (args) {
10064 if (args[0].type != ARGT_STR)
10065 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010066 name_str = args[0].data.str.area;
10067 name_len = args[0].data.str.data;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010068
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010069 if (args[1].type == ARGT_SINT)
10070 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010071 }
10072
10073 CHECK_HTTP_MESSAGE_FIRST();
10074
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010075 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010076 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 +020010077
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010078 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10079 /* search for header from the beginning */
10080 ctx->idx = 0;
10081
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010082 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010083 /* no explicit occurrence and single fetch => last header by default */
10084 occ = -1;
10085
10086 if (!occ)
10087 /* prepare to report multiple occurrences for ACL fetches */
10088 smp->flags |= SMP_F_NOT_LAST;
10089
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010090 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010091 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010092 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 +020010093 return 1;
10094
10095 smp->flags &= ~SMP_F_NOT_LAST;
10096 return 0;
10097}
10098
10099/* 6. Check on HTTP header count. The number of occurrences is returned.
10100 * Accepts exactly 1 argument of type string. It does not stop on commas and
10101 * returns full lines instead (useful for User-Agent or Date for example).
10102 */
10103static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010104smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010105{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010106 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010107 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010108 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010109 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010110 const char *name = NULL;
10111 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010112
Willy Tarreau601a4d12015-04-01 19:16:09 +020010113 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010114 name = args->data.str.area;
10115 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010116 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010117
10118 CHECK_HTTP_MESSAGE_FIRST();
10119
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010120 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010121 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 +020010122
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010123 ctx.idx = 0;
10124 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010125 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010126 cnt++;
10127
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010128 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010129 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010130 smp->flags = SMP_F_VOL_HDR;
10131 return 1;
10132}
10133
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010134static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010135smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010136{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010137 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010138 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010139 const struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +020010140 struct buffer *temp;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010141 char del = ',';
10142
10143 if (args && args->type == ARGT_STR)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010144 del = *args[0].data.str.area;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010145
10146 CHECK_HTTP_MESSAGE_FIRST();
10147
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010148 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010149 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 +020010150
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010151 temp = get_trash_chunk();
10152
10153 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010154 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010155 if (temp->data)
10156 temp->area[temp->data++] = del;
10157 memcpy(temp->area + temp->data, ctx.line, ctx.del);
10158 temp->data += ctx.del;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010159 }
10160
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010161 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010162 smp->data.u.str.area = temp->area;
10163 smp->data.u.str.data = temp->data;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010164 smp->flags = SMP_F_VOL_HDR;
10165 return 1;
10166}
10167
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010168/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10169 * Accepts an optional argument of type string containing the header field name,
10170 * and an optional argument of type signed or unsigned integer to request an
10171 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010172 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010173 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010174static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010175smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010176{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010177 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010178 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010179 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010180 int occ = 0;
10181 const char *name_str = NULL;
10182 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010183
Willy Tarreaua890d072013-04-02 12:01:06 +020010184 if (!ctx) {
10185 /* first call */
10186 ctx = &static_hdr_ctx;
10187 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010188 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010189 }
10190
Willy Tarreau185b5c42012-04-26 15:11:51 +020010191 if (args) {
10192 if (args[0].type != ARGT_STR)
10193 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010194 name_str = args[0].data.str.area;
10195 name_len = args[0].data.str.data;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010196
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010197 if (args[1].type == ARGT_SINT)
10198 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010199 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010200
Willy Tarreaue333ec92012-04-16 16:26:40 +020010201 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010202
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010203 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010204 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 +020010205
Willy Tarreau185b5c42012-04-26 15:11:51 +020010206 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010207 /* search for header from the beginning */
10208 ctx->idx = 0;
10209
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010210 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010211 /* no explicit occurrence and single fetch => last header by default */
10212 occ = -1;
10213
10214 if (!occ)
10215 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010216 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010217
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010218 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010219 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010220 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 +020010221 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010222
Willy Tarreau37406352012-04-23 16:16:37 +020010223 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010224 return 0;
10225}
10226
Willy Tarreauc11416f2007-06-17 16:58:38 +020010227/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010228 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010229 */
10230static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010231smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010232{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010233 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010234 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010235 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010236 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010237 const char *name = NULL;
10238 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010239
Willy Tarreau601a4d12015-04-01 19:16:09 +020010240 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010241 name = args->data.str.area;
10242 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010243 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010244
Willy Tarreaue333ec92012-04-16 16:26:40 +020010245 CHECK_HTTP_MESSAGE_FIRST();
10246
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010247 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010248 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 +020010249
Willy Tarreau33a7e692007-06-10 19:45:56 +020010250 ctx.idx = 0;
10251 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010252 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010253 cnt++;
10254
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010255 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010256 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010257 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010258 return 1;
10259}
10260
Willy Tarreau185b5c42012-04-26 15:11:51 +020010261/* Fetch an HTTP header's integer value. The integer value is returned. It
10262 * takes a mandatory argument of type string and an optional one of type int
10263 * to designate a specific occurrence. It returns an unsigned integer, which
10264 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010265 */
10266static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010267smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010268{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010269 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010270
Willy Tarreauf853c462012-04-23 18:53:56 +020010271 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010272 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010273 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10274 smp->data.u.str.data);
Willy Tarreauf853c462012-04-23 18:53:56 +020010275 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010276
Willy Tarreaud53e2422012-04-16 17:21:11 +020010277 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010278}
10279
Cyril Bonté69fa9922012-10-25 00:01:06 +020010280/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10281 * and an optional one of type int to designate a specific occurrence.
10282 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010283 */
10284static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010285smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010286{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010287 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010288
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010290 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010291 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010292 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010293 } else {
Willy Tarreau83061a82018-07-13 11:56:34 +020010294 struct buffer *temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010295 if (smp->data.u.str.data < temp->size - 1) {
10296 memcpy(temp->area, smp->data.u.str.area,
10297 smp->data.u.str.data);
10298 temp->area[smp->data.u.str.data] = '\0';
10299 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010300 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010301 break;
10302 }
10303 }
10304 }
10305
Willy Tarreaud53e2422012-04-16 17:21:11 +020010306 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010307 if (!(smp->flags & SMP_F_NOT_LAST))
10308 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010309 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010310 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010311}
10312
Willy Tarreau737b0c12007-06-10 21:28:46 +020010313/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10314 * the first '/' after the possible hostname, and ends before the possible '?'.
10315 */
10316static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010317smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010318{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010319 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010320 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010321
Willy Tarreauc0239e02012-04-16 14:42:55 +020010322 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010323
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010324 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010325 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010326 ptr = http_get_path(txn);
10327 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010328 return 0;
10329
10330 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010331 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010332 smp->data.u.str.area = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010333
10334 while (ptr < end && *ptr != '?')
10335 ptr++;
10336
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010337 smp->data.u.str.data = ptr - smp->data.u.str.area;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010338 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010339 return 1;
10340}
10341
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010342/* This produces a concatenation of the first occurrence of the Host header
10343 * followed by the path component if it begins with a slash ('/'). This means
10344 * that '*' will not be added, resulting in exactly the first Host entry.
10345 * If no Host header is found, then the path is returned as-is. The returned
10346 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010347 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010348 */
10349static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010350smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010351{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010352 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010353 char *ptr, *end, *beg;
10354 struct hdr_ctx ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +020010355 struct buffer *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010356
10357 CHECK_HTTP_MESSAGE_FIRST();
10358
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010359 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010360 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010361 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010362 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010363
10364 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010365 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010366 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010367 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010368 smp->data.u.str.area = temp->area;
10369 smp->data.u.str.data = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010370
10371 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010372 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010373 beg = http_get_path(txn);
10374 if (!beg)
10375 beg = end;
10376
10377 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10378
10379 if (beg < ptr && *beg == '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010380 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
10381 ptr - beg);
10382 smp->data.u.str.data += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010383 }
10384
10385 smp->flags = SMP_F_VOL_1ST;
10386 return 1;
10387}
10388
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010389/* This produces a 32-bit hash of the concatenation of the first occurrence of
10390 * the Host header followed by the path component if it begins with a slash ('/').
10391 * This means that '*' will not be added, resulting in exactly the first Host
10392 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010393 * is hashed using the path hash followed by a full avalanche hash and provides a
10394 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010395 * high-traffic sites without having to store whole paths.
10396 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010397int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010398smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010399{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010400 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010401 struct hdr_ctx ctx;
10402 unsigned int hash = 0;
10403 char *ptr, *beg, *end;
10404 int len;
10405
10406 CHECK_HTTP_MESSAGE_FIRST();
10407
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010408 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010409 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010410 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010411 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10412 ptr = ctx.line + ctx.val;
10413 len = ctx.vlen;
10414 while (len--)
10415 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10416 }
10417
10418 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010419 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010420 beg = http_get_path(txn);
10421 if (!beg)
10422 beg = end;
10423
10424 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10425
10426 if (beg < ptr && *beg == '/') {
10427 while (beg < ptr)
10428 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10429 }
10430 hash = full_hash(hash);
10431
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010432 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010433 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010434 smp->flags = SMP_F_VOL_1ST;
10435 return 1;
10436}
10437
Willy Tarreau4a550602012-12-09 14:53:32 +010010438/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010439 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10440 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10441 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010442 * that in environments where IPv6 is insignificant, truncating the output to
10443 * 8 bytes would still work.
10444 */
10445static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010446smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010447{
Willy Tarreau83061a82018-07-13 11:56:34 +020010448 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010449 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010450
10451 if (!cli_conn)
10452 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010453
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010455 return 0;
10456
Willy Tarreau47ca5452012-12-23 20:22:19 +010010457 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010458 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
10459 temp->data += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010460
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010461 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010462 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010463 memcpy(temp->area + temp->data,
10464 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
10465 4);
10466 temp->data += 4;
Willy Tarreau4a550602012-12-09 14:53:32 +010010467 break;
10468 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010469 memcpy(temp->area + temp->data,
10470 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
10471 16);
10472 temp->data += 16;
Willy Tarreau4a550602012-12-09 14:53:32 +010010473 break;
10474 default:
10475 return 0;
10476 }
10477
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010478 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010479 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010480 return 1;
10481}
10482
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010483/* Extracts the query string, which comes after the question mark '?'. If no
10484 * question mark is found, nothing is returned. Otherwise it returns a sample
10485 * of type string carrying the whole query string.
10486 */
10487static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010488smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010489{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010490 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010491 char *ptr, *end;
10492
10493 CHECK_HTTP_MESSAGE_FIRST();
10494
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010495 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010496 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010497 end = ptr + txn->req.sl.rq.u_l;
10498
10499 /* look up the '?' */
10500 do {
10501 if (ptr == end)
10502 return 0;
10503 } while (*ptr++ != '?');
10504
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010505 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010506 smp->data.u.str.area = ptr;
10507 smp->data.u.str.data = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010508 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10509 return 1;
10510}
10511
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010512static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010513smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010514{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010515 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10516 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10517 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010518
Willy Tarreau24e32d82012-04-23 23:55:44 +020010519 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010520
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010521 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010522 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010523 return 1;
10524}
10525
Willy Tarreau7f18e522010-10-22 20:04:13 +020010526/* return a valid test if the current request is the first one on the connection */
10527static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010528smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010529{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010530 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010531 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010532 return 1;
10533}
10534
Willy Tarreau34db1082012-04-19 17:16:54 +020010535/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010536static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010537smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010538{
10539
Willy Tarreau24e32d82012-04-23 23:55:44 +020010540 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010541 return 0;
10542
Willy Tarreauc0239e02012-04-16 14:42:55 +020010543 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010544
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010545 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010546 return 0;
10547
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010548 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010549 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010550 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010551 return 1;
10552}
Willy Tarreau8797c062007-05-07 00:55:35 +020010553
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010554/* Accepts exactly 1 argument of type userlist */
10555static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010556smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010557{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010558 if (!args || args->type != ARGT_USR)
10559 return 0;
10560
10561 CHECK_HTTP_MESSAGE_FIRST();
10562
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010563 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010564 return 0;
10565
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010566 /* if the user does not belong to the userlist or has a wrong password,
10567 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010568 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010569 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010570 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10571 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010572 return 0;
10573
10574 /* pat_match_auth() will need the user list */
10575 smp->ctx.a[0] = args->data.usr;
10576
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010577 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010578 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010579 smp->data.u.str.area = smp->strm->txn->auth.user;
10580 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010581
10582 return 1;
10583}
10584
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010585/* Try to find the next occurrence of a cookie name in a cookie header value.
10586 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10587 * the cookie value is returned into *value and *value_l, and the function
10588 * returns a pointer to the next pointer to search from if the value was found.
10589 * Otherwise if the cookie was not found, NULL is returned and neither value
10590 * nor value_l are touched. The input <hdr> string should first point to the
10591 * header's value, and the <hdr_end> pointer must point to the first character
10592 * not part of the value. <list> must be non-zero if value may represent a list
10593 * of values (cookie headers). This makes it faster to abort parsing when no
10594 * list is expected.
10595 */
David Carlier4686f792015-09-25 14:10:50 +010010596char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010597extract_cookie_value(char *hdr, const char *hdr_end,
10598 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010599 char **value, size_t *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010600{
10601 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10602 char *next;
10603
10604 /* we search at least a cookie name followed by an equal, and more
10605 * generally something like this :
10606 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10607 */
10608 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10609 /* Iterate through all cookies on this line */
10610
Willy Tarreau2235b262016-11-05 15:50:20 +010010611 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010612 att_beg++;
10613
10614 /* find att_end : this is the first character after the last non
10615 * space before the equal. It may be equal to hdr_end.
10616 */
10617 equal = att_end = att_beg;
10618
10619 while (equal < hdr_end) {
10620 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10621 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010622 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010623 continue;
10624 att_end = equal;
10625 }
10626
10627 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10628 * is between <att_beg> and <equal>, both may be identical.
10629 */
10630
10631 /* look for end of cookie if there is an equal sign */
10632 if (equal < hdr_end && *equal == '=') {
10633 /* look for the beginning of the value */
10634 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010635 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010636 val_beg++;
10637
10638 /* find the end of the value, respecting quotes */
10639 next = find_cookie_value_end(val_beg, hdr_end);
10640
10641 /* make val_end point to the first white space or delimitor after the value */
10642 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010643 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010644 val_end--;
10645 } else {
10646 val_beg = val_end = next = equal;
10647 }
10648
10649 /* We have nothing to do with attributes beginning with '$'. However,
10650 * they will automatically be removed if a header before them is removed,
10651 * since they're supposed to be linked together.
10652 */
10653 if (*att_beg == '$')
10654 continue;
10655
10656 /* Ignore cookies with no equal sign */
10657 if (equal == next)
10658 continue;
10659
10660 /* Now we have the cookie name between att_beg and att_end, and
10661 * its value between val_beg and val_end.
10662 */
10663
10664 if (att_end - att_beg == cookie_name_l &&
10665 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10666 /* let's return this value and indicate where to go on from */
10667 *value = val_beg;
10668 *value_l = val_end - val_beg;
10669 return next + 1;
10670 }
10671
10672 /* Set-Cookie headers only have the name in the first attr=value part */
10673 if (!list)
10674 break;
10675 }
10676
10677 return NULL;
10678}
10679
William Lallemanda43ba4e2014-01-28 18:14:25 +010010680/* Fetch a captured HTTP request header. The index is the position of
10681 * the "capture" option in the configuration file
10682 */
10683static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010684smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010685{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010686 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010687 int idx;
10688
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010689 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010690 return 0;
10691
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010692 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010693
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010694 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010695 return 0;
10696
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010697 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010698 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010699 smp->data.u.str.area = smp->strm->req_cap[idx];
10700 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010701
10702 return 1;
10703}
10704
10705/* Fetch a captured HTTP response header. The index is the position of
10706 * the "capture" option in the configuration file
10707 */
10708static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010709smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010710{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010711 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010712 int idx;
10713
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010714 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010715 return 0;
10716
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010717 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010718
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010719 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 +010010720 return 0;
10721
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010722 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010723 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010724 smp->data.u.str.area = smp->strm->res_cap[idx];
10725 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010726
10727 return 1;
10728}
10729
William Lallemand65ad6e12014-01-31 15:08:02 +010010730/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10731static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010732smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010733{
Willy Tarreau83061a82018-07-13 11:56:34 +020010734 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010735 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010736 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010737
Willy Tarreau15e91e12015-04-04 00:52:09 +020010738 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010739 return 0;
10740
William Lallemand96a77852014-02-05 00:30:02 +010010741 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010742
William Lallemand96a77852014-02-05 00:30:02 +010010743 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10744 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010745
William Lallemand96a77852014-02-05 00:30:02 +010010746 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010747 temp->area = txn->uri;
10748 temp->data = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010749 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010750 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010751 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010752
10753 return 1;
10754
10755}
10756
10757/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10758static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010759smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010760{
Willy Tarreau83061a82018-07-13 11:56:34 +020010761 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010762 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010763 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010764
Willy Tarreau15e91e12015-04-04 00:52:09 +020010765 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010766 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010767
William Lallemand65ad6e12014-01-31 15:08:02 +010010768 ptr = txn->uri;
10769
10770 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10771 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010772
William Lallemand65ad6e12014-01-31 15:08:02 +010010773 if (!*ptr)
10774 return 0;
10775
10776 ptr++; /* skip the space */
10777
10778 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010779 ptr = temp->area = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010780 if (!ptr)
10781 return 0;
10782 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10783 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010785 smp->data.u.str = *temp;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010786 smp->data.u.str.data = ptr - temp->area;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010787 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010788 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010789
10790 return 1;
10791}
10792
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010793/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10794 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10795 */
10796static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010797smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010798{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010799 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010800
Willy Tarreau15e91e12015-04-04 00:52:09 +020010801 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010802 return 0;
10803
10804 if (txn->req.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010805 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010806 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010807 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010808
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010809 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010810 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010811 smp->flags = SMP_F_CONST;
10812 return 1;
10813
10814}
10815
10816/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10817 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10818 */
10819static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010820smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010821{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010822 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010823
Willy Tarreau15e91e12015-04-04 00:52:09 +020010824 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010825 return 0;
10826
10827 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010828 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010829 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010830 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010831
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010832 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010833 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010834 smp->flags = SMP_F_CONST;
10835 return 1;
10836
10837}
10838
William Lallemand65ad6e12014-01-31 15:08:02 +010010839
Willy Tarreaue333ec92012-04-16 16:26:40 +020010840/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010841 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010842 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010843 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010844 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010845 * Accepts exactly 1 argument of type string. If the input options indicate
10846 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010847 * The returned sample is of type CSTR. Can be used to parse cookies in other
10848 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010849 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010850int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010851{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010852 struct http_txn *txn;
10853 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010854 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010855 const struct http_msg *msg;
10856 const char *hdr_name;
10857 int hdr_name_len;
10858 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010859 int occ = 0;
10860 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010861
Willy Tarreau24e32d82012-04-23 23:55:44 +020010862 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010863 return 0;
10864
Willy Tarreaua890d072013-04-02 12:01:06 +020010865 if (!ctx) {
10866 /* first call */
10867 ctx = &static_hdr_ctx;
10868 ctx->idx = 0;
10869 smp->ctx.a[2] = ctx;
10870 }
10871
Willy Tarreaue333ec92012-04-16 16:26:40 +020010872 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010873
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010874 txn = smp->strm->txn;
10875 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010876
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010877 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010878 msg = &txn->req;
10879 hdr_name = "Cookie";
10880 hdr_name_len = 6;
10881 } else {
10882 msg = &txn->rsp;
10883 hdr_name = "Set-Cookie";
10884 hdr_name_len = 10;
10885 }
10886
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010887 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010888 /* no explicit occurrence and single fetch => last cookie by default */
10889 occ = -1;
10890
10891 /* OK so basically here, either we want only one value and it's the
10892 * last one, or we want to iterate over all of them and we fetch the
10893 * next one.
10894 */
10895
Willy Tarreauf37954d2018-06-15 18:31:02 +020010896 sol = ci_head(msg->chn);
Willy Tarreau37406352012-04-23 16:16:37 +020010897 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898 /* search for the header from the beginning, we must first initialize
10899 * the search parameters.
10900 */
Willy Tarreau37406352012-04-23 16:16:37 +020010901 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010902 ctx->idx = 0;
10903 }
10904
Willy Tarreau28376d62012-04-26 21:26:10 +020010905 smp->flags |= SMP_F_VOL_HDR;
10906
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010907 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010908 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10909 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010910 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10911 goto out;
10912
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010913 if (ctx->vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914 continue;
10915
Willy Tarreau37406352012-04-23 16:16:37 +020010916 smp->ctx.a[0] = ctx->line + ctx->val;
10917 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010918 }
10919
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010920 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010921 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010922 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010923 args->data.str.area,
10924 args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010925 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010926 &smp->data.u.str.area,
10927 &smp->data.u.str.data);
Willy Tarreau37406352012-04-23 16:16:37 +020010928 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010929 found = 1;
10930 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010931 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010932 smp->flags |= SMP_F_NOT_LAST;
10933 return 1;
10934 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010935 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010936 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010937 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010938 /* all cookie headers and values were scanned. If we're looking for the
10939 * last occurrence, we may return it now.
10940 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010941 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010942 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010943 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010944}
10945
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010946/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010947 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010948 * multiple cookies may be parsed on the same line. The returned sample is of
10949 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010950 */
10951static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010952smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010953{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010954 struct http_txn *txn;
10955 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010956 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010957 const struct http_msg *msg;
10958 const char *hdr_name;
10959 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010960 int cnt;
10961 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010962 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010963
Willy Tarreau24e32d82012-04-23 23:55:44 +020010964 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010965 return 0;
10966
Willy Tarreaue333ec92012-04-16 16:26:40 +020010967 CHECK_HTTP_MESSAGE_FIRST();
10968
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010969 txn = smp->strm->txn;
10970 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010971
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010972 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010973 msg = &txn->req;
10974 hdr_name = "Cookie";
10975 hdr_name_len = 6;
10976 } else {
10977 msg = &txn->rsp;
10978 hdr_name = "Set-Cookie";
10979 hdr_name_len = 10;
10980 }
10981
Willy Tarreauf37954d2018-06-15 18:31:02 +020010982 sol = ci_head(msg->chn);
Willy Tarreau46787ed2012-04-11 17:28:40 +020010983 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010984 ctx.idx = 0;
10985 cnt = 0;
10986
10987 while (1) {
10988 /* Note: val_beg == NULL every time we need to fetch a new header */
10989 if (!val_beg) {
10990 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10991 break;
10992
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010993 if (ctx.vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010994 continue;
10995
10996 val_beg = ctx.line + ctx.val;
10997 val_end = val_beg + ctx.vlen;
10998 }
10999
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011000 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011001 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011002 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011003 args->data.str.area, args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011004 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011005 &smp->data.u.str.area,
11006 &smp->data.u.str.data))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011007 cnt++;
11008 }
11009 }
11010
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011011 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011012 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011013 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011014 return 1;
11015}
11016
Willy Tarreau51539362012-05-08 12:46:28 +020011017/* Fetch an cookie's integer value. The integer value is returned. It
11018 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11019 */
11020static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011021smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011022{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011023 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011024
11025 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011026 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011027 smp->data.u.sint = strl2ic(smp->data.u.str.area,
11028 smp->data.u.str.data);
Willy Tarreau51539362012-05-08 12:46:28 +020011029 }
11030
11031 return ret;
11032}
11033
Willy Tarreau8797c062007-05-07 00:55:35 +020011034/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011035/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011036/************************************************************************/
11037
David Cournapeau16023ee2010-12-23 20:55:41 +090011038/*
11039 * Given a path string and its length, find the position of beginning of the
11040 * query string. Returns NULL if no query string is found in the path.
11041 *
11042 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11043 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011044 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011045 */
bedis4c75cca2012-10-05 08:38:24 +020011046static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011047{
11048 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011049
bedis4c75cca2012-10-05 08:38:24 +020011050 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011051 return p ? p + 1 : NULL;
11052}
11053
bedis4c75cca2012-10-05 08:38:24 +020011054static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011055{
bedis4c75cca2012-10-05 08:38:24 +020011056 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011057}
11058
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011059/* after increasing a pointer value, it can exceed the first buffer
11060 * size. This function transform the value of <ptr> according with
11061 * the expected position. <chunks> is an array of the one or two
11062 * avalaible chunks. The first value is the start of the first chunk,
11063 * the second value if the end+1 of the first chunks. The third value
11064 * is NULL or the start of the second chunk and the fourth value is
11065 * the end+1 of the second chunk. The function returns 1 if does a
11066 * wrap, else returns 0.
11067 */
11068static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11069{
11070 if (*ptr < chunks[1])
11071 return 0;
11072 if (!chunks[2])
11073 return 0;
11074 *ptr = chunks[2] + ( *ptr - chunks[1] );
11075 return 1;
11076}
11077
David Cournapeau16023ee2010-12-23 20:55:41 +090011078/*
11079 * Given a url parameter, find the starting position of the first occurence,
11080 * or NULL if the parameter is not found.
11081 *
11082 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11083 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011084 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011085 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011086 * or the second chunk. The caller must be check the position before using the
11087 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011088 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011089static const char *
11090find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011091 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011092 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011093{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011094 const char *pos, *last, *equal;
11095 const char **bufs = chunks;
11096 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011097
David Cournapeau16023ee2010-12-23 20:55:41 +090011098
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011099 pos = bufs[0];
11100 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011101 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011102 /* Check the equal. */
11103 equal = pos + url_param_name_l;
11104 if (fix_pointer_if_wrap(chunks, &equal)) {
11105 if (equal >= chunks[3])
11106 return NULL;
11107 } else {
11108 if (equal >= chunks[1])
11109 return NULL;
11110 }
11111 if (*equal == '=') {
11112 if (pos + url_param_name_l > last) {
11113 /* process wrap case, we detect a wrap. In this case, the
11114 * comparison is performed in two parts.
11115 */
11116
11117 /* This is the end, we dont have any other chunk. */
11118 if (bufs != chunks || !bufs[2])
11119 return NULL;
11120
11121 /* Compute the length of each part of the comparison. */
11122 l1 = last - pos;
11123 l2 = url_param_name_l - l1;
11124
11125 /* The second buffer is too short to contain the compared string. */
11126 if (bufs[2] + l2 > bufs[3])
11127 return NULL;
11128
11129 if (memcmp(pos, url_param_name, l1) == 0 &&
11130 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11131 return pos;
11132
11133 /* Perform wrapping and jump the string who fail the comparison. */
11134 bufs += 2;
11135 pos = bufs[0] + l2;
11136 last = bufs[1];
11137
11138 } else {
11139 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011140 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11141 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011142 pos += url_param_name_l + 1;
11143 if (fix_pointer_if_wrap(chunks, &pos))
11144 last = bufs[2];
11145 }
11146 }
11147
11148 while (1) {
11149 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011150 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011151 pos++;
11152 if (pos < last)
11153 break;
11154 /* process buffer wrapping. */
11155 if (bufs != chunks || !bufs[2])
11156 return NULL;
11157 bufs += 2;
11158 pos = bufs[0];
11159 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011160 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011161 pos++;
11162 }
11163 return NULL;
11164}
11165
11166/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011167 * Given a url parameter name and a query string, find the next value.
11168 * An empty url_param_name matches the first available parameter.
11169 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11170 * respectively provide a pointer to the value and its end.
11171 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011172 */
11173static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011174find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011175 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011176 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011177{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011178 const char *arg_start, *qs_end;
11179 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011180
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011181 arg_start = chunks[0];
11182 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011183 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011184 /* Looks for an argument name. */
11185 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011186 url_param_name, url_param_name_l,
11187 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011188 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011189 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011190 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011191 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011192 if (!arg_start)
11193 return 0;
11194
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011195 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011196 while (1) {
11197 /* looks for the first argument. */
11198 value_start = memchr(arg_start, '=', qs_end - arg_start);
11199 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011200 /* Check for wrapping. */
11201 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011202 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011203 chunks[2]) {
11204 arg_start = chunks[2];
11205 qs_end = chunks[3];
11206 continue;
11207 }
11208 return 0;
11209 }
11210 break;
11211 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011212 value_start++;
11213 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011214 else {
11215 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011216 value_start = arg_start + url_param_name_l + 1;
11217
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011218 /* Check for pointer wrapping. */
11219 if (fix_pointer_if_wrap(chunks, &value_start)) {
11220 /* Update the end pointer. */
11221 qs_end = chunks[3];
11222
11223 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011224 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011225 return 0;
11226 }
11227 }
11228
David Cournapeau16023ee2010-12-23 20:55:41 +090011229 value_end = value_start;
11230
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011231 while (1) {
11232 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11233 value_end++;
11234 if (value_end < qs_end)
11235 break;
11236 /* process buffer wrapping. */
11237 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011238 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011239 chunks[2]) {
11240 value_end = chunks[2];
11241 qs_end = chunks[3];
11242 continue;
11243 }
11244 break;
11245 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011246
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011247 *vstart = value_start;
11248 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011249 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011250}
11251
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011252/* This scans a URL-encoded query string. It takes an optionally wrapping
11253 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11254 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11255 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011256 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011257static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011258smp_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 +090011259{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011260 const char *vstart, *vend;
Willy Tarreau83061a82018-07-13 11:56:34 +020011261 struct buffer *temp;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011262 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011263
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011264 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011265 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011266 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011267 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011268 return 0;
11269
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011270 /* Create sample. If the value is contiguous, return the pointer as CONST,
11271 * if the value is wrapped, copy-it in a buffer.
11272 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011273 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011274 if (chunks[2] &&
11275 vstart >= chunks[0] && vstart <= chunks[1] &&
11276 vend >= chunks[2] && vend <= chunks[3]) {
11277 /* Wrapped case. */
11278 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011279 memcpy(temp->area, vstart, chunks[1] - vstart);
11280 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
11281 vend - chunks[2]);
11282 smp->data.u.str.area = temp->area;
11283 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011284 } else {
11285 /* Contiguous case. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011286 smp->data.u.str.area = (char *)vstart;
11287 smp->data.u.str.data = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011288 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11289 }
11290
11291 /* Update context, check wrapping. */
11292 chunks[0] = vend;
11293 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11294 chunks[1] = chunks[3];
11295 chunks[2] = NULL;
11296 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011297
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011298 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011299 smp->flags |= SMP_F_NOT_LAST;
11300
David Cournapeau16023ee2010-12-23 20:55:41 +090011301 return 1;
11302}
11303
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011304/* This function iterates over each parameter of the query string. It uses
11305 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011306 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11307 * An optional parameter name is passed in args[0], otherwise any parameter is
11308 * considered. It supports an optional delimiter argument for the beginning of
11309 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011310 */
11311static int
11312smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11313{
11314 struct http_msg *msg;
11315 char delim = '?';
11316 const char *name;
11317 int name_len;
11318
Dragan Dosen26f77e52015-05-25 10:02:11 +020011319 if (!args ||
11320 (args[0].type && args[0].type != ARGT_STR) ||
11321 (args[1].type && args[1].type != ARGT_STR))
11322 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011323
Dragan Dosen26f77e52015-05-25 10:02:11 +020011324 name = "";
11325 name_len = 0;
11326 if (args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011327 name = args->data.str.area;
11328 name_len = args->data.str.data;
Dragan Dosen26f77e52015-05-25 10:02:11 +020011329 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011330
Dragan Dosen26f77e52015-05-25 10:02:11 +020011331 if (args[1].type)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011332 delim = *args[1].data.str.area;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011333
Dragan Dosen26f77e52015-05-25 10:02:11 +020011334 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011335 CHECK_HTTP_MESSAGE_FIRST();
11336
11337 msg = &smp->strm->txn->req;
11338
Willy Tarreauf37954d2018-06-15 18:31:02 +020011339 smp->ctx.a[0] = find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011340 msg->sl.rq.u_l, delim);
11341 if (!smp->ctx.a[0])
11342 return 0;
11343
Willy Tarreauf37954d2018-06-15 18:31:02 +020011344 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011345
11346 /* Assume that the context is filled with NULL pointer
11347 * before the first call.
11348 * smp->ctx.a[2] = NULL;
11349 * smp->ctx.a[3] = NULL;
11350 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011351 }
11352
11353 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11354}
11355
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011356/* This function iterates over each parameter of the body. This requires
11357 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011358 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11359 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11360 * optional second part if the body wraps at the end of the buffer. An optional
11361 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011362 */
11363static int
11364smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11365{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011366 struct http_msg *msg;
11367 unsigned long len;
11368 unsigned long block1;
11369 char *body;
11370 const char *name;
11371 int name_len;
11372
11373 if (!args || (args[0].type && args[0].type != ARGT_STR))
11374 return 0;
11375
11376 name = "";
11377 name_len = 0;
11378 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011379 name = args[0].data.str.area;
11380 name_len = args[0].data.str.data;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011381 }
11382
11383 if (!smp->ctx.a[0]) { // first call, find the query string
11384 CHECK_HTTP_MESSAGE_FIRST();
11385
11386 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011387 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011388 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011389 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011390
11391 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020011392 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011393
11394 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011395 if (block1 > b_wrap(&msg->chn->buf) - body)
11396 block1 = b_wrap(&msg->chn->buf) - body;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011397
11398 if (block1 == len) {
11399 /* buffer is not wrapped (or empty) */
11400 smp->ctx.a[0] = body;
11401 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011402
11403 /* Assume that the context is filled with NULL pointer
11404 * before the first call.
11405 * smp->ctx.a[2] = NULL;
11406 * smp->ctx.a[3] = NULL;
11407 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011408 }
11409 else {
11410 /* buffer is wrapped, we need to defragment it */
11411 smp->ctx.a[0] = body;
11412 smp->ctx.a[1] = body + block1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011413 smp->ctx.a[2] = b_orig(&msg->chn->buf);
11414 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011415 }
11416 }
11417 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11418}
11419
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011420/* Return the signed integer value for the specified url parameter (see url_param
11421 * above).
11422 */
11423static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011424smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011425{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011426 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011427
11428 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011429 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011430 smp->data.u.sint = strl2ic(smp->data.u.str.area,
11431 smp->data.u.str.data);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011432 }
11433
11434 return ret;
11435}
11436
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011437/* This produces a 32-bit hash of the concatenation of the first occurrence of
11438 * the Host header followed by the path component if it begins with a slash ('/').
11439 * This means that '*' will not be added, resulting in exactly the first Host
11440 * entry. If no Host header is found, then the path is used. The resulting value
11441 * is hashed using the url hash followed by a full avalanche hash and provides a
11442 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11443 * high-traffic sites without having to store whole paths.
11444 * this differs from the base32 functions in that it includes the url parameters
11445 * as well as the path
11446 */
11447static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011448smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011449{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011450 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011451 struct hdr_ctx ctx;
11452 unsigned int hash = 0;
11453 char *ptr, *beg, *end;
11454 int len;
11455
11456 CHECK_HTTP_MESSAGE_FIRST();
11457
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011458 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011459 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011460 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011461 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11462 ptr = ctx.line + ctx.val;
11463 len = ctx.vlen;
11464 while (len--)
11465 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11466 }
11467
11468 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020011469 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 +000011470 beg = http_get_path(txn);
11471 if (!beg)
11472 beg = end;
11473
11474 for (ptr = beg; ptr < end ; ptr++);
11475
11476 if (beg < ptr && *beg == '/') {
11477 while (beg < ptr)
11478 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11479 }
11480 hash = full_hash(hash);
11481
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011482 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011483 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011484 smp->flags = SMP_F_VOL_1ST;
11485 return 1;
11486}
11487
11488/* This concatenates the source address with the 32-bit hash of the Host and
11489 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11490 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11491 * on the source address length. The URL hash is stored before the address so
11492 * that in environments where IPv6 is insignificant, truncating the output to
11493 * 8 bytes would still work.
11494 */
11495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011496smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011497{
Willy Tarreau83061a82018-07-13 11:56:34 +020011498 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011499 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011500
Dragan Dosendb5af612016-06-16 11:23:01 +020011501 if (!cli_conn)
11502 return 0;
11503
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011504 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011505 return 0;
11506
11507 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011508 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
11509 temp->data += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011510
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011511 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011512 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011513 memcpy(temp->area + temp->data,
11514 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
11515 4);
11516 temp->data += 4;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011517 break;
11518 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011519 memcpy(temp->area + temp->data,
11520 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
11521 16);
11522 temp->data += 16;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011523 break;
11524 default:
11525 return 0;
11526 }
11527
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011528 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011529 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011530 return 1;
11531}
11532
Willy Tarreau185b5c42012-04-26 15:11:51 +020011533/* This function is used to validate the arguments passed to any "hdr" fetch
11534 * keyword. These keywords support an optional positive or negative occurrence
11535 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11536 * is assumed that the types are already the correct ones. Returns 0 on error,
11537 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11538 * error message in case of error, that the caller is responsible for freeing.
11539 * The initial location must either be freeable or NULL.
11540 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011541int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011542{
11543 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011544 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011545 return 0;
11546 }
11547 return 1;
11548}
11549
Willy Tarreau276fae92013-07-25 14:36:01 +020011550/* takes an UINT value on input supposed to represent the time since EPOCH,
11551 * adds an optional offset found in args[0] and emits a string representing
11552 * the date in RFC-1123/5322 format.
11553 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011554static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011555{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011556 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011557 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Willy Tarreau83061a82018-07-13 11:56:34 +020011558 struct buffer *temp;
Willy Tarreau276fae92013-07-25 14:36:01 +020011559 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011560 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011561 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011562
11563 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011564 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011565 curr_date += args[0].data.sint;
11566
11567 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011568 if (!tm)
11569 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011570
11571 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011572 temp->data = snprintf(temp->area, temp->size - temp->data,
11573 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11574 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
11575 1900+tm->tm_year,
11576 tm->tm_hour, tm->tm_min, tm->tm_sec);
Willy Tarreau276fae92013-07-25 14:36:01 +020011577
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011578 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011579 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011580 return 1;
11581}
11582
Thierry FOURNIERad903512014-04-11 17:51:01 +020011583/* Match language range with language tag. RFC2616 14.4:
11584 *
11585 * A language-range matches a language-tag if it exactly equals
11586 * the tag, or if it exactly equals a prefix of the tag such
11587 * that the first tag character following the prefix is "-".
11588 *
11589 * Return 1 if the strings match, else return 0.
11590 */
11591static inline int language_range_match(const char *range, int range_len,
11592 const char *tag, int tag_len)
11593{
11594 const char *end = range + range_len;
11595 const char *tend = tag + tag_len;
11596 while (range < end) {
11597 if (*range == '-' && tag == tend)
11598 return 1;
11599 if (*range != *tag || tag == tend)
11600 return 0;
11601 range++;
11602 tag++;
11603 }
11604 /* Return true only if the last char of the tag is matched. */
11605 return tag == tend;
11606}
11607
11608/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011609static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011610{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011611 const char *al = smp->data.u.str.area;
11612 const char *end = al + smp->data.u.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011613 const char *token;
11614 int toklen;
11615 int qvalue;
11616 const char *str;
11617 const char *w;
11618 int best_q = 0;
11619
11620 /* Set the constant to the sample, because the output of the
11621 * function will be peek in the constant configuration string.
11622 */
11623 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011624 smp->data.u.str.size = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011625 smp->data.u.str.area = "";
11626 smp->data.u.str.data = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011627
11628 /* Parse the accept language */
11629 while (1) {
11630
11631 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011632 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011633 al++;
11634 if (al >= end)
11635 break;
11636
11637 /* Start of the fisrt word. */
11638 token = al;
11639
11640 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011641 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011642 al++;
11643 if (al == token)
11644 goto expect_comma;
11645
11646 /* Length of the token. */
11647 toklen = al - token;
11648 qvalue = 1000;
11649
11650 /* Check if the token exists in the list. If the token not exists,
11651 * jump to the next token.
11652 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011653 str = args[0].data.str.area;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011654 w = str;
11655 while (1) {
11656 if (*str == ';' || *str == '\0') {
11657 if (language_range_match(token, toklen, w, str-w))
11658 goto look_for_q;
11659 if (*str == '\0')
11660 goto expect_comma;
11661 w = str + 1;
11662 }
11663 str++;
11664 }
11665 goto expect_comma;
11666
11667look_for_q:
11668
11669 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011670 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011671 al++;
11672 if (al >= end)
11673 goto process_value;
11674
11675 /* If ',' is found, process the result */
11676 if (*al == ',')
11677 goto process_value;
11678
11679 /* If the character is different from ';', look
11680 * for the end of the header part in best effort.
11681 */
11682 if (*al != ';')
11683 goto expect_comma;
11684
11685 /* Assumes that the char is ';', now expect "q=". */
11686 al++;
11687
11688 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011689 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011690 al++;
11691 if (al >= end)
11692 goto process_value;
11693
11694 /* Expect 'q'. If no 'q', continue in best effort */
11695 if (*al != 'q')
11696 goto process_value;
11697 al++;
11698
11699 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011700 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011701 al++;
11702 if (al >= end)
11703 goto process_value;
11704
11705 /* Expect '='. If no '=', continue in best effort */
11706 if (*al != '=')
11707 goto process_value;
11708 al++;
11709
11710 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011711 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011712 al++;
11713 if (al >= end)
11714 goto process_value;
11715
11716 /* Parse the q value. */
11717 qvalue = parse_qvalue(al, &al);
11718
11719process_value:
11720
11721 /* If the new q value is the best q value, then store the associated
11722 * language in the response. If qvalue is the biggest value (1000),
11723 * break the process.
11724 */
11725 if (qvalue > best_q) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011726 smp->data.u.str.area = (char *)w;
11727 smp->data.u.str.data = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011728 if (qvalue >= 1000)
11729 break;
11730 best_q = qvalue;
11731 }
11732
11733expect_comma:
11734
11735 /* Expect comma or end. If the end is detected, quit the loop. */
11736 while (al < end && *al != ',')
11737 al++;
11738 if (al >= end)
11739 break;
11740
11741 /* Comma is found, jump it and restart the analyzer. */
11742 al++;
11743 }
11744
11745 /* Set default value if required. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011746 if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
11747 smp->data.u.str.area = args[1].data.str.area;
11748 smp->data.u.str.data = args[1].data.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011749 }
11750
11751 /* Return true only if a matching language was found. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011752 return smp->data.u.str.data != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011753}
11754
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011755/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011756static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011757{
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011758 int len;
11759
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011760 /* If the constant flag is set or if not size is avalaible at
11761 * the end of the buffer, copy the string in other buffer
11762 * before decoding.
11763 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011764 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
Willy Tarreau83061a82018-07-13 11:56:34 +020011765 struct buffer *str = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011766 memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
11767 smp->data.u.str.area = str->area;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011768 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011769 smp->flags &= ~SMP_F_CONST;
11770 }
11771
11772 /* Add final \0 required by url_decode(), and convert the input string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011773 smp->data.u.str.area[smp->data.u.str.data] = '\0';
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011774 len = url_decode(smp->data.u.str.area);
11775 if (len < 0)
11776 return 0;
11777 smp->data.u.str.data = len;
11778 return 1;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011779}
11780
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011781static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11782{
11783 struct proxy *fe = strm_fe(smp->strm);
11784 int idx, i;
11785 struct cap_hdr *hdr;
11786 int len;
11787
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011788 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011789 return 0;
11790
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011791 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011792
11793 /* Check the availibity of the capture id. */
11794 if (idx > fe->nb_req_cap - 1)
11795 return 0;
11796
11797 /* Look for the original configuration. */
11798 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11799 hdr != NULL && i != idx ;
11800 i--, hdr = hdr->next);
11801 if (!hdr)
11802 return 0;
11803
11804 /* check for the memory allocation */
11805 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011806 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011807 if (smp->strm->req_cap[hdr->index] == NULL)
11808 return 0;
11809
11810 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011811 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011812 if (len > hdr->len)
11813 len = hdr->len;
11814
11815 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011816 memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011817 smp->strm->req_cap[idx][len] = '\0';
11818
11819 return 1;
11820}
11821
11822static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11823{
11824 struct proxy *fe = strm_fe(smp->strm);
11825 int idx, i;
11826 struct cap_hdr *hdr;
11827 int len;
11828
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011829 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011830 return 0;
11831
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011832 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011833
11834 /* Check the availibity of the capture id. */
11835 if (idx > fe->nb_rsp_cap - 1)
11836 return 0;
11837
11838 /* Look for the original configuration. */
11839 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11840 hdr != NULL && i != idx ;
11841 i--, hdr = hdr->next);
11842 if (!hdr)
11843 return 0;
11844
11845 /* check for the memory allocation */
11846 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011847 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011848 if (smp->strm->res_cap[hdr->index] == NULL)
11849 return 0;
11850
11851 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011852 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011853 if (len > hdr->len)
11854 len = hdr->len;
11855
11856 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011857 memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011858 smp->strm->res_cap[idx][len] = '\0';
11859
11860 return 1;
11861}
11862
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011863/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011864 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011865 * the relevant part of the request line accordingly. Then it updates various
11866 * pointers to the next elements which were moved, and the total buffer length.
11867 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011868 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11869 * error, though this can be revisited when this code is finally exploited.
11870 *
11871 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11872 * query string and 3 to replace uri.
11873 *
11874 * In query string case, the mark question '?' must be set at the start of the
11875 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011876 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011877int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011878 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011879{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011880 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011881 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011882 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011883 int delta;
11884
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011885 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011886 case 0: // method
Willy Tarreauf37954d2018-06-15 18:31:02 +020011887 cur_ptr = ci_head(&s->req);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011888 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11889
11890 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011891 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011892 txn->req.sl.rq.m_l += delta;
11893 txn->req.sl.rq.u += delta;
11894 txn->req.sl.rq.v += delta;
11895 break;
11896
11897 case 1: // path
11898 cur_ptr = http_get_path(txn);
11899 if (!cur_ptr)
Willy Tarreauf37954d2018-06-15 18:31:02 +020011900 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011901
11902 cur_end = cur_ptr;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011903 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 +010011904 cur_end++;
11905
11906 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011907 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011908 txn->req.sl.rq.u_l += delta;
11909 txn->req.sl.rq.v += delta;
11910 break;
11911
11912 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011913 offset = 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011914 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011915 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11916 while (cur_ptr < cur_end && *cur_ptr != '?')
11917 cur_ptr++;
11918
11919 /* skip the question mark or indicate that we must insert it
11920 * (but only if the format string is not empty then).
11921 */
11922 if (cur_ptr < cur_end)
11923 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011924 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011925 offset = 0;
11926
11927 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011928 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011929 txn->req.sl.rq.u_l += delta;
11930 txn->req.sl.rq.v += delta;
11931 break;
11932
11933 case 3: // uri
Willy Tarreauf37954d2018-06-15 18:31:02 +020011934 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011935 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11936
11937 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011938 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011939 txn->req.sl.rq.u_l += delta;
11940 txn->req.sl.rq.v += delta;
11941 break;
11942
11943 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011944 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011945 }
11946
11947 /* commit changes and adjust end of message */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011948 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011949 txn->req.sl.rq.l += delta;
11950 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011951 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011952 return 0;
11953}
11954
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011955/* This function replace the HTTP status code and the associated message. The
11956 * variable <status> contains the new status code. This function never fails.
11957 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011958void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011959{
11960 struct http_txn *txn = s->txn;
11961 char *cur_ptr, *cur_end;
11962 int delta;
11963 char *res;
11964 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011965 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011966 int msg_len;
11967
11968 chunk_reset(&trash);
11969
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011970 res = ultoa_o(status, trash.area, trash.size);
11971 c_l = res - trash.area;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011972
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011973 trash.area[c_l] = ' ';
11974 trash.data = c_l + 1;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011975
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011976 /* Do we have a custom reason format string? */
11977 if (msg == NULL)
11978 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011979 msg_len = strlen(msg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011980 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
11981 trash.data += msg_len;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011982
Willy Tarreauf37954d2018-06-15 18:31:02 +020011983 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
11984 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011985
11986 /* commit changes and adjust message */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011987 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
11988 trash.data);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011989
11990 /* adjust res line offsets and lengths */
11991 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11992 txn->rsp.sl.st.c_l = c_l;
11993 txn->rsp.sl.st.r_l = msg_len;
11994
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011995 delta = trash.data - (cur_end - cur_ptr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011996 txn->rsp.sl.st.l += delta;
11997 txn->hdr_idx.v[0].len += delta;
11998 http_msg_move_end(&txn->rsp, delta);
11999}
12000
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012001/* This function executes one of the set-{method,path,query,uri} actions. It
12002 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012003 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012004 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012005 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12006 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012007 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012008enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012009 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012010{
Willy Tarreau83061a82018-07-13 11:56:34 +020012011 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012012 enum act_return ret = ACT_RET_ERR;
12013
12014 replace = alloc_trash_chunk();
12015 if (!replace)
12016 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012017
12018 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012019 if (rule->arg.http.action == 2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012020 replace->area[replace->data++] = '?';
12021 replace->data += build_logline(s, replace->area + replace->data,
12022 replace->size - replace->data,
12023 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012024
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012025 http_replace_req_line(rule->arg.http.action, replace->area,
12026 replace->data, px, s);
Dragan Dosen2ae327e2017-10-26 11:25:10 +020012027
12028 ret = ACT_RET_CONT;
12029
12030leave:
12031 free_trash_chunk(replace);
12032 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012033}
12034
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012035/* This function is just a compliant action wrapper for "set-status". */
12036enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012037 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012038{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012039 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012040 return ACT_RET_CONT;
12041}
12042
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012043/* parse an http-request action among :
12044 * set-method
12045 * set-path
12046 * set-query
12047 * set-uri
12048 *
12049 * All of them accept a single argument of type string representing a log-format.
12050 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12051 * 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 +020012052 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012053 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012054enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12055 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012056{
12057 int cur_arg = *orig_arg;
12058
Thierry FOURNIER42148732015-09-02 17:17:33 +020012059 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012060
12061 switch (args[0][4]) {
12062 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012063 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012064 rule->action_ptr = http_action_set_req_line;
12065 break;
12066 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012067 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012068 rule->action_ptr = http_action_set_req_line;
12069 break;
12070 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012071 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012072 rule->action_ptr = http_action_set_req_line;
12073 break;
12074 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012075 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012076 rule->action_ptr = http_action_set_req_line;
12077 break;
12078 default:
12079 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012080 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012081 }
12082
12083 if (!*args[cur_arg] ||
12084 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12085 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012086 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012087 }
12088
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012089 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012090 px->conf.args.ctx = ARGC_HRQ;
12091 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12092 (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 +010012093 return ACT_RET_PRS_ERR;
12094 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012095
12096 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012097 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012098}
12099
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012100/* parse set-status action:
12101 * This action accepts a single argument of type int representing
12102 * an http status code. It returns ACT_RET_PRS_OK on success,
12103 * ACT_RET_PRS_ERR on error.
12104 */
12105enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12106 struct act_rule *rule, char **err)
12107{
12108 char *error;
12109
Thierry FOURNIER42148732015-09-02 17:17:33 +020012110 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012111 rule->action_ptr = action_http_set_status;
12112
12113 /* Check if an argument is available */
12114 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012115 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012116 return ACT_RET_PRS_ERR;
12117 }
12118
12119 /* convert status code as integer */
12120 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12121 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12122 memprintf(err, "expects an integer status code between 100 and 999");
12123 return ACT_RET_PRS_ERR;
12124 }
12125
12126 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012127
12128 /* set custom reason string */
12129 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12130 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12131 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12132 (*orig_arg)++;
12133 rule->arg.status.reason = strdup(args[*orig_arg]);
12134 (*orig_arg)++;
12135 }
12136
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012137 return ACT_RET_PRS_OK;
12138}
12139
Willy Tarreau53275e82017-11-24 07:52:01 +010012140/* This function executes the "reject" HTTP action. It clears the request and
12141 * response buffer without sending any response. It can be useful as an HTTP
12142 * alternative to the silent-drop action to defend against DoS attacks, and may
12143 * also be used with HTTP/2 to close a connection instead of just a stream.
12144 * The txn status is unchanged, indicating no response was sent. The termination
12145 * flags will indicate "PR". It always returns ACT_RET_STOP.
12146 */
12147enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12148 struct session *sess, struct stream *s, int flags)
12149{
12150 channel_abort(&s->req);
12151 channel_abort(&s->res);
12152 s->req.analysers = 0;
12153 s->res.analysers = 0;
12154
12155 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12156 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12157 if (sess->listener && sess->listener->counters)
12158 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12159
12160 if (!(s->flags & SF_ERR_MASK))
12161 s->flags |= SF_ERR_PRXCOND;
12162 if (!(s->flags & SF_FINST_MASK))
12163 s->flags |= SF_FINST_R;
12164
12165 return ACT_RET_CONT;
12166}
12167
12168/* parse the "reject" action:
12169 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12170 * ACT_RET_PRS_ERR on error.
12171 */
12172enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12173 struct act_rule *rule, char **err)
12174{
12175 rule->action = ACT_CUSTOM;
12176 rule->action_ptr = http_action_reject;
12177 return ACT_RET_PRS_OK;
12178}
12179
Willy Tarreaua9083d02015-05-08 15:27:59 +020012180/* This function executes the "capture" action. It executes a fetch expression,
12181 * turns the result into a string and puts it in a capture slot. It always
12182 * returns 1. If an error occurs the action is cancelled, but the rule
12183 * processing continues.
12184 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012185enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012186 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012187{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012188 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012189 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012190 char **cap = s->req_cap;
12191 int len;
12192
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012193 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 +020012194 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012195 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012196
12197 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012198 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012199
12200 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012201 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012202
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012203 len = key->data.u.str.data;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012204 if (len > h->len)
12205 len = h->len;
12206
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012207 memcpy(cap[h->index], key->data.u.str.area, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012208 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012209 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012210}
12211
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012212/* This function executes the "capture" action and store the result in a
12213 * capture slot if exists. It executes a fetch expression, turns the result
12214 * into a string and puts it in a capture slot. It always returns 1. If an
12215 * error occurs the action is cancelled, but the rule processing continues.
12216 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012217enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012218 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012219{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012220 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012221 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012222 char **cap = s->req_cap;
12223 struct proxy *fe = strm_fe(s);
12224 int len;
12225 int i;
12226
12227 /* Look for the original configuration. */
12228 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012229 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012230 i--, h = h->next);
12231 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012232 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012233
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012234 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 +020012235 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012236 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012237
12238 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012239 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012240
12241 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012242 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012243
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012244 len = key->data.u.str.data;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012245 if (len > h->len)
12246 len = h->len;
12247
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012248 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012249 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012250 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012251}
12252
Christopher Faulet29730ba2017-09-18 15:26:32 +020012253/* Check an "http-request capture" action.
12254 *
12255 * The function returns 1 in success case, otherwise, it returns 0 and err is
12256 * filled.
12257 */
12258int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12259{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012260 if (rule->action_ptr != http_action_req_capture_by_id)
12261 return 1;
12262
Christopher Faulet29730ba2017-09-18 15:26:32 +020012263 if (rule->arg.capid.idx >= px->nb_req_cap) {
12264 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12265 rule->arg.capid.idx);
12266 return 0;
12267 }
12268
12269 return 1;
12270}
12271
Willy Tarreaua9083d02015-05-08 15:27:59 +020012272/* parse an "http-request capture" action. It takes a single argument which is
12273 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012274 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012275 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012276 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012277enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12278 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012279{
12280 struct sample_expr *expr;
12281 struct cap_hdr *hdr;
12282 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012283 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012284
12285 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12286 if (strcmp(args[cur_arg], "if") == 0 ||
12287 strcmp(args[cur_arg], "unless") == 0)
12288 break;
12289
12290 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012291 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012292 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012293 }
12294
Willy Tarreaua9083d02015-05-08 15:27:59 +020012295 cur_arg = *orig_arg;
12296 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12297 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012298 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012299
12300 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12301 memprintf(err,
12302 "fetch method '%s' extracts information from '%s', none of which is available here",
12303 args[cur_arg-1], sample_src_names(expr->fetch->use));
12304 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012305 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012306 }
12307
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012308 if (!args[cur_arg] || !*args[cur_arg]) {
12309 memprintf(err, "expects 'len or 'id'");
12310 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012311 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012312 }
12313
Willy Tarreaua9083d02015-05-08 15:27:59 +020012314 if (strcmp(args[cur_arg], "len") == 0) {
12315 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012316
12317 if (!(px->cap & PR_CAP_FE)) {
12318 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012319 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012320 }
12321
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012322 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012323
Willy Tarreaua9083d02015-05-08 15:27:59 +020012324 if (!args[cur_arg]) {
12325 memprintf(err, "missing length value");
12326 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012327 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012328 }
12329 /* we copy the table name for now, it will be resolved later */
12330 len = atoi(args[cur_arg]);
12331 if (len <= 0) {
12332 memprintf(err, "length must be > 0");
12333 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012334 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012335 }
12336 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012337
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012338 if (!len) {
12339 memprintf(err, "a positive 'len' argument is mandatory");
12340 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012341 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012342 }
12343
Vincent Bernat02779b62016-04-03 13:48:43 +020012344 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012345 hdr->next = px->req_cap;
12346 hdr->name = NULL; /* not a header capture */
12347 hdr->namelen = 0;
12348 hdr->len = len;
12349 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12350 hdr->index = px->nb_req_cap++;
12351
12352 px->req_cap = hdr;
12353 px->to_log |= LW_REQHDR;
12354
Thierry FOURNIER42148732015-09-02 17:17:33 +020012355 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012356 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012357 rule->arg.cap.expr = expr;
12358 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012359 }
12360
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012361 else if (strcmp(args[cur_arg], "id") == 0) {
12362 int id;
12363 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012364
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012365 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012366
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012367 if (!args[cur_arg]) {
12368 memprintf(err, "missing id value");
12369 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012370 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012371 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012372
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012373 id = strtol(args[cur_arg], &error, 10);
12374 if (*error != '\0') {
12375 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12376 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012377 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012378 }
12379 cur_arg++;
12380
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012381 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012382
Thierry FOURNIER42148732015-09-02 17:17:33 +020012383 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012384 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012385 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012386 rule->arg.capid.expr = expr;
12387 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012388 }
12389
12390 else {
12391 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12392 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012393 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012394 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012395
12396 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012397 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012398}
12399
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012400/* This function executes the "capture" action and store the result in a
12401 * capture slot if exists. It executes a fetch expression, turns the result
12402 * into a string and puts it in a capture slot. It always returns 1. If an
12403 * error occurs the action is cancelled, but the rule processing continues.
12404 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012405enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012406 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012407{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012408 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012409 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012410 char **cap = s->res_cap;
12411 struct proxy *fe = strm_fe(s);
12412 int len;
12413 int i;
12414
12415 /* Look for the original configuration. */
12416 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012417 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012418 i--, h = h->next);
12419 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012420 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012421
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012422 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 +020012423 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012424 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012425
12426 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012427 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012428
12429 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012430 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012431
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012432 len = key->data.u.str.data;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012433 if (len > h->len)
12434 len = h->len;
12435
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012436 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012437 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012438 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012439}
12440
Christopher Faulet29730ba2017-09-18 15:26:32 +020012441/* Check an "http-response capture" action.
12442 *
12443 * The function returns 1 in success case, otherwise, it returns 0 and err is
12444 * filled.
12445 */
12446int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12447{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012448 if (rule->action_ptr != http_action_res_capture_by_id)
12449 return 1;
12450
Christopher Faulet29730ba2017-09-18 15:26:32 +020012451 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12452 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12453 rule->arg.capid.idx);
12454 return 0;
12455 }
12456
12457 return 1;
12458}
12459
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012460/* parse an "http-response capture" action. It takes a single argument which is
12461 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12462 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012463 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012464 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012465enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12466 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012467{
12468 struct sample_expr *expr;
12469 int cur_arg;
12470 int id;
12471 char *error;
12472
12473 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12474 if (strcmp(args[cur_arg], "if") == 0 ||
12475 strcmp(args[cur_arg], "unless") == 0)
12476 break;
12477
12478 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012479 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012480 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012481 }
12482
12483 cur_arg = *orig_arg;
12484 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12485 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012486 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012487
12488 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12489 memprintf(err,
12490 "fetch method '%s' extracts information from '%s', none of which is available here",
12491 args[cur_arg-1], sample_src_names(expr->fetch->use));
12492 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012493 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012494 }
12495
12496 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012497 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012498 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012499 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012500 }
12501
12502 if (strcmp(args[cur_arg], "id") != 0) {
12503 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12504 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012505 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012506 }
12507
12508 cur_arg++;
12509
12510 if (!args[cur_arg]) {
12511 memprintf(err, "missing id value");
12512 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012513 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012514 }
12515
12516 id = strtol(args[cur_arg], &error, 10);
12517 if (*error != '\0') {
12518 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12519 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012520 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012521 }
12522 cur_arg++;
12523
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012524 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012525
Thierry FOURNIER42148732015-09-02 17:17:33 +020012526 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012527 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012528 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012529 rule->arg.capid.expr = expr;
12530 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012531
12532 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012533 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012534}
12535
William Lallemand73025dd2014-04-24 14:38:37 +020012536/*
12537 * Return the struct http_req_action_kw associated to a keyword.
12538 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012539struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012540{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012541 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012542}
12543
12544/*
12545 * Return the struct http_res_action_kw associated to a keyword.
12546 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012547struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012548{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012549 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012550}
12551
Willy Tarreau12207b32016-11-22 19:48:51 +010012552
Willy Tarreau4a568972010-05-12 08:08:50 +020012553/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012554/* All supported ACL keywords must be declared here. */
12555/************************************************************************/
12556
12557/* Note: must not be declared <const> as its list will be overwritten.
12558 * Please take care of keeping this list alphabetically sorted.
12559 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012560static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012561 { "base", "base", PAT_MATCH_STR },
12562 { "base_beg", "base", PAT_MATCH_BEG },
12563 { "base_dir", "base", PAT_MATCH_DIR },
12564 { "base_dom", "base", PAT_MATCH_DOM },
12565 { "base_end", "base", PAT_MATCH_END },
12566 { "base_len", "base", PAT_MATCH_LEN },
12567 { "base_reg", "base", PAT_MATCH_REG },
12568 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012569
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012570 { "cook", "req.cook", PAT_MATCH_STR },
12571 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12572 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12573 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12574 { "cook_end", "req.cook", PAT_MATCH_END },
12575 { "cook_len", "req.cook", PAT_MATCH_LEN },
12576 { "cook_reg", "req.cook", PAT_MATCH_REG },
12577 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012578
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012579 { "hdr", "req.hdr", PAT_MATCH_STR },
12580 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12581 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12582 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12583 { "hdr_end", "req.hdr", PAT_MATCH_END },
12584 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12585 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12586 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012587
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012588 /* these two declarations uses strings with list storage (in place
12589 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12590 * and delete functions are relative to the list management. The parse
12591 * and match method are related to the corresponding fetch methods. This
12592 * is very particular ACL declaration mode.
12593 */
12594 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12595 { "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 +020012596
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012597 { "path", "path", PAT_MATCH_STR },
12598 { "path_beg", "path", PAT_MATCH_BEG },
12599 { "path_dir", "path", PAT_MATCH_DIR },
12600 { "path_dom", "path", PAT_MATCH_DOM },
12601 { "path_end", "path", PAT_MATCH_END },
12602 { "path_len", "path", PAT_MATCH_LEN },
12603 { "path_reg", "path", PAT_MATCH_REG },
12604 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012605
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012606 { "req_ver", "req.ver", PAT_MATCH_STR },
12607 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012608
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012609 { "scook", "res.cook", PAT_MATCH_STR },
12610 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12611 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12612 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12613 { "scook_end", "res.cook", PAT_MATCH_END },
12614 { "scook_len", "res.cook", PAT_MATCH_LEN },
12615 { "scook_reg", "res.cook", PAT_MATCH_REG },
12616 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012617
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012618 { "shdr", "res.hdr", PAT_MATCH_STR },
12619 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12620 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12621 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12622 { "shdr_end", "res.hdr", PAT_MATCH_END },
12623 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12624 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12625 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012626
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012627 { "url", "url", PAT_MATCH_STR },
12628 { "url_beg", "url", PAT_MATCH_BEG },
12629 { "url_dir", "url", PAT_MATCH_DIR },
12630 { "url_dom", "url", PAT_MATCH_DOM },
12631 { "url_end", "url", PAT_MATCH_END },
12632 { "url_len", "url", PAT_MATCH_LEN },
12633 { "url_reg", "url", PAT_MATCH_REG },
12634 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012635
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012636 { "urlp", "urlp", PAT_MATCH_STR },
12637 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12638 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12639 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12640 { "urlp_end", "urlp", PAT_MATCH_END },
12641 { "urlp_len", "urlp", PAT_MATCH_LEN },
12642 { "urlp_reg", "urlp", PAT_MATCH_REG },
12643 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012644
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012645 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012646}};
12647
12648/************************************************************************/
12649/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012650/************************************************************************/
12651/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012652static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012653 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012654 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012655 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12656
Willy Tarreau87b09662015-04-03 00:22:06 +020012657 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012658 { "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 +020012659
12660 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012661 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12662 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12663 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012664
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012665 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12666 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012667
Willy Tarreau409bcde2013-01-08 00:31:00 +010012668 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12669 * are only here to match the ACL's name, are request-only and are used
12670 * for ACL compatibility only.
12671 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012672 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12673 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012674 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12675 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012676
12677 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12678 * only here to match the ACL's name, are request-only and are used for
12679 * ACL compatibility only.
12680 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012681 { "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 +020012682 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012683 { "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 +020012684 { "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 +010012685
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012686 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012687 { "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 +010012688 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012689 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012690 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012691 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012692
12693 /* HTTP protocol on the request path */
12694 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012695 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012696
12697 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012698 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12699 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012700
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012701 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012702 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12703 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012704 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012705
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012706 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012707 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12708
Willy Tarreau18ed2562013-01-14 15:56:36 +010012709 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012710 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12711 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012712
Willy Tarreau18ed2562013-01-14 15:56:36 +010012713 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012714 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012715 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12716 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012717
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012718 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012719 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012720 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012721 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012722 { "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 +010012723 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012724 { "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 +010012725
12726 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012727 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012728 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12729 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012730
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012731 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012732 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012733 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012734 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012735 { "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 +010012736 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012737 { "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 +010012738
Willy Tarreau409bcde2013-01-08 00:31:00 +010012739 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012740 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012741 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12742 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012743 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012744
12745 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012746 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012747 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012748 { "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 +020012749 { "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 +010012750
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012751 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012752 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012753 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012754 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012755 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012756 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012757 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012758 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12759 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012760 { "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 +010012761 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012762}};
12763
Willy Tarreau8797c062007-05-07 00:55:35 +020012764
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012765/************************************************************************/
12766/* All supported converter keywords must be declared here. */
12767/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012768/* Note: must not be declared <const> as its list will be overwritten */
12769static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012770 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012771 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012772 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12773 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012774 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012775 { NULL, NULL, 0, 0, 0 },
12776}};
12777
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012778
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012779/************************************************************************/
12780/* All supported http-request action keywords must be declared here. */
12781/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012782struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012783 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012784 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012785 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012786 { "set-method", parse_set_req_line },
12787 { "set-path", parse_set_req_line },
12788 { "set-query", parse_set_req_line },
12789 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012790 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012791 }
12792};
12793
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012794struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012795 .kw = {
12796 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012797 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012798 { NULL, NULL }
12799 }
12800};
12801
Willy Tarreau8797c062007-05-07 00:55:35 +020012802__attribute__((constructor))
12803static void __http_protocol_init(void)
12804{
12805 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012806 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012807 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012808 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012809 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012810}
12811
12812
Willy Tarreau58f10d72006-12-04 02:26:12 +010012813/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012814 * Local variables:
12815 * c-indent-level: 8
12816 * c-basic-offset: 8
12817 * End:
12818 */