blob: afeb96ee52d4ac47743b4bd2c31b660f6b8366e3 [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>
Willy Tarreauc7e42382012-08-24 19:22:53 +020029#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010045#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau8797c062007-05-07 00:55:35 +020048#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020049#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020050#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010051#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020053#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010054#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010055#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020056#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010057#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020059#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020060#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020061#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020062#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010063#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010064#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020065#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010067#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020069#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010070#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020071#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010072#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020073#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020074#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020075#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020076
Willy Tarreau522d6c02009-12-06 18:49:18 +010077const char HTTP_100[] =
78 "HTTP/1.1 100 Continue\r\n\r\n";
79
80const struct chunk http_100_chunk = {
81 .str = (char *)&HTTP_100,
82 .len = sizeof(HTTP_100)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020089 "Location: "; /* not terminated since it will be concatenated with the URL */
90
Willy Tarreau0f772532006-12-23 20:51:41 +010091const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
97/* same as 302 except that the browser MUST retry with the GET method */
98const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100101 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400104
105/* same as 302 except that the browser MUST retry with the same method */
106const char *HTTP_307 =
107 "HTTP/1.1 307 Temporary Redirect\r\n"
108 "Cache-Control: no-cache\r\n"
109 "Content-length: 0\r\n"
110 "Location: "; /* not terminated since it will be concatenated with the URL */
111
112/* same as 301 except that the browser MUST retry with the same method */
113const char *HTTP_308 =
114 "HTTP/1.1 308 Permanent Redirect\r\n"
115 "Content-length: 0\r\n"
116 "Location: "; /* not terminated since it will be concatenated with the URL */
117
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
119const char *HTTP_401_fmt =
120 "HTTP/1.0 401 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200123 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128const char *HTTP_407_fmt =
129 "HTTP/1.0 407 Unauthorized\r\n"
130 "Cache-Control: no-cache\r\n"
131 "Connection: close\r\n"
132 "Content-Type: text/html\r\n"
133 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
134 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800135 "<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 +0100136
Willy Tarreau0f772532006-12-23 20:51:41 +0100137
138const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200139 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100140 [HTTP_ERR_400] = 400,
141 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400142 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100143 [HTTP_ERR_408] = 408,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200144 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400145 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100146 [HTTP_ERR_500] = 500,
147 [HTTP_ERR_502] = 502,
148 [HTTP_ERR_503] = 503,
149 [HTTP_ERR_504] = 504,
150};
151
Willy Tarreau80587432006-12-24 17:47:20 +0100152static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200153 [HTTP_ERR_200] =
154 "HTTP/1.0 200 OK\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
160
Willy Tarreau0f772532006-12-23 20:51:41 +0100161 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100162 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
168
169 [HTTP_ERR_403] =
170 "HTTP/1.0 403 Forbidden\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
176
CJ Ess108b1dd2015-04-07 12:03:37 -0400177 [HTTP_ERR_405] =
178 "HTTP/1.0 405 Method Not Allowed\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<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",
184
Willy Tarreau0f772532006-12-23 20:51:41 +0100185 [HTTP_ERR_408] =
186 "HTTP/1.0 408 Request Time-out\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
192
Olivier Houchard51a76d82017-10-02 16:12:07 +0200193 [HTTP_ERR_425] =
194 "HTTP/1.0 425 Too Early\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
200
CJ Ess108b1dd2015-04-07 12:03:37 -0400201 [HTTP_ERR_429] =
202 "HTTP/1.0 429 Too Many Requests\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
208
Willy Tarreau0f772532006-12-23 20:51:41 +0100209 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200210 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200215 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100216
217 [HTTP_ERR_502] =
218 "HTTP/1.0 502 Bad Gateway\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
224
225 [HTTP_ERR_503] =
226 "HTTP/1.0 503 Service Unavailable\r\n"
227 "Cache-Control: no-cache\r\n"
228 "Connection: close\r\n"
229 "Content-Type: text/html\r\n"
230 "\r\n"
231 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
232
233 [HTTP_ERR_504] =
234 "HTTP/1.0 504 Gateway Time-out\r\n"
235 "Cache-Control: no-cache\r\n"
236 "Connection: close\r\n"
237 "Content-Type: text/html\r\n"
238 "\r\n"
239 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
240
241};
242
Cyril Bonté19979e12012-04-04 12:57:21 +0200243/* status codes available for the stats admin page (strictly 4 chars length) */
244const char *stat_status_codes[STAT_STATUS_SIZE] = {
245 [STAT_STATUS_DENY] = "DENY",
246 [STAT_STATUS_DONE] = "DONE",
247 [STAT_STATUS_ERRP] = "ERRP",
248 [STAT_STATUS_EXCD] = "EXCD",
249 [STAT_STATUS_NONE] = "NONE",
250 [STAT_STATUS_PART] = "PART",
251 [STAT_STATUS_UNKN] = "UNKN",
252};
253
254
William Lallemand73025dd2014-04-24 14:38:37 +0200255/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200256struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200257 .list = LIST_HEAD_INIT(http_req_keywords.list)
258};
259
260/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200261struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200262 .list = LIST_HEAD_INIT(http_res_keywords.list)
263};
264
Willy Tarreau80587432006-12-24 17:47:20 +0100265/* We must put the messages here since GCC cannot initialize consts depending
266 * on strlen().
267 */
268struct chunk http_err_chunks[HTTP_ERR_SIZE];
269
Willy Tarreaua890d072013-04-02 12:01:06 +0200270/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100271static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200272
Willy Tarreau42250582007-04-01 01:30:43 +0200273#define FD_SETS_ARE_BITFIELDS
274#ifdef FD_SETS_ARE_BITFIELDS
275/*
276 * This map is used with all the FD_* macros to check whether a particular bit
277 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
278 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
279 * byte should be encoded. Be careful to always pass bytes from 0 to 255
280 * exclusively to the macros.
281 */
282fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
283fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100284fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200285
286#else
287#error "Check if your OS uses bitfields for fd_sets"
288#endif
289
Willy Tarreau87b09662015-04-03 00:22:06 +0200290static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200291
David Carlier7365f7d2016-04-04 11:54:42 +0100292static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
293static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100294
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200295/* This function returns a reason associated with the HTTP status.
296 * This function never fails, a message is always returned.
297 */
298const char *get_reason(unsigned int status)
299{
300 switch (status) {
301 case 100: return "Continue";
302 case 101: return "Switching Protocols";
303 case 102: return "Processing";
304 case 200: return "OK";
305 case 201: return "Created";
306 case 202: return "Accepted";
307 case 203: return "Non-Authoritative Information";
308 case 204: return "No Content";
309 case 205: return "Reset Content";
310 case 206: return "Partial Content";
311 case 207: return "Multi-Status";
312 case 210: return "Content Different";
313 case 226: return "IM Used";
314 case 300: return "Multiple Choices";
315 case 301: return "Moved Permanently";
316 case 302: return "Moved Temporarily";
317 case 303: return "See Other";
318 case 304: return "Not Modified";
319 case 305: return "Use Proxy";
320 case 307: return "Temporary Redirect";
321 case 308: return "Permanent Redirect";
322 case 310: return "Too many Redirects";
323 case 400: return "Bad Request";
324 case 401: return "Unauthorized";
325 case 402: return "Payment Required";
326 case 403: return "Forbidden";
327 case 404: return "Not Found";
328 case 405: return "Method Not Allowed";
329 case 406: return "Not Acceptable";
330 case 407: return "Proxy Authentication Required";
331 case 408: return "Request Time-out";
332 case 409: return "Conflict";
333 case 410: return "Gone";
334 case 411: return "Length Required";
335 case 412: return "Precondition Failed";
336 case 413: return "Request Entity Too Large";
337 case 414: return "Request-URI Too Long";
338 case 415: return "Unsupported Media Type";
339 case 416: return "Requested range unsatisfiable";
340 case 417: return "Expectation failed";
341 case 418: return "I'm a teapot";
342 case 422: return "Unprocessable entity";
343 case 423: return "Locked";
344 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200345 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200346 case 426: return "Upgrade Required";
347 case 428: return "Precondition Required";
348 case 429: return "Too Many Requests";
349 case 431: return "Request Header Fields Too Large";
350 case 449: return "Retry With";
351 case 450: return "Blocked by Windows Parental Controls";
352 case 451: return "Unavailable For Legal Reasons";
353 case 456: return "Unrecoverable Error";
354 case 499: return "client has closed connection";
355 case 500: return "Internal Server Error";
356 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200357 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200358 case 503: return "Service Unavailable";
359 case 504: return "Gateway Time-out";
360 case 505: return "HTTP Version not supported";
361 case 506: return "Variant also negociate";
362 case 507: return "Insufficient storage";
363 case 508: return "Loop detected";
364 case 509: return "Bandwidth Limit Exceeded";
365 case 510: return "Not extended";
366 case 511: return "Network authentication required";
367 case 520: return "Web server is returning an unknown error";
368 default:
369 switch (status) {
370 case 100 ... 199: return "Informational";
371 case 200 ... 299: return "Success";
372 case 300 ... 399: return "Redirection";
373 case 400 ... 499: return "Client Error";
374 case 500 ... 599: return "Server Error";
375 default: return "Other";
376 }
377 }
378}
379
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200380/* This function returns HTTP_ERR_<num> (enum) matching http status code.
381 * Returned value should match codes from http_err_codes.
382 */
383static const int http_get_status_idx(unsigned int status)
384{
385 switch (status) {
386 case 200: return HTTP_ERR_200;
387 case 400: return HTTP_ERR_400;
388 case 403: return HTTP_ERR_403;
389 case 405: return HTTP_ERR_405;
390 case 408: return HTTP_ERR_408;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200391 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200392 case 429: return HTTP_ERR_429;
393 case 500: return HTTP_ERR_500;
394 case 502: return HTTP_ERR_502;
395 case 503: return HTTP_ERR_503;
396 case 504: return HTTP_ERR_504;
397 default: return HTTP_ERR_500;
398 }
399}
400
Willy Tarreau80587432006-12-24 17:47:20 +0100401void init_proto_http()
402{
Willy Tarreau42250582007-04-01 01:30:43 +0200403 int i;
404 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100405 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200406
Willy Tarreau80587432006-12-24 17:47:20 +0100407 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
408 if (!http_err_msgs[msg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100409 ha_alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
Willy Tarreau80587432006-12-24 17:47:20 +0100410 abort();
411 }
412
413 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
414 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
415 }
Willy Tarreau42250582007-04-01 01:30:43 +0200416
417 /* initialize the log header encoding map : '{|}"#' should be encoded with
418 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
419 * URL encoding only requires '"', '#' to be encoded as well as non-
420 * printable characters above.
421 */
422 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
423 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100424 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200425 for (i = 0; i < 32; i++) {
426 FD_SET(i, hdr_encode_map);
427 FD_SET(i, url_encode_map);
428 }
429 for (i = 127; i < 256; i++) {
430 FD_SET(i, hdr_encode_map);
431 FD_SET(i, url_encode_map);
432 }
433
434 tmp = "\"#{|}";
435 while (*tmp) {
436 FD_SET(*tmp, hdr_encode_map);
437 tmp++;
438 }
439
440 tmp = "\"#";
441 while (*tmp) {
442 FD_SET(*tmp, url_encode_map);
443 tmp++;
444 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200445
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100446 /* initialize the http header encoding map. The draft httpbis define the
447 * header content as:
448 *
449 * HTTP-message = start-line
450 * *( header-field CRLF )
451 * CRLF
452 * [ message-body ]
453 * header-field = field-name ":" OWS field-value OWS
454 * field-value = *( field-content / obs-fold )
455 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
456 * obs-fold = CRLF 1*( SP / HTAB )
457 * field-vchar = VCHAR / obs-text
458 * VCHAR = %x21-7E
459 * obs-text = %x80-FF
460 *
461 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
462 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
463 * "obs-fold" is volontary forgotten because haproxy remove this.
464 */
465 memset(http_encode_map, 0, sizeof(http_encode_map));
466 for (i = 0x00; i <= 0x08; i++)
467 FD_SET(i, http_encode_map);
468 for (i = 0x0a; i <= 0x1f; i++)
469 FD_SET(i, http_encode_map);
470 FD_SET(0x7f, http_encode_map);
471
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200472 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100473 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
474 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100475}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476
Willy Tarreau53b6c742006-12-17 13:37:46 +0100477/*
478 * We have 26 list of methods (1 per first letter), each of which can have
479 * up to 3 entries (2 valid, 1 null).
480 */
481struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100482 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100483 int len;
484 const char text[8];
485};
486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100487const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100488 ['C' - 'A'] = {
489 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
490 },
491 ['D' - 'A'] = {
492 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
493 },
494 ['G' - 'A'] = {
495 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
496 },
497 ['H' - 'A'] = {
498 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
499 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200500 ['O' - 'A'] = {
501 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
502 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100503 ['P' - 'A'] = {
504 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
505 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
506 },
507 ['T' - 'A'] = {
508 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
509 },
510 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200511 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100512 */
513};
514
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100515const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100516 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
517 [HTTP_METH_GET] = { "GET", 3 },
518 [HTTP_METH_HEAD] = { "HEAD", 4 },
519 [HTTP_METH_POST] = { "POST", 4 },
520 [HTTP_METH_PUT] = { "PUT", 3 },
521 [HTTP_METH_DELETE] = { "DELETE", 6 },
522 [HTTP_METH_TRACE] = { "TRACE", 5 },
523 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
524};
525
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100526/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100527 * Adds a header and its CRLF at the tail of the message's buffer, just before
528 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100529 * The header is also automatically added to the index <hdr_idx>, and the end
530 * of headers is automatically adjusted. The number of bytes added is returned
531 * on success, otherwise <0 is returned indicating an error.
532 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100533int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100534{
535 int bytes, len;
536
537 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200538 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100539 if (!bytes)
540 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100541 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100542 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
543}
544
545/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100546 * Adds a header and its CRLF at the tail of the message's buffer, just before
547 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100548 * the buffer is only opened and the space reserved, but nothing is copied.
549 * The header is also automatically added to the index <hdr_idx>, and the end
550 * of headers is automatically adjusted. The number of bytes added is returned
551 * on success, otherwise <0 is returned indicating an error.
552 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100553int http_header_add_tail2(struct http_msg *msg,
554 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100555{
556 int bytes;
557
Willy Tarreau9b28e032012-10-12 23:49:43 +0200558 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100559 if (!bytes)
560 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100561 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100562 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
563}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564
565/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100566 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
567 * If so, returns the position of the first non-space character relative to
568 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
569 * to return a pointer to the place after the first space. Returns 0 if the
570 * header name does not match. Checks are case-insensitive.
571 */
572int http_header_match2(const char *hdr, const char *end,
573 const char *name, int len)
574{
575 const char *val;
576
577 if (hdr + len >= end)
578 return 0;
579 if (hdr[len] != ':')
580 return 0;
581 if (strncasecmp(hdr, name, len) != 0)
582 return 0;
583 val = hdr + len + 1;
584 while (val < end && HTTP_IS_SPHT(*val))
585 val++;
586 if ((val >= end) && (len + 2 <= end - hdr))
587 return len + 2; /* we may replace starting from second space */
588 return val - hdr;
589}
590
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200591/* Find the first or next occurrence of header <name> in message buffer <sol>
592 * using headers index <idx>, and return it in the <ctx> structure. This
593 * structure holds everything necessary to use the header and find next
594 * occurrence. If its <idx> member is 0, the header is searched from the
595 * beginning. Otherwise, the next occurrence is returned. The function returns
596 * 1 when it finds a value, and 0 when there is no more. It is very similar to
597 * http_find_header2() except that it is designed to work with full-line headers
598 * whose comma is not a delimiter but is part of the syntax. As a special case,
599 * if ctx->val is NULL when searching for a new values of a header, the current
600 * header is rescanned. This allows rescanning after a header deletion.
601 */
602int http_find_full_header2(const char *name, int len,
603 char *sol, struct hdr_idx *idx,
604 struct hdr_ctx *ctx)
605{
606 char *eol, *sov;
607 int cur_idx, old_idx;
608
609 cur_idx = ctx->idx;
610 if (cur_idx) {
611 /* We have previously returned a header, let's search another one */
612 sol = ctx->line;
613 eol = sol + idx->v[cur_idx].len;
614 goto next_hdr;
615 }
616
617 /* first request for this header */
618 sol += hdr_idx_first_pos(idx);
619 old_idx = 0;
620 cur_idx = hdr_idx_first_idx(idx);
621 while (cur_idx) {
622 eol = sol + idx->v[cur_idx].len;
623
624 if (len == 0) {
625 /* No argument was passed, we want any header.
626 * To achieve this, we simply build a fake request. */
627 while (sol + len < eol && sol[len] != ':')
628 len++;
629 name = sol;
630 }
631
632 if ((len < eol - sol) &&
633 (sol[len] == ':') &&
634 (strncasecmp(sol, name, len) == 0)) {
635 ctx->del = len;
636 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100637 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200638 sov++;
639
640 ctx->line = sol;
641 ctx->prev = old_idx;
642 ctx->idx = cur_idx;
643 ctx->val = sov - sol;
644 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100645 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200646 eol--;
647 ctx->tws++;
648 }
649 ctx->vlen = eol - sov;
650 return 1;
651 }
652 next_hdr:
653 sol = eol + idx->v[cur_idx].cr + 1;
654 old_idx = cur_idx;
655 cur_idx = idx->v[cur_idx].next;
656 }
657 return 0;
658}
659
Willy Tarreauc90dc232015-02-20 13:51:36 +0100660/* Find the first or next header field in message buffer <sol> using headers
661 * index <idx>, and return it in the <ctx> structure. This structure holds
662 * everything necessary to use the header and find next occurrence. If its
663 * <idx> member is 0, the first header is retrieved. Otherwise, the next
664 * occurrence is returned. The function returns 1 when it finds a value, and
665 * 0 when there is no more. It is equivalent to http_find_full_header2() with
666 * no header name.
667 */
668int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
669{
670 char *eol, *sov;
671 int cur_idx, old_idx;
672 int len;
673
674 cur_idx = ctx->idx;
675 if (cur_idx) {
676 /* We have previously returned a header, let's search another one */
677 sol = ctx->line;
678 eol = sol + idx->v[cur_idx].len;
679 goto next_hdr;
680 }
681
682 /* first request for this header */
683 sol += hdr_idx_first_pos(idx);
684 old_idx = 0;
685 cur_idx = hdr_idx_first_idx(idx);
686 while (cur_idx) {
687 eol = sol + idx->v[cur_idx].len;
688
689 len = 0;
690 while (1) {
691 if (len >= eol - sol)
692 goto next_hdr;
693 if (sol[len] == ':')
694 break;
695 len++;
696 }
697
698 ctx->del = len;
699 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100700 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100701 sov++;
702
703 ctx->line = sol;
704 ctx->prev = old_idx;
705 ctx->idx = cur_idx;
706 ctx->val = sov - sol;
707 ctx->tws = 0;
708
Willy Tarreau2235b262016-11-05 15:50:20 +0100709 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100710 eol--;
711 ctx->tws++;
712 }
713 ctx->vlen = eol - sov;
714 return 1;
715
716 next_hdr:
717 sol = eol + idx->v[cur_idx].cr + 1;
718 old_idx = cur_idx;
719 cur_idx = idx->v[cur_idx].next;
720 }
721 return 0;
722}
723
Lukas Tribus23953682017-04-28 13:24:30 +0000724/* Find the end of the header value contained between <s> and <e>. See RFC7230,
725 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100726 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200727 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100728char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200729{
730 int quoted, qdpair;
731
732 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100733
734#if defined(__x86_64__) || \
735 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
736 defined(__ARM_ARCH_7A__)
737 /* speedup: skip everything not a comma nor a double quote */
738 for (; s <= e - sizeof(int); s += sizeof(int)) {
739 unsigned int c = *(int *)s; // comma
740 unsigned int q = c; // quote
741
742 c ^= 0x2c2c2c2c; // contains one zero on a comma
743 q ^= 0x22222222; // contains one zero on a quote
744
745 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
746 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
747
748 if ((c | q) & 0x80808080)
749 break; // found a comma or a quote
750 }
751#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200752 for (; s < e; s++) {
753 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200754 else if (quoted) {
755 if (*s == '\\') qdpair = 1;
756 else if (*s == '"') quoted = 0;
757 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200758 else if (*s == '"') quoted = 1;
759 else if (*s == ',') return s;
760 }
761 return s;
762}
763
764/* Find the first or next occurrence of header <name> in message buffer <sol>
765 * using headers index <idx>, and return it in the <ctx> structure. This
766 * structure holds everything necessary to use the header and find next
767 * occurrence. If its <idx> member is 0, the header is searched from the
768 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100769 * 1 when it finds a value, and 0 when there is no more. It is designed to work
770 * with headers defined as comma-separated lists. As a special case, if ctx->val
771 * is NULL when searching for a new values of a header, the current header is
772 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200773 */
774int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100775 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200776 struct hdr_ctx *ctx)
777{
Willy Tarreau68085d82010-01-18 14:54:04 +0100778 char *eol, *sov;
779 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200780
Willy Tarreau68085d82010-01-18 14:54:04 +0100781 cur_idx = ctx->idx;
782 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200783 /* We have previously returned a value, let's search
784 * another one on the same line.
785 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200786 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200787 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100788 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200789 eol = sol + idx->v[cur_idx].len;
790
791 if (sov >= eol)
792 /* no more values in this header */
793 goto next_hdr;
794
Willy Tarreau68085d82010-01-18 14:54:04 +0100795 /* values remaining for this header, skip the comma but save it
796 * for later use (eg: for header deletion).
797 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100799 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200800 sov++;
801
802 goto return_hdr;
803 }
804
805 /* first request for this header */
806 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100807 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200808 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200809 while (cur_idx) {
810 eol = sol + idx->v[cur_idx].len;
811
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200812 if (len == 0) {
813 /* No argument was passed, we want any header.
814 * To achieve this, we simply build a fake request. */
815 while (sol + len < eol && sol[len] != ':')
816 len++;
817 name = sol;
818 }
819
Willy Tarreau33a7e692007-06-10 19:45:56 +0200820 if ((len < eol - sol) &&
821 (sol[len] == ':') &&
822 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100823 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200824 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100825 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200826 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100827
Willy Tarreau33a7e692007-06-10 19:45:56 +0200828 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100829 ctx->prev = old_idx;
830 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200831 ctx->idx = cur_idx;
832 ctx->val = sov - sol;
833
834 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200835 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100836 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200837 eol--;
838 ctx->tws++;
839 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200840 ctx->vlen = eol - sov;
841 return 1;
842 }
843 next_hdr:
844 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100845 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 cur_idx = idx->v[cur_idx].next;
847 }
848 return 0;
849}
850
851int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100852 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200853 struct hdr_ctx *ctx)
854{
855 return http_find_header2(name, strlen(name), sol, idx, ctx);
856}
857
Willy Tarreau68085d82010-01-18 14:54:04 +0100858/* Remove one value of a header. This only works on a <ctx> returned by one of
859 * the http_find_header functions. The value is removed, as well as surrounding
860 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100861 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100862 * message <msg>. The new index is returned. If it is zero, it means there is
863 * no more header, so any processing may stop. The ctx is always left in a form
864 * that can be handled by http_find_header2() to find next occurrence.
865 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100866int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100867{
868 int cur_idx = ctx->idx;
869 char *sol = ctx->line;
870 struct hdr_idx_elem *hdr;
871 int delta, skip_comma;
872
873 if (!cur_idx)
874 return 0;
875
876 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200877 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100878 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200879 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100880 http_msg_move_end(msg, delta);
881 idx->used--;
882 hdr->len = 0; /* unused entry */
883 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100884 if (idx->tail == ctx->idx)
885 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100886 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100887 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100888 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200889 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100890 return ctx->idx;
891 }
892
893 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200894 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
895 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100896 */
897
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200898 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200899 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200900 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100901 NULL, 0);
902 hdr->len += delta;
903 http_msg_move_end(msg, delta);
904 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200905 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100906 return ctx->idx;
907}
908
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100909/* This function handles a server error at the stream interface level. The
910 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100911 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100912 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100913 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200915static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100916 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917{
Willy Tarreau2019f952017-03-14 11:07:31 +0100918 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100919 channel_auto_read(si_oc(si));
920 channel_abort(si_oc(si));
921 channel_auto_close(si_oc(si));
922 channel_erase(si_oc(si));
923 channel_auto_close(si_ic(si));
924 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200925 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200926 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200927 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200928 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200929 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200930 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931}
932
Willy Tarreau87b09662015-04-03 00:22:06 +0200933/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100934 * and message.
935 */
936
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200937struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100938{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200939 const int msgnum = http_get_status_idx(s->txn->status);
940
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200941 if (s->be->errmsg[msgnum].str)
942 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200943 else if (strm_fe(s)->errmsg[msgnum].str)
944 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100945 else
946 return &http_err_chunks[msgnum];
947}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200948
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100949void
950http_reply_and_close(struct stream *s, short status, struct chunk *msg)
951{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200952 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100953 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100954 stream_int_retnclose(&s->si[0], msg);
955}
956
Willy Tarreau53b6c742006-12-17 13:37:46 +0100957/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200958 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
959 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100960 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100961enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100962{
963 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100964 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100965
966 m = ((unsigned)*str - 'A');
967
968 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100969 for (h = http_methods[m]; h->len > 0; h++) {
970 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100971 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100972 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100973 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100975 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200976 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977}
978
Willy Tarreau21d2af32008-02-14 20:25:24 +0100979/* Parse the URI from the given transaction (which is assumed to be in request
980 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
981 * It is returned otherwise.
982 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200983char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100984{
985 char *ptr, *end;
986
Willy Tarreau9b28e032012-10-12 23:49:43 +0200987 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100988 end = ptr + txn->req.sl.rq.u_l;
989
990 if (ptr >= end)
991 return NULL;
992
Lukas Tribus23953682017-04-28 13:24:30 +0000993 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100994 * Request-URI = "*" | absuri | abspath | authority
995 */
996
997 if (*ptr == '*')
998 return NULL;
999
1000 if (isalpha((unsigned char)*ptr)) {
1001 /* this is a scheme as described by RFC3986, par. 3.1 */
1002 ptr++;
1003 while (ptr < end &&
1004 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1005 ptr++;
1006 /* skip '://' */
1007 if (ptr == end || *ptr++ != ':')
1008 return NULL;
1009 if (ptr == end || *ptr++ != '/')
1010 return NULL;
1011 if (ptr == end || *ptr++ != '/')
1012 return NULL;
1013 }
1014 /* skip [user[:passwd]@]host[:[port]] */
1015
1016 while (ptr < end && *ptr != '/')
1017 ptr++;
1018
1019 if (ptr == end)
1020 return NULL;
1021
1022 /* OK, we got the '/' ! */
1023 return ptr;
1024}
1025
William Lallemand65ad6e12014-01-31 15:08:02 +01001026/* Parse the URI from the given string and look for the "/" beginning the PATH.
1027 * If not found, return NULL. It is returned otherwise.
1028 */
1029static char *
1030http_get_path_from_string(char *str)
1031{
1032 char *ptr = str;
1033
1034 /* RFC2616, par. 5.1.2 :
1035 * Request-URI = "*" | absuri | abspath | authority
1036 */
1037
1038 if (*ptr == '*')
1039 return NULL;
1040
1041 if (isalpha((unsigned char)*ptr)) {
1042 /* this is a scheme as described by RFC3986, par. 3.1 */
1043 ptr++;
1044 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1045 ptr++;
1046 /* skip '://' */
1047 if (*ptr == '\0' || *ptr++ != ':')
1048 return NULL;
1049 if (*ptr == '\0' || *ptr++ != '/')
1050 return NULL;
1051 if (*ptr == '\0' || *ptr++ != '/')
1052 return NULL;
1053 }
1054 /* skip [user[:passwd]@]host[:[port]] */
1055
1056 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1057 ptr++;
1058
1059 if (*ptr == '\0' || *ptr == ' ')
1060 return NULL;
1061
1062 /* OK, we got the '/' ! */
1063 return ptr;
1064}
1065
Willy Tarreau71241ab2012-12-27 11:30:54 +01001066/* Returns a 302 for a redirectable request that reaches a server working in
1067 * in redirect mode. This may only be called just after the stream interface
1068 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1069 * follow normal proxy processing. NOTE: this function is designed to support
1070 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001072void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001073{
1074 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001075 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001076 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001077 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001078
1079 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001080 trash.len = strlen(HTTP_302);
1081 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001083 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001084
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001086 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001087 return;
1088
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001089 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001090 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001091 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1092 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001093 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001094
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001095 /* 3: add the request URI. Since it was already forwarded, we need
1096 * to temporarily rewind the buffer.
1097 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001098 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001099 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001100
Willy Tarreauefb453c2008-10-26 20:49:47 +01001101 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001102 len = buffer_count(s->req.buf, path, b_ptr(s->req.buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001103
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001104 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001105
Willy Tarreauefb453c2008-10-26 20:49:47 +01001106 if (!path)
1107 return;
1108
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001109 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001110 return;
1111
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001112 memcpy(trash.str + trash.len, path, len);
1113 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001114
1115 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001116 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1117 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001118 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001119 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1120 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001121 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001122
1123 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001124 si_shutr(si);
1125 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001126 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001127 si->state = SI_ST_CLO;
1128
1129 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001130 txn->status = 302;
1131 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001132
1133 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001134 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001135 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001136}
1137
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001138/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001139 * that the server side is closed. Note that err_type is actually a
1140 * bitmask, where almost only aborts may be cumulated with other
1141 * values. We consider that aborted operations are more important
1142 * than timeouts or errors due to the fact that nobody else in the
1143 * logs might explain incomplete retries. All others should avoid
1144 * being cumulated. It should normally not be possible to have multiple
1145 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001146 * Note that connection errors appearing on the second request of a keep-alive
1147 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001148 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001149void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001150{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001151 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001152
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001153 /* set s->txn->status for http_error_message(s) */
1154 s->txn->status = 503;
1155
Willy Tarreauefb453c2008-10-26 20:49:47 +01001156 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001157 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001158 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001159 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001160 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001161 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001162 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001163 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001164 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001165 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001166 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001167 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001168 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001169 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001170 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001171 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001172 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001173 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001174 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001175 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001176 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001177 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001178 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001179 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001180 http_error_message(s));
1181 else { /* SI_ET_CONN_OTHER and others */
1182 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001183 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001184 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001185 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001186}
1187
Willy Tarreau42250582007-04-01 01:30:43 +02001188extern const char sess_term_cond[8];
1189extern const char sess_fin_state[8];
1190extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001191struct pool_head *pool_head_http_txn;
1192struct pool_head *pool_head_requri;
1193struct pool_head *pool_head_capture = NULL;
1194struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001195
Willy Tarreau117f59e2007-03-04 18:17:17 +01001196/*
1197 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001198 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001199 */
1200void capture_headers(char *som, struct hdr_idx *idx,
1201 char **cap, struct cap_hdr *cap_hdr)
1202{
1203 char *eol, *sol, *col, *sov;
1204 int cur_idx;
1205 struct cap_hdr *h;
1206 int len;
1207
1208 sol = som + hdr_idx_first_pos(idx);
1209 cur_idx = hdr_idx_first_idx(idx);
1210
1211 while (cur_idx) {
1212 eol = sol + idx->v[cur_idx].len;
1213
1214 col = sol;
1215 while (col < eol && *col != ':')
1216 col++;
1217
1218 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001219 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001220 sov++;
1221
1222 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001223 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001224 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1225 if (cap[h->index] == NULL)
1226 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001227 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228
1229 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001230 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001231 continue;
1232 }
1233
1234 len = eol - sov;
1235 if (len > h->len)
1236 len = h->len;
1237
1238 memcpy(cap[h->index], sov, len);
1239 cap[h->index][len]=0;
1240 }
1241 }
1242 sol = eol + idx->v[cur_idx].cr + 1;
1243 cur_idx = idx->v[cur_idx].next;
1244 }
1245}
1246
Willy Tarreaubaaee002006-06-26 02:48:02 +02001247/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248 * Returns the data from Authorization header. Function may be called more
1249 * than once so data is stored in txn->auth_data. When no header is found
1250 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001251 * searching again for something we are unable to find anyway. However, if
1252 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001253 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001254 */
1255
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001256int
Willy Tarreau87b09662015-04-03 00:22:06 +02001257get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001258{
1259
Willy Tarreaueee5b512015-04-03 23:46:31 +02001260 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001261 struct chunk auth_method;
1262 struct hdr_ctx ctx;
1263 char *h, *p;
1264 int len;
1265
1266#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001267 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001268#endif
1269
1270 if (txn->auth.method == HTTP_AUTH_WRONG)
1271 return 0;
1272
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001273 txn->auth.method = HTTP_AUTH_WRONG;
1274
1275 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001276
1277 if (txn->flags & TX_USE_PX_CONN) {
1278 h = "Proxy-Authorization";
1279 len = strlen(h);
1280 } else {
1281 h = "Authorization";
1282 len = strlen(h);
1283 }
1284
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001285 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001286 return 0;
1287
1288 h = ctx.line + ctx.val;
1289
1290 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001291 len = p - h;
1292 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001293 return 0;
1294
David Carlier7365f7d2016-04-04 11:54:42 +01001295 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1296 return 0;
1297
Willy Tarreau5c557d12016-03-13 08:17:02 +01001298 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001299
1300 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001301 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001302
1303 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001304 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001305
1306 if (len < 0)
1307 return 0;
1308
1309
Christopher Faulet6988f672017-07-27 15:18:52 +02001310 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001311
Christopher Faulet6988f672017-07-27 15:18:52 +02001312 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001313
1314 if (!p)
1315 return 0;
1316
Christopher Faulet6988f672017-07-27 15:18:52 +02001317 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001318 *p = '\0';
1319 txn->auth.pass = p+1;
1320
1321 txn->auth.method = HTTP_AUTH_BASIC;
1322 return 1;
1323 }
1324
1325 return 0;
1326}
1327
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001328
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001329/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1330 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1331 * nothing is done and 1 is returned.
1332 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001333static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001334{
1335 int delta;
1336 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001337 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001338
1339 if (msg->sl.rq.v_l != 0)
1340 return 1;
1341
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001342 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1343 if (txn->meth != HTTP_METH_GET)
1344 return 0;
1345
Willy Tarreau9b28e032012-10-12 23:49:43 +02001346 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001347
1348 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001349 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1350 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001351 }
1352 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001353 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001354 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001355 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001356 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001357 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001358 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001359 NULL, NULL);
1360 if (unlikely(!cur_end))
1361 return 0;
1362
1363 /* we have a full HTTP/1.0 request now and we know that
1364 * we have either a CR or an LF at <ptr>.
1365 */
1366 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1367 return 1;
1368}
1369
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001370/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001371 * and "keep-alive" values. If we already know that some headers may safely
1372 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001373 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1374 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001375 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001376 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1377 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1378 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001379 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001380 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001381void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001382{
Willy Tarreau5b154472009-12-21 20:11:07 +01001383 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001384 const char *hdr_val = "Connection";
1385 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001386
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001387 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001388 return;
1389
Willy Tarreau88d349d2010-01-25 12:15:43 +01001390 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1391 hdr_val = "Proxy-Connection";
1392 hdr_len = 16;
1393 }
1394
Willy Tarreau5b154472009-12-21 20:11:07 +01001395 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001396 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001397 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001398 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1399 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001400 if (to_del & 2)
1401 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001402 else
1403 txn->flags |= TX_CON_KAL_SET;
1404 }
1405 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1406 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001407 if (to_del & 1)
1408 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001409 else
1410 txn->flags |= TX_CON_CLO_SET;
1411 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001412 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1413 txn->flags |= TX_HDR_CONN_UPG;
1414 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001415 }
1416
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001417 txn->flags |= TX_HDR_CONN_PRS;
1418 return;
1419}
Willy Tarreau5b154472009-12-21 20:11:07 +01001420
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001421/* Apply desired changes on the Connection: header. Values may be removed and/or
1422 * added depending on the <wanted> flags, which are exclusively composed of
1423 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1424 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1425 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001426void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001427{
1428 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001429 const char *hdr_val = "Connection";
1430 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001431
1432 ctx.idx = 0;
1433
Willy Tarreau88d349d2010-01-25 12:15:43 +01001434
1435 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1436 hdr_val = "Proxy-Connection";
1437 hdr_len = 16;
1438 }
1439
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001440 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001441 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001442 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1443 if (wanted & TX_CON_KAL_SET)
1444 txn->flags |= TX_CON_KAL_SET;
1445 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001446 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001447 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001448 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1449 if (wanted & TX_CON_CLO_SET)
1450 txn->flags |= TX_CON_CLO_SET;
1451 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001452 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001453 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001454 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001455
1456 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1457 return;
1458
1459 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1460 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001461 hdr_val = "Connection: close";
1462 hdr_len = 17;
1463 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1464 hdr_val = "Proxy-Connection: close";
1465 hdr_len = 23;
1466 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001467 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001468 }
1469
1470 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1471 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001472 hdr_val = "Connection: keep-alive";
1473 hdr_len = 22;
1474 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1475 hdr_val = "Proxy-Connection: keep-alive";
1476 hdr_len = 28;
1477 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001478 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001479 }
1480 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001481}
1482
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001483/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1484 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1485 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1486 * Unparsable qvalues return 1000 as "q=1.000".
1487 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001488int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001489{
1490 int q = 1000;
1491
Willy Tarreau506c69a2014-07-08 00:59:48 +02001492 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001493 goto out;
1494 q = (*qvalue++ - '0') * 1000;
1495
1496 if (*qvalue++ != '.')
1497 goto out;
1498
Willy Tarreau506c69a2014-07-08 00:59:48 +02001499 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001500 goto out;
1501 q += (*qvalue++ - '0') * 100;
1502
Willy Tarreau506c69a2014-07-08 00:59:48 +02001503 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001504 goto out;
1505 q += (*qvalue++ - '0') * 10;
1506
Willy Tarreau506c69a2014-07-08 00:59:48 +02001507 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001508 goto out;
1509 q += (*qvalue++ - '0') * 1;
1510 out:
1511 if (q > 1000)
1512 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001513 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001514 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001515 return q;
1516}
William Lallemand82fe75c2012-10-23 10:25:10 +02001517
Willy Tarreau87b09662015-04-03 00:22:06 +02001518void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001519{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001520 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001521 int tmp = TX_CON_WANT_KAL;
1522
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001523 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1524 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001525 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1526 tmp = TX_CON_WANT_TUN;
1527
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001528 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001529 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1530 tmp = TX_CON_WANT_TUN;
1531 }
1532
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001533 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001534 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1535 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001536 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001537 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1538 tmp = TX_CON_WANT_CLO;
1539 else
1540 tmp = TX_CON_WANT_SCL;
1541 }
1542
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001543 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001544 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1545 tmp = TX_CON_WANT_CLO;
1546
1547 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1548 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1549
1550 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1551 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1552 /* parse the Connection header and possibly clean it */
1553 int to_del = 0;
1554 if ((msg->flags & HTTP_MSGF_VER_11) ||
1555 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001556 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001557 to_del |= 2; /* remove "keep-alive" */
1558 if (!(msg->flags & HTTP_MSGF_VER_11))
1559 to_del |= 1; /* remove "close" */
1560 http_parse_connection_header(txn, msg, to_del);
1561 }
1562
1563 /* check if client or config asks for explicit close in KAL/SCL */
1564 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1565 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1566 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1567 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1568 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001569 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001570 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1571}
William Lallemand82fe75c2012-10-23 10:25:10 +02001572
Willy Tarreaud787e662009-07-07 10:14:51 +02001573/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1574 * processing can continue on next analysers, or zero if it either needs more
1575 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001576 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001577 * when it has nothing left to do, and may remove any analyser when it wants to
1578 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001580int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581{
Willy Tarreau59234e92008-11-30 23:51:27 +01001582 /*
1583 * We will parse the partial (or complete) lines.
1584 * We will check the request syntax, and also join multi-line
1585 * headers. An index of all the lines will be elaborated while
1586 * parsing.
1587 *
1588 * For the parsing, we use a 28 states FSM.
1589 *
1590 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001591 * req->buf->p = beginning of request
1592 * req->buf->p + msg->eoh = end of processed headers / start of current one
1593 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001594 * msg->eol = end of current header or line (LF or CRLF)
1595 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001596 *
1597 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001598 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001599 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1600 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001601 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau59234e92008-11-30 23:51:27 +01001603 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001604 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001605 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001606 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001607 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001608
Willy Tarreau87b09662015-04-03 00:22:06 +02001609 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01001610 now_ms, __FUNCTION__,
1611 s,
1612 req,
1613 req->rex, req->wex,
1614 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001615 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001616 req->analysers);
1617
Willy Tarreau52a0c602009-08-16 22:45:38 +02001618 /* we're speaking HTTP here, so let's speak HTTP to the client */
1619 s->srv_error = http_return_srv_error;
1620
Willy Tarreau83e3af02009-12-28 17:39:57 +01001621 /* There's a protected area at the end of the buffer for rewriting
1622 * purposes. We don't want to start to parse the request if the
1623 * protected area is affected, because we may have to move processed
1624 * data later, which is much more complicated.
1625 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001626 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001627
1628 /* This point is executed when some data is avalaible for analysis,
1629 * so we log the end of the idle time. */
1630 if (s->logs.t_idle == -1)
1631 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1632
Willy Tarreau379357a2013-06-08 12:55:46 +02001633 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001634 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001635 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001636 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001637 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001638 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001639 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001640 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001641 return 0;
1642 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001643 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1644 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1645 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001646 }
1647
Willy Tarreau9b28e032012-10-12 23:49:43 +02001648 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001649 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001650 }
1651
Willy Tarreau59234e92008-11-30 23:51:27 +01001652 /* 1: we might have to print this header in debug mode */
1653 if (unlikely((global.mode & MODE_DEBUG) &&
1654 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001655 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001656 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001657
Willy Tarreau9b28e032012-10-12 23:49:43 +02001658 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001659 /* this is a bit complex : in case of error on the request line,
1660 * we know that rq.l is still zero, so we display only the part
1661 * up to the end of the line (truncated by debug_hdr).
1662 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001663 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001664 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001665
Willy Tarreau59234e92008-11-30 23:51:27 +01001666 sol += hdr_idx_first_pos(&txn->hdr_idx);
1667 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001668
Willy Tarreau59234e92008-11-30 23:51:27 +01001669 while (cur_idx) {
1670 eol = sol + txn->hdr_idx.v[cur_idx].len;
1671 debug_hdr("clihdr", s, sol, eol);
1672 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1673 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001675 }
1676
Willy Tarreau58f10d72006-12-04 02:26:12 +01001677
Willy Tarreau59234e92008-11-30 23:51:27 +01001678 /*
1679 * Now we quickly check if we have found a full valid request.
1680 * If not so, we check the FD and buffer states before leaving.
1681 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001682 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001683 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001684 * on a keep-alive stream, if we encounter and error, close, t/o,
1685 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001686 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001687 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001688 * Last, we may increase some tracked counters' http request errors on
1689 * the cases that are deliberately the client's fault. For instance,
1690 * a timeout or connection reset is not counted as an error. However
1691 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001692 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001693
Willy Tarreau655dce92009-11-08 13:10:58 +01001694 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001695 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001696 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001697 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001698 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001699 stream_inc_http_req_ctr(s);
1700 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001701 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001702 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001704
Willy Tarreau59234e92008-11-30 23:51:27 +01001705 /* 1: Since we are in header mode, if there's no space
1706 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001707 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001708 * must terminate it now.
1709 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001710 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001711 /* FIXME: check if URI is set and return Status
1712 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001713 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001714 stream_inc_http_req_ctr(s);
1715 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001716 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001717 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001718 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001719 goto return_bad_req;
1720 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001721
Willy Tarreau59234e92008-11-30 23:51:27 +01001722 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001723 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001724 if (!(s->flags & SF_ERR_MASK))
1725 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001726
Willy Tarreaufcffa692010-01-10 14:21:19 +01001727 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001728 goto failed_keep_alive;
1729
Willy Tarreau0f228a02015-05-01 15:37:53 +02001730 if (sess->fe->options & PR_O_IGNORE_PRB)
1731 goto failed_keep_alive;
1732
Willy Tarreau59234e92008-11-30 23:51:27 +01001733 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001734 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001735 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001736 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001737 }
1738
Willy Tarreaudc979f22012-12-04 10:39:01 +01001739 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001740 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001741 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001742 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001743 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001744 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001745 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001746 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001747 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001748 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001749
Willy Tarreaue7dff022015-04-03 01:14:29 +02001750 if (!(s->flags & SF_FINST_MASK))
1751 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001752 return 0;
1753 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001754
Willy Tarreau59234e92008-11-30 23:51:27 +01001755 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001756 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001757 if (!(s->flags & SF_ERR_MASK))
1758 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001759
Willy Tarreaufcffa692010-01-10 14:21:19 +01001760 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001761 goto failed_keep_alive;
1762
Willy Tarreau0f228a02015-05-01 15:37:53 +02001763 if (sess->fe->options & PR_O_IGNORE_PRB)
1764 goto failed_keep_alive;
1765
Willy Tarreau59234e92008-11-30 23:51:27 +01001766 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001767 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001768 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001769 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001770 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001771 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001772 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001773 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001774 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001775 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001776
Willy Tarreau87b09662015-04-03 00:22:06 +02001777 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001778 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001779 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001780 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001781 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001782
Willy Tarreaue7dff022015-04-03 01:14:29 +02001783 if (!(s->flags & SF_FINST_MASK))
1784 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001785 return 0;
1786 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001787
Willy Tarreau59234e92008-11-30 23:51:27 +01001788 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001789 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001790 if (!(s->flags & SF_ERR_MASK))
1791 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001792
Willy Tarreaufcffa692010-01-10 14:21:19 +01001793 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001794 goto failed_keep_alive;
1795
Willy Tarreau0f228a02015-05-01 15:37:53 +02001796 if (sess->fe->options & PR_O_IGNORE_PRB)
1797 goto failed_keep_alive;
1798
Willy Tarreau4076a152009-04-02 15:18:36 +02001799 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001800 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001801 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001802 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001803 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001804 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001805 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001806 stream_inc_http_err_ctr(s);
1807 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001808 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001809 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001810 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001811 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001812
Willy Tarreaue7dff022015-04-03 01:14:29 +02001813 if (!(s->flags & SF_FINST_MASK))
1814 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001815 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001816 }
1817
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001818 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001819 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001820 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001821#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001822 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1823 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001824 /* We need more data, we have to re-enable quick-ack in case we
1825 * previously disabled it, otherwise we might cause the client
1826 * to delay next data.
1827 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001828 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001829 }
1830#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001831
Willy Tarreaufcffa692010-01-10 14:21:19 +01001832 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1833 /* If the client starts to talk, let's fall back to
1834 * request timeout processing.
1835 */
1836 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001837 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001838 }
1839
Willy Tarreau59234e92008-11-30 23:51:27 +01001840 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001841 if (!tick_isset(req->analyse_exp)) {
1842 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1843 (txn->flags & TX_WAIT_NEXT_RQ) &&
1844 tick_isset(s->be->timeout.httpka))
1845 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1846 else
1847 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1848 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001849
Willy Tarreau59234e92008-11-30 23:51:27 +01001850 /* we're not ready yet */
1851 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001852
1853 failed_keep_alive:
1854 /* Here we process low-level errors for keep-alive requests. In
1855 * short, if the request is not the first one and it experiences
1856 * a timeout, read error or shutdown, we just silently close so
1857 * that the client can try again.
1858 */
1859 txn->status = 0;
1860 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001861 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001862 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001863 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001864 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001865 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001866 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001867 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001868
Willy Tarreaud787e662009-07-07 10:14:51 +02001869 /* OK now we have a complete HTTP request with indexed headers. Let's
1870 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001871 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001872 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001873 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001874 * byte after the last LF. msg->sov points to the first byte of data.
1875 * msg->eol cannot be trusted because it may have been left uninitialized
1876 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001877 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001878
Willy Tarreau87b09662015-04-03 00:22:06 +02001879 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001880 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001881
Willy Tarreaub16a5742010-01-10 14:46:16 +01001882 if (txn->flags & TX_WAIT_NEXT_RQ) {
1883 /* kill the pending keep-alive timeout */
1884 txn->flags &= ~TX_WAIT_NEXT_RQ;
1885 req->analyse_exp = TICK_ETERNITY;
1886 }
1887
1888
Willy Tarreaud787e662009-07-07 10:14:51 +02001889 /* Maybe we found in invalid header name while we were configured not
1890 * to block on that, so we have to capture it now.
1891 */
1892 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001893 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001894
Willy Tarreau59234e92008-11-30 23:51:27 +01001895 /*
1896 * 1: identify the method
1897 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001898 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001899
1900 /* we can make use of server redirect on GET and HEAD */
1901 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001902 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001903 else if (txn->meth == HTTP_METH_OTHER &&
1904 msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) {
1905 /* PRI is reserved for the HTTP/2 preface */
1906 msg->err_pos = 0;
1907 goto return_bad_req;
1908 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001909
Willy Tarreau59234e92008-11-30 23:51:27 +01001910 /*
1911 * 2: check if the URI matches the monitor_uri.
1912 * We have to do this for every request which gets in, because
1913 * the monitor-uri is defined by the frontend.
1914 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001915 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1916 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001917 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001918 sess->fe->monitor_uri,
1919 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001920 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001921 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001923 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001924
Willy Tarreaue7dff022015-04-03 01:14:29 +02001925 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001926 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001927
Willy Tarreau59234e92008-11-30 23:51:27 +01001928 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001929 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001930 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001931
Willy Tarreau59234e92008-11-30 23:51:27 +01001932 ret = acl_pass(ret);
1933 if (cond->pol == ACL_COND_UNLESS)
1934 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001935
Willy Tarreau59234e92008-11-30 23:51:27 +01001936 if (ret) {
1937 /* we fail this request, let's return 503 service unavail */
1938 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001939 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001940 if (!(s->flags & SF_ERR_MASK))
1941 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001942 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001943 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001945
Willy Tarreau59234e92008-11-30 23:51:27 +01001946 /* nothing to fail, let's reply normaly */
1947 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001948 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001949 if (!(s->flags & SF_ERR_MASK))
1950 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001951 goto return_prx_cond;
1952 }
1953
1954 /*
1955 * 3: Maybe we have to copy the original REQURI for the logs ?
1956 * Note: we cannot log anymore if the request has been
1957 * classified as invalid.
1958 */
1959 if (unlikely(s->logs.logwait & LW_REQ)) {
1960 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001961 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001962 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001963
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001964 if (urilen >= global.tune.requri_len )
1965 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001966 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001968
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001969 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001970 s->do_log(s);
1971 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001972 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001974 }
Willy Tarreau06619262006-12-17 08:37:22 +01001975
Willy Tarreau91852eb2015-05-01 13:26:00 +02001976 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1977 * exactly one digit "." one digit. This check may be disabled using
1978 * option accept-invalid-http-request.
1979 */
1980 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1981 if (msg->sl.rq.v_l != 8) {
1982 msg->err_pos = msg->sl.rq.v;
1983 goto return_bad_req;
1984 }
1985
1986 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1987 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1988 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1989 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1990 msg->err_pos = msg->sl.rq.v + 4;
1991 goto return_bad_req;
1992 }
1993 }
Willy Tarreau13317662015-05-01 13:47:08 +02001994 else {
1995 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1996 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1997 goto return_bad_req;
1998 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001999
Willy Tarreau5b154472009-12-21 20:11:07 +01002000 /* ... and check if the request is HTTP/1.1 or above */
2001 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002002 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2003 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2004 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002005 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002006
2007 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002008 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 +01002009
Willy Tarreau88d349d2010-01-25 12:15:43 +01002010 /* if the frontend has "option http-use-proxy-header", we'll check if
2011 * we have what looks like a proxied connection instead of a connection,
2012 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2013 * Note that this is *not* RFC-compliant, however browsers and proxies
2014 * happen to do that despite being non-standard :-(
2015 * We consider that a request not beginning with either '/' or '*' is
2016 * a proxied connection, which covers both "scheme://location" and
2017 * CONNECT ip:port.
2018 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002019 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002020 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002021 txn->flags |= TX_USE_PX_CONN;
2022
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002023 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002024 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002025
Willy Tarreau59234e92008-11-30 23:51:27 +01002026 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002027 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002028 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002029 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002030
Willy Tarreau557f1992015-05-01 10:05:17 +02002031 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2032 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002033 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002034 * The length of a message body is determined by one of the following
2035 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002036 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002037 * 1. Any response to a HEAD request and any response with a 1xx
2038 * (Informational), 204 (No Content), or 304 (Not Modified) status
2039 * code is always terminated by the first empty line after the
2040 * header fields, regardless of the header fields present in the
2041 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002042 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002043 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2044 * the connection will become a tunnel immediately after the empty
2045 * line that concludes the header fields. A client MUST ignore any
2046 * Content-Length or Transfer-Encoding header fields received in
2047 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002048 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002049 * 3. If a Transfer-Encoding header field is present and the chunked
2050 * transfer coding (Section 4.1) is the final encoding, the message
2051 * body length is determined by reading and decoding the chunked
2052 * data until the transfer coding indicates the data is complete.
2053 *
2054 * If a Transfer-Encoding header field is present in a response and
2055 * the chunked transfer coding is not the final encoding, the
2056 * message body length is determined by reading the connection until
2057 * it is closed by the server. If a Transfer-Encoding header field
2058 * is present in a request and the chunked transfer coding is not
2059 * the final encoding, the message body length cannot be determined
2060 * reliably; the server MUST respond with the 400 (Bad Request)
2061 * status code and then close the connection.
2062 *
2063 * If a message is received with both a Transfer-Encoding and a
2064 * Content-Length header field, the Transfer-Encoding overrides the
2065 * Content-Length. Such a message might indicate an attempt to
2066 * perform request smuggling (Section 9.5) or response splitting
2067 * (Section 9.4) and ought to be handled as an error. A sender MUST
2068 * remove the received Content-Length field prior to forwarding such
2069 * a message downstream.
2070 *
2071 * 4. If a message is received without Transfer-Encoding and with
2072 * either multiple Content-Length header fields having differing
2073 * field-values or a single Content-Length header field having an
2074 * invalid value, then the message framing is invalid and the
2075 * recipient MUST treat it as an unrecoverable error. If this is a
2076 * request message, the server MUST respond with a 400 (Bad Request)
2077 * status code and then close the connection. If this is a response
2078 * message received by a proxy, the proxy MUST close the connection
2079 * to the server, discard the received response, and send a 502 (Bad
2080 * Gateway) response to the client. If this is a response message
2081 * received by a user agent, the user agent MUST close the
2082 * connection to the server and discard the received response.
2083 *
2084 * 5. If a valid Content-Length header field is present without
2085 * Transfer-Encoding, its decimal value defines the expected message
2086 * body length in octets. If the sender closes the connection or
2087 * the recipient times out before the indicated number of octets are
2088 * received, the recipient MUST consider the message to be
2089 * incomplete and close the connection.
2090 *
2091 * 6. If this is a request message and none of the above are true, then
2092 * the message body length is zero (no message body is present).
2093 *
2094 * 7. Otherwise, this is a response message without a declared message
2095 * body length, so the message body length is determined by the
2096 * number of octets received prior to the server closing the
2097 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002098 */
2099
Willy Tarreau32b47f42009-10-18 20:55:02 +02002100 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002101 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002102 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002103 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002104 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002105 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002106 /* chunked not last, return badreq */
2107 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002108 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002109 }
2110
Willy Tarreau1c913912015-04-30 10:57:51 +02002111 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002112 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002113 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2114 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2115 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2116 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002117 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002118 signed long long cl;
2119
Willy Tarreauad14f752011-09-02 20:33:27 +02002120 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002121 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002122 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002123 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002124
Willy Tarreauad14f752011-09-02 20:33:27 +02002125 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002126 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002127 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002128 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002129
Willy Tarreauad14f752011-09-02 20:33:27 +02002130 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002131 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002132 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002133 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002134
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002135 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002136 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002137 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002138 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002139
Christopher Fauletbe821b92017-03-30 11:21:53 +02002140 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002141 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002142 }
2143
Willy Tarreau34dfc602015-05-01 10:09:49 +02002144 /* even bodyless requests have a known length */
2145 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002146
Willy Tarreau179085c2014-04-28 16:48:56 +02002147 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2148 * only change if both the request and the config reference something else.
2149 * Option httpclose by itself sets tunnel mode where headers are mangled.
2150 * However, if another mode is set, it will affect it (eg: server-close/
2151 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2152 * if FE and BE have the same settings (common). The method consists in
2153 * checking if options changed between the two calls (implying that either
2154 * one is non-null, or one of them is non-null and we are there for the first
2155 * time.
2156 */
2157 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002158 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002159 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002160
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002161 /* we may have to wait for the request's body */
2162 if ((s->be->options & PR_O_WREQ_BODY) &&
2163 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2164 req->analysers |= AN_REQ_HTTP_BODY;
2165
Willy Tarreau83ece462017-12-21 15:13:09 +01002166 /*
2167 * RFC7234#4:
2168 * A cache MUST write through requests with methods
2169 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2170 * the origin server; i.e., a cache is not allowed
2171 * to generate a reply to such a request before
2172 * having forwarded the request and having received
2173 * a corresponding response.
2174 *
2175 * RFC7231#4.2.1:
2176 * Of the request methods defined by this
2177 * specification, the GET, HEAD, OPTIONS, and TRACE
2178 * methods are defined to be safe.
2179 */
2180 if (likely(txn->meth == HTTP_METH_GET ||
2181 txn->meth == HTTP_METH_HEAD ||
2182 txn->meth == HTTP_METH_OPTIONS ||
2183 txn->meth == HTTP_METH_TRACE))
2184 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2185
Willy Tarreaud787e662009-07-07 10:14:51 +02002186 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002187 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002188 req->analyse_exp = TICK_ETERNITY;
2189 return 1;
2190
2191 return_bad_req:
2192 /* We centralize bad requests processing here */
2193 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2194 /* we detected a parsing error. We want to archive this request
2195 * in the dedicated proxy area for later troubleshooting.
2196 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002197 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002198 }
2199
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002200 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002201 txn->req.msg_state = HTTP_MSG_ERROR;
2202 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002203 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002204
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002205 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002206 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002207 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002208
2209 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002210 if (!(s->flags & SF_ERR_MASK))
2211 s->flags |= SF_ERR_PRXCOND;
2212 if (!(s->flags & SF_FINST_MASK))
2213 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002214
Christopher Faulet0184ea72017-01-05 14:06:34 +01002215 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002216 req->analyse_exp = TICK_ETERNITY;
2217 return 0;
2218}
2219
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002220
Willy Tarreau347a35d2013-11-22 17:51:09 +01002221/* This function prepares an applet to handle the stats. It can deal with the
2222 * "100-continue" expectation, check that admin rules are met for POST requests,
2223 * and program a response message if something was unexpected. It cannot fail
2224 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002225 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002227 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002228 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002229int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002230{
2231 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002232 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002233 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002234 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002235 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002236 struct uri_auth *uri_auth = s->be->uri_auth;
2237 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002239
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002240 appctx = si_appctx(si);
2241 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2242 appctx->st1 = appctx->st2 = 0;
2243 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2244 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002245 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002246 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002247
2248 uri = msg->chn->buf->p + msg->sl.rq.u;
2249 lookup = uri + uri_auth->uri_len;
2250
2251 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2252 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002253 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002254 break;
2255 }
2256 }
2257
2258 if (uri_auth->refresh) {
2259 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2260 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002261 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002262 break;
2263 }
2264 }
2265 }
2266
2267 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2268 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002269 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002270 break;
2271 }
2272 }
2273
Willy Tarreau1e62df92016-01-11 18:57:53 +01002274 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2275 if (memcmp(h, ";typed", 6) == 0) {
2276 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2277 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2278 break;
2279 }
2280 }
2281
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002282 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2283 if (memcmp(h, ";st=", 4) == 0) {
2284 int i;
2285 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002286 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002287 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2288 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002289 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002290 break;
2291 }
2292 }
2293 break;
2294 }
2295 }
2296
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 appctx->ctx.stats.scope_str = 0;
2298 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002299 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2300 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2301 int itx = 0;
2302 const char *h2;
2303 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2304 const char *err;
2305
2306 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2307 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002308 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002309 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2310 itx++;
2311 h++;
2312 }
2313
2314 if (itx > STAT_SCOPE_TXT_MAXLEN)
2315 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002316 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002318 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002319 memcpy(scope_txt, h2, itx);
2320 scope_txt[itx] = '\0';
2321 err = invalid_char(scope_txt);
2322 if (err) {
2323 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 appctx->ctx.stats.scope_str = 0;
2325 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002326 }
2327 break;
2328 }
2329 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002330
2331 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002332 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002333 int ret = 1;
2334
2335 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002336 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 +01002337 ret = acl_pass(ret);
2338 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2339 ret = !ret;
2340 }
2341
2342 if (ret) {
2343 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002345 break;
2346 }
2347 }
2348
2349 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002350 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002351 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002352 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002353 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2354 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002355 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002356 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002357 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002358 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2359 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002360 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002361 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002362 else {
2363 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002364 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002365 }
2366
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002367 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002368 return 1;
2369}
2370
Lukas Tribus67db8df2013-06-23 17:37:13 +02002371/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2372 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2373 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002374void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002375{
2376#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002377 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002378 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2379#endif
2380#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002381 if (from->ss_family == AF_INET6) {
2382 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002383 /* v4-mapped addresses need IP_TOS */
2384 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2385 else
2386 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2387 }
2388#endif
2389}
2390
Willy Tarreau87b09662015-04-03 00:22:06 +02002391int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002392 const char* name, unsigned int name_len,
2393 const char *str, struct my_regex *re,
2394 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002395{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002396 struct hdr_ctx ctx;
2397 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002398 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002399 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2400 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002401 struct chunk *output = get_trash_chunk();
2402
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002403 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002404
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002405 /* Choose the header browsing function. */
2406 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002407 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002408 http_find_hdr_func = http_find_header2;
2409 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002410 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002411 http_find_hdr_func = http_find_full_header2;
2412 break;
2413 default: /* impossible */
2414 return -1;
2415 }
2416
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002417 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2418 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002419 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002420 char *val = ctx.line + ctx.val;
2421 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002422
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002423 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2424 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002425
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002426 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002427 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002428 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002429
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002430 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002431
2432 hdr->len += delta;
2433 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002434
2435 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002436 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002437 }
2438
2439 return 0;
2440}
2441
Willy Tarreau87b09662015-04-03 00:22:06 +02002442static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002443 const char* name, unsigned int name_len,
2444 struct list *fmt, struct my_regex *re,
2445 int action)
2446{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002447 struct chunk *replace;
2448 int ret = -1;
2449
2450 replace = alloc_trash_chunk();
2451 if (!replace)
2452 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002453
2454 replace->len = build_logline(s, replace->str, replace->size, fmt);
2455 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002456 goto leave;
2457
2458 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002459
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002460 leave:
2461 free_trash_chunk(replace);
2462 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002463}
2464
Willy Tarreau87b09662015-04-03 00:22:06 +02002465/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002466 * transaction <txn>. Returns the verdict of the first rule that prevents
2467 * further processing of the request (auth, deny, ...), and defaults to
2468 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2469 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002470 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2471 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2472 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002473 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002474enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002475http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002476{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002477 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002478 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002479 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002480 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002481 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002482 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002483 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002484 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002485
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002486 /* If "the current_rule_list" match the executed rule list, we are in
2487 * resume condition. If a resume is needed it is always in the action
2488 * and never in the ACL or converters. In this case, we initialise the
2489 * current rule, and go to the action execution point.
2490 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002491 if (s->current_rule) {
2492 rule = s->current_rule;
2493 s->current_rule = NULL;
2494 if (s->current_rule_list == rules)
2495 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002496 }
2497 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002498
Willy Tarreauff011f22011-01-06 17:51:27 +01002499 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002500
Willy Tarreau96257ec2012-12-27 10:46:37 +01002501 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002502 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002503 int ret;
2504
Willy Tarreau192252e2015-04-04 01:47:55 +02002505 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002506 ret = acl_pass(ret);
2507
Willy Tarreauff011f22011-01-06 17:51:27 +01002508 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002509 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002510
2511 if (!ret) /* condition not matched */
2512 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002513 }
2514
Willy Tarreauacc98002015-09-27 23:34:39 +02002515 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002516resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002517 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002518 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002519 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002520
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002521 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002522 if (deny_status)
2523 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002524 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002525
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002526 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002527 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002528 if (deny_status)
2529 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002530 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002531
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002532 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002533 /* Auth might be performed on regular http-req rules as well as on stats */
2534 auth_realm = rule->arg.auth.realm;
2535 if (!auth_realm) {
2536 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2537 auth_realm = STATS_DEFAULT_REALM;
2538 else
2539 auth_realm = px->id;
2540 }
2541 /* send 401/407 depending on whether we use a proxy or not. We still
2542 * count one error, because normal browsing won't significantly
2543 * increase the counter but brute force attempts will.
2544 */
2545 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2546 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002547 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002548 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002549 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002550
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002551 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002552 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2553 return HTTP_RULE_RES_BADREQ;
2554 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002555
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002556 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002557 s->task->nice = rule->arg.nice;
2558 break;
2559
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002560 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002561 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002562 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002563 break;
2564
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002565 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002566#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002567 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002568 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002569#endif
2570 break;
2571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002572 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002573 s->logs.level = rule->arg.loglevel;
2574 break;
2575
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002576 case ACT_HTTP_REPLACE_HDR:
2577 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002578 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2579 rule->arg.hdr_add.name_len,
2580 &rule->arg.hdr_add.fmt,
2581 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002582 return HTTP_RULE_RES_BADREQ;
2583 break;
2584
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002585 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002586 ctx.idx = 0;
2587 /* remove all occurrences of the header */
2588 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2589 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2590 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002591 }
Willy Tarreau85603282015-01-21 20:39:27 +01002592 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002593
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002594 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002595 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002596 /* The scope of the trash buffer must be limited to this function. The
2597 * build_logline() function can execute a lot of other function which
2598 * can use the trash buffer. So for limiting the scope of this global
2599 * buffer, we build first the header value using build_logline, and
2600 * after we store the header name.
2601 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002602 struct chunk *replace;
2603
2604 replace = alloc_trash_chunk();
2605 if (!replace)
2606 return HTTP_RULE_RES_BADREQ;
2607
Thierry Fournier4b788f72016-06-01 13:35:36 +02002608 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002609 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2610 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2611 replace->str[rule->arg.hdr_add.name_len] = ':';
2612 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2613 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002614
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002615 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002616 /* remove all occurrences of the header */
2617 ctx.idx = 0;
2618 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2619 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2620 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2621 }
2622 }
2623
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002624 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2625
2626 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002627 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002628 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002629
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002630 case ACT_HTTP_DEL_ACL:
2631 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002632 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002633 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002634
2635 /* collect reference */
2636 ref = pat_ref_lookup(rule->arg.map.ref);
2637 if (!ref)
2638 continue;
2639
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002640 /* allocate key */
2641 key = alloc_trash_chunk();
2642 if (!key)
2643 return HTTP_RULE_RES_BADREQ;
2644
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002645 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002646 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2647 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002648
2649 /* perform update */
2650 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002651 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002652 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002653 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002654
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002655 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002656 break;
2657 }
2658
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002659 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002660 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002661 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002662
2663 /* collect reference */
2664 ref = pat_ref_lookup(rule->arg.map.ref);
2665 if (!ref)
2666 continue;
2667
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002668 /* allocate key */
2669 key = alloc_trash_chunk();
2670 if (!key)
2671 return HTTP_RULE_RES_BADREQ;
2672
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002673 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002674 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2675 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002676
2677 /* perform update */
2678 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002679 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002680 if (pat_ref_find_elt(ref, key->str) == NULL)
2681 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002682 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002683
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002684 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002685 break;
2686 }
2687
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002688 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002689 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002690 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002691
2692 /* collect reference */
2693 ref = pat_ref_lookup(rule->arg.map.ref);
2694 if (!ref)
2695 continue;
2696
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002697 /* allocate key */
2698 key = alloc_trash_chunk();
2699 if (!key)
2700 return HTTP_RULE_RES_BADREQ;
2701
2702 /* allocate value */
2703 value = alloc_trash_chunk();
2704 if (!value) {
2705 free_trash_chunk(key);
2706 return HTTP_RULE_RES_BADREQ;
2707 }
2708
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002709 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002710 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2711 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002712
2713 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002714 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2715 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002716
2717 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002718 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002719 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002720 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002721 else
2722 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002723 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002724
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002725 free_trash_chunk(key);
2726 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002727 break;
2728 }
William Lallemand73025dd2014-04-24 14:38:37 +02002729
Thierry FOURNIER42148732015-09-02 17:17:33 +02002730 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002731 if ((s->req.flags & CF_READ_ERROR) ||
2732 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2733 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2734 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002735 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002736
Willy Tarreauacc98002015-09-27 23:34:39 +02002737 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002738 case ACT_RET_ERR:
2739 case ACT_RET_CONT:
2740 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002741 case ACT_RET_STOP:
2742 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002743 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002744 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002745 return HTTP_RULE_RES_YIELD;
2746 }
William Lallemand73025dd2014-04-24 14:38:37 +02002747 break;
2748
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002749 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002750 /* Note: only the first valid tracking parameter of each
2751 * applies.
2752 */
2753
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002754 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002755 struct stktable *t;
2756 struct stksess *ts;
2757 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002758 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002759
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002760 t = rule->arg.trk_ctr.table.t;
2761 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 +02002762
2763 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002764 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002765
2766 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002767 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2768 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2769 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002770 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002771
Emeric Brun819fc6f2017-06-13 19:37:32 +02002772 if (ptr1)
2773 stktable_data_cast(ptr1, http_req_cnt)++;
2774
2775 if (ptr2)
2776 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2777 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2778
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002779 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002780
2781 /* If data was modified, we need to touch to re-schedule sync */
2782 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002783 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002784
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002785 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002786 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002787 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002788 }
2789 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002790 break;
2791
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002792 /* other flags exists, but normaly, they never be matched. */
2793 default:
2794 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002795 }
2796 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002797
2798 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002799 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002800}
2801
Willy Tarreau71241ab2012-12-27 11:30:54 +01002802
Willy Tarreau51d861a2015-05-22 17:30:48 +02002803/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2804 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2805 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2806 * is returned, the process can continue the evaluation of next rule list. If
2807 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2808 * is returned, it means the operation could not be processed and a server error
2809 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2810 * deny rule. If *YIELD is returned, the caller must call again the function
2811 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002812 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002813static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002814http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002815{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002816 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002817 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002818 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002819 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002820 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002821 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002822
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002823 /* If "the current_rule_list" match the executed rule list, we are in
2824 * resume condition. If a resume is needed it is always in the action
2825 * and never in the ACL or converters. In this case, we initialise the
2826 * current rule, and go to the action execution point.
2827 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002828 if (s->current_rule) {
2829 rule = s->current_rule;
2830 s->current_rule = NULL;
2831 if (s->current_rule_list == rules)
2832 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002833 }
2834 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002835
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002836 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002837
2838 /* check optional condition */
2839 if (rule->cond) {
2840 int ret;
2841
Willy Tarreau192252e2015-04-04 01:47:55 +02002842 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002843 ret = acl_pass(ret);
2844
2845 if (rule->cond->pol == ACL_COND_UNLESS)
2846 ret = !ret;
2847
2848 if (!ret) /* condition not matched */
2849 continue;
2850 }
2851
Willy Tarreauacc98002015-09-27 23:34:39 +02002852 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002853resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002854 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002855 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002856 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002857
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002858 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002859 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002860 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002861
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002862 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002863 s->task->nice = rule->arg.nice;
2864 break;
2865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002866 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002867 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002868 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002869 break;
2870
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002871 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002872#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002873 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002874 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002875#endif
2876 break;
2877
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002878 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002879 s->logs.level = rule->arg.loglevel;
2880 break;
2881
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002882 case ACT_HTTP_REPLACE_HDR:
2883 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002884 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2885 rule->arg.hdr_add.name_len,
2886 &rule->arg.hdr_add.fmt,
2887 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002888 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002889 break;
2890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002891 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002892 ctx.idx = 0;
2893 /* remove all occurrences of the header */
2894 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2895 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2896 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2897 }
Willy Tarreau85603282015-01-21 20:39:27 +01002898 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002899
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002900 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002901 case ACT_HTTP_ADD_HDR: {
2902 struct chunk *replace;
2903
2904 replace = alloc_trash_chunk();
2905 if (!replace)
2906 return HTTP_RULE_RES_BADREQ;
2907
2908 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2909 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2910 replace->len = rule->arg.hdr_add.name_len;
2911 replace->str[replace->len++] = ':';
2912 replace->str[replace->len++] = ' ';
2913 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2914 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002915
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002916 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002917 /* remove all occurrences of the header */
2918 ctx.idx = 0;
2919 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2920 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2921 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2922 }
2923 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002924 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2925
2926 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002927 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002928 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002929
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002930 case ACT_HTTP_DEL_ACL:
2931 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002932 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002933 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002934
2935 /* collect reference */
2936 ref = pat_ref_lookup(rule->arg.map.ref);
2937 if (!ref)
2938 continue;
2939
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002940 /* allocate key */
2941 key = alloc_trash_chunk();
2942 if (!key)
2943 return HTTP_RULE_RES_BADREQ;
2944
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002945 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002946 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2947 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002948
2949 /* perform update */
2950 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002951 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002952 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002953 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002954
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002955 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002956 break;
2957 }
2958
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002959 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002960 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002961 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002962
2963 /* collect reference */
2964 ref = pat_ref_lookup(rule->arg.map.ref);
2965 if (!ref)
2966 continue;
2967
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002968 /* allocate key */
2969 key = alloc_trash_chunk();
2970 if (!key)
2971 return HTTP_RULE_RES_BADREQ;
2972
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002973 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002974 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2975 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002976
2977 /* perform update */
2978 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002979 if (pat_ref_find_elt(ref, key->str) == NULL)
2980 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002981
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002982 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002983 break;
2984 }
2985
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002986 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002987 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002988 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002989
2990 /* collect reference */
2991 ref = pat_ref_lookup(rule->arg.map.ref);
2992 if (!ref)
2993 continue;
2994
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002995 /* allocate key */
2996 key = alloc_trash_chunk();
2997 if (!key)
2998 return HTTP_RULE_RES_BADREQ;
2999
3000 /* allocate value */
3001 value = alloc_trash_chunk();
3002 if (!value) {
3003 free_trash_chunk(key);
3004 return HTTP_RULE_RES_BADREQ;
3005 }
3006
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003007 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003008 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3009 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003010
3011 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003012 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
3013 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003014
3015 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003016 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003017 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003018 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003019 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003020 else
3021 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003022 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003023 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003024 free_trash_chunk(key);
3025 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003026 break;
3027 }
William Lallemand73025dd2014-04-24 14:38:37 +02003028
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003029 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003030 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3031 return HTTP_RULE_RES_BADREQ;
3032 return HTTP_RULE_RES_DONE;
3033
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003034 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3035 /* Note: only the first valid tracking parameter of each
3036 * applies.
3037 */
3038
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003039 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003040 struct stktable *t;
3041 struct stksess *ts;
3042 struct stktable_key *key;
3043 void *ptr;
3044
3045 t = rule->arg.trk_ctr.table.t;
3046 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3047
3048 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003049 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003050
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003051 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003052
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003053 /* let's count a new HTTP request as it's the first time we do it */
3054 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3055 if (ptr)
3056 stktable_data_cast(ptr, http_req_cnt)++;
3057
3058 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3059 if (ptr)
3060 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3061 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3062
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003063 /* When the client triggers a 4xx from the server, it's most often due
3064 * to a missing object or permission. These events should be tracked
3065 * because if they happen often, it may indicate a brute force or a
3066 * vulnerability scan. Normally this is done when receiving the response
3067 * but here we're tracking after this ought to have been done so we have
3068 * to do it on purpose.
3069 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003070 if ((unsigned)(txn->status - 400) < 100) {
3071 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3072 if (ptr)
3073 stktable_data_cast(ptr, http_err_cnt)++;
3074
3075 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3076 if (ptr)
3077 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3078 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3079 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003080
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003081 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003082
Emeric Brun0fed0b02017-11-29 16:15:07 +01003083 /* If data was modified, we need to touch to re-schedule sync */
3084 stktable_touch_local(t, ts, 0);
3085
Emeric Brun819fc6f2017-06-13 19:37:32 +02003086 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3087 if (sess->fe != s->be)
3088 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3089
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003090 }
3091 }
3092 break;
3093
Thierry FOURNIER42148732015-09-02 17:17:33 +02003094 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003095 if ((s->req.flags & CF_READ_ERROR) ||
3096 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3097 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3098 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003099 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003100
Willy Tarreauacc98002015-09-27 23:34:39 +02003101 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003102 case ACT_RET_ERR:
3103 case ACT_RET_CONT:
3104 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003105 case ACT_RET_STOP:
3106 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003107 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003108 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003109 return HTTP_RULE_RES_YIELD;
3110 }
William Lallemand73025dd2014-04-24 14:38:37 +02003111 break;
3112
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003113 /* other flags exists, but normaly, they never be matched. */
3114 default:
3115 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003116 }
3117 }
3118
3119 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003120 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003121}
3122
3123
Willy Tarreau71241ab2012-12-27 11:30:54 +01003124/* Perform an HTTP redirect based on the information in <rule>. The function
3125 * returns non-zero on success, or zero in case of a, irrecoverable error such
3126 * as too large a request to build a valid response.
3127 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003128static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003129{
Willy Tarreaub329a312015-05-22 16:27:37 +02003130 struct http_msg *req = &txn->req;
3131 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003132 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003133 struct chunk *chunk;
3134 int ret = 0;
3135
3136 chunk = alloc_trash_chunk();
3137 if (!chunk)
3138 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003139
3140 /* build redirect message */
3141 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003142 case 308:
3143 msg_fmt = HTTP_308;
3144 break;
3145 case 307:
3146 msg_fmt = HTTP_307;
3147 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003148 case 303:
3149 msg_fmt = HTTP_303;
3150 break;
3151 case 301:
3152 msg_fmt = HTTP_301;
3153 break;
3154 case 302:
3155 default:
3156 msg_fmt = HTTP_302;
3157 break;
3158 }
3159
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003160 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3161 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003162
3163 switch(rule->type) {
3164 case REDIRECT_TYPE_SCHEME: {
3165 const char *path;
3166 const char *host;
3167 struct hdr_ctx ctx;
3168 int pathlen;
3169 int hostlen;
3170
3171 host = "";
3172 hostlen = 0;
3173 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003174 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003175 host = ctx.line + ctx.val;
3176 hostlen = ctx.vlen;
3177 }
3178
3179 path = http_get_path(txn);
3180 /* build message using path */
3181 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003182 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003183 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3184 int qs = 0;
3185 while (qs < pathlen) {
3186 if (path[qs] == '?') {
3187 pathlen = qs;
3188 break;
3189 }
3190 qs++;
3191 }
3192 }
3193 } else {
3194 path = "/";
3195 pathlen = 1;
3196 }
3197
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003198 if (rule->rdr_str) { /* this is an old "redirect" rule */
3199 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003200 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3201 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003202
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003203 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003204 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3205 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003206 }
3207 else {
3208 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003209 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003210
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003211 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003212 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3213 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003214 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003215 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003216 memcpy(chunk->str + chunk->len, "://", 3);
3217 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003218
3219 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003220 memcpy(chunk->str + chunk->len, host, hostlen);
3221 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003222
3223 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003224 memcpy(chunk->str + chunk->len, path, pathlen);
3225 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003226
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003227 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003228 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003229 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003230 if (chunk->len > chunk->size - 5)
3231 goto leave;
3232 chunk->str[chunk->len] = '/';
3233 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003234 }
3235
3236 break;
3237 }
3238 case REDIRECT_TYPE_PREFIX: {
3239 const char *path;
3240 int pathlen;
3241
3242 path = http_get_path(txn);
3243 /* build message using path */
3244 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003245 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003246 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3247 int qs = 0;
3248 while (qs < pathlen) {
3249 if (path[qs] == '?') {
3250 pathlen = qs;
3251 break;
3252 }
3253 qs++;
3254 }
3255 }
3256 } else {
3257 path = "/";
3258 pathlen = 1;
3259 }
3260
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003261 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003262 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3263 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003264
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003265 /* add prefix. Note that if prefix == "/", we don't want to
3266 * add anything, otherwise it makes it hard for the user to
3267 * configure a self-redirection.
3268 */
3269 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003270 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3271 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003272 }
3273 }
3274 else {
3275 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003276 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003277
3278 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003279 if (chunk->len + pathlen > chunk->size - 4)
3280 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003281 }
3282
3283 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003284 memcpy(chunk->str + chunk->len, path, pathlen);
3285 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003286
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003287 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003288 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003289 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003290 if (chunk->len > chunk->size - 5)
3291 goto leave;
3292 chunk->str[chunk->len] = '/';
3293 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003294 }
3295
3296 break;
3297 }
3298 case REDIRECT_TYPE_LOCATION:
3299 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003300 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003301 if (chunk->len + rule->rdr_len > chunk->size - 4)
3302 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003303
3304 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003305 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3306 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003307 }
3308 else {
3309 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003310 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003311
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003312 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003313 if (chunk->len > chunk->size - 4)
3314 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003315 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003316 break;
3317 }
3318
3319 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003320 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3321 chunk->len += 14;
3322 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3323 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003324 }
3325
Willy Tarreau19b14122017-02-28 09:48:11 +01003326 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003327 txn->status = rule->code;
3328 /* let's log the request time */
3329 s->logs.tv_request = now;
3330
Christopher Fauletbe821b92017-03-30 11:21:53 +02003331 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003332 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3333 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3334 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003335 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003336 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003337 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3338 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003339 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003340 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3341 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003342 }
3343 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003344 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3345 chunk->len += 4;
3346 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003347 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003348 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003349 bi_fast_delete(req->chn->buf, req->sov);
3350 req->next -= req->sov;
3351 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003352 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003353 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003354 req->msg_state = HTTP_MSG_CLOSED;
3355 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003356 /* Trim any possible response */
3357 res->chn->buf->i = 0;
3358 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003359 /* let the server side turn to SI_ST_CLO */
3360 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003361 } else {
3362 /* keep-alive not possible */
3363 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003364 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3365 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003366 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003367 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3368 chunk->len += 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
Willy Tarreau87b09662015-04-03 00:22:06 +02003408 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003409 now_ms, __FUNCTION__,
3410 s,
3411 req,
3412 req->rex, req->wex,
3413 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003414 req->buf->i,
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 Houchardc2aae742017-09-22 18:26:28 +02003448 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3449 struct hdr_ctx ctx;
3450
3451 ctx.idx = 0;
3452 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3453 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3454 if (unlikely(http_header_add_tail2(&txn->req,
3455 &txn->hdr_idx, "Early-Data: 1",
3456 strlen("Early-Data: 1"))) < 0) {
3457 goto return_bad_req;
3458 }
3459 }
3460
3461 }
3462
Willy Tarreau52542592014-04-28 18:33:26 +02003463 /* OK at this stage, we know that the request was accepted according to
3464 * the http-request rules, we can check for the stats. Note that the
3465 * URI is detected *before* the req* rules in order not to be affected
3466 * by a possible reqrep, while they are processed *after* so that a
3467 * reqdeny can still block them. This clearly needs to change in 1.6!
3468 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003469 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003470 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003471 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003472 txn->status = 500;
3473 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003474 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003475
Willy Tarreaue7dff022015-04-03 01:14:29 +02003476 if (!(s->flags & SF_ERR_MASK))
3477 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003478 goto return_prx_cond;
3479 }
3480
3481 /* parse the whole stats request and extract the relevant information */
3482 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003483 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003484 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003485
Willy Tarreau0b748332014-04-29 00:13:29 +02003486 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3487 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003488
Willy Tarreau0b748332014-04-29 00:13:29 +02003489 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3490 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003491 }
3492
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003493 /* evaluate the req* rules except reqadd */
3494 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003495 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003496 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003497
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003498 if (txn->flags & TX_CLDENY)
3499 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003500
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003501 if (txn->flags & TX_CLTARPIT) {
3502 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003503 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003504 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003505 }
Willy Tarreau06619262006-12-17 08:37:22 +01003506
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003507 /* add request headers from the rule sets in the same order */
3508 list_for_each_entry(wl, &px->req_add, list) {
3509 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003510 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003511 ret = acl_pass(ret);
3512 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3513 ret = !ret;
3514 if (!ret)
3515 continue;
3516 }
3517
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003518 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003519 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003520 }
3521
Willy Tarreau52542592014-04-28 18:33:26 +02003522
3523 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003524 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3525 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003526 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003527 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003528 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003529
Willy Tarreaue7dff022015-04-03 01:14:29 +02003530 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3531 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3532 if (!(s->flags & SF_FINST_MASK))
3533 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003534
Willy Tarreau70730dd2014-04-24 18:06:27 +02003535 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003536 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3537 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003538 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003539 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003540 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003541
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003542 /* check whether we have some ACLs set to redirect this request */
3543 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003544 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003545 int ret;
3546
Willy Tarreau192252e2015-04-04 01:47:55 +02003547 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003548 ret = acl_pass(ret);
3549 if (rule->cond->pol == ACL_COND_UNLESS)
3550 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003551 if (!ret)
3552 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003553 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003554 if (!http_apply_redirect_rule(rule, s, txn))
3555 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003556 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003557 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003558
Willy Tarreau2be39392010-01-03 17:24:51 +01003559 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3560 * If this happens, then the data will not come immediately, so we must
3561 * send all what we have without waiting. Note that due to the small gain
3562 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003563 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003564 * itself once used.
3565 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003566 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003567
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003568 done: /* done with this analyser, continue with next ones that the calling
3569 * points will have set, if any.
3570 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003571 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003572 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3573 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003574 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003575
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003576 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003577 /* Allow cookie logging
3578 */
3579 if (s->be->cookie_name || sess->fe->capture_name)
3580 manage_client_side_cookies(s, req);
3581
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003582 /* When a connection is tarpitted, we use the tarpit timeout,
3583 * which may be the same as the connect timeout if unspecified.
3584 * If unset, then set it to zero because we really want it to
3585 * eventually expire. We build the tarpit as an analyser.
3586 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003587 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003588
3589 /* wipe the request out so that we can drop the connection early
3590 * if the client closes first.
3591 */
3592 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003593
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003594 txn->status = http_err_codes[deny_status];
3595
Christopher Faulet0184ea72017-01-05 14:06:34 +01003596 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003597 req->analysers |= AN_REQ_HTTP_TARPIT;
3598 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3599 if (!req->analyse_exp)
3600 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003601 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003602 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003603 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003604 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003605 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003606 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003607 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003608
3609 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003610
3611 /* Allow cookie logging
3612 */
3613 if (s->be->cookie_name || sess->fe->capture_name)
3614 manage_client_side_cookies(s, req);
3615
Willy Tarreau0b748332014-04-29 00:13:29 +02003616 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003617 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003618 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003619 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003620 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003621 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003622 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003623 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003624 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003625 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003626 goto return_prx_cond;
3627
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003628 return_bad_req:
3629 /* We centralize bad requests processing here */
3630 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3631 /* we detected a parsing error. We want to archive this request
3632 * in the dedicated proxy area for later troubleshooting.
3633 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003634 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003635 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003636
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003637 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003638 txn->req.msg_state = HTTP_MSG_ERROR;
3639 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003640 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003641
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003642 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003643 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003644 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003645
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003646 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003647 if (!(s->flags & SF_ERR_MASK))
3648 s->flags |= SF_ERR_PRXCOND;
3649 if (!(s->flags & SF_FINST_MASK))
3650 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003651
Christopher Faulet0184ea72017-01-05 14:06:34 +01003652 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003653 req->analyse_exp = TICK_ETERNITY;
3654 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003655
3656 return_prx_yield:
3657 channel_dont_connect(req);
3658 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003659}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003660
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003661/* This function performs all the processing enabled for the current request.
3662 * It returns 1 if the processing can continue on next analysers, or zero if it
3663 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003664 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003665 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003666int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003667{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003668 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003669 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003670 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003671 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003672
Willy Tarreau655dce92009-11-08 13:10:58 +01003673 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003674 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003675 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003676 return 0;
3677 }
3678
Willy Tarreau87b09662015-04-03 00:22:06 +02003679 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003680 now_ms, __FUNCTION__,
3681 s,
3682 req,
3683 req->rex, req->wex,
3684 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003685 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003686 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003687
Willy Tarreau59234e92008-11-30 23:51:27 +01003688 /*
3689 * Right now, we know that we have processed the entire headers
3690 * and that unwanted requests have been filtered out. We can do
3691 * whatever we want with the remaining request. Also, now we
3692 * may have separate values for ->fe, ->be.
3693 */
Willy Tarreau06619262006-12-17 08:37:22 +01003694
Willy Tarreau59234e92008-11-30 23:51:27 +01003695 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003696 * If HTTP PROXY is set we simply get remote server address parsing
3697 * incoming request. Note that this requires that a connection is
3698 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003699 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003700 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003701 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003702 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003703
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003704 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003705 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003706 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003707 txn->req.msg_state = HTTP_MSG_ERROR;
3708 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003709 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003710 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003711
Willy Tarreaue7dff022015-04-03 01:14:29 +02003712 if (!(s->flags & SF_ERR_MASK))
3713 s->flags |= SF_ERR_RESOURCE;
3714 if (!(s->flags & SF_FINST_MASK))
3715 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003716
3717 return 0;
3718 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003719
3720 path = http_get_path(txn);
3721 url2sa(req->buf->p + msg->sl.rq.u,
3722 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003723 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003724 /* if the path was found, we have to remove everything between
3725 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3726 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3727 * u_l characters by a single "/".
3728 */
3729 if (path) {
3730 char *cur_ptr = req->buf->p;
3731 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3732 int delta;
3733
3734 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3735 http_msg_move_end(&txn->req, delta);
3736 cur_end += delta;
3737 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3738 goto return_bad_req;
3739 }
3740 else {
3741 char *cur_ptr = req->buf->p;
3742 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3743 int delta;
3744
3745 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3746 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3747 http_msg_move_end(&txn->req, delta);
3748 cur_end += delta;
3749 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3750 goto return_bad_req;
3751 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003752 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003753
Willy Tarreau59234e92008-11-30 23:51:27 +01003754 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003755 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003756 * Note that doing so might move headers in the request, but
3757 * the fields will stay coherent and the URI will not move.
3758 * This should only be performed in the backend.
3759 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003760 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003761 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003762
William Lallemanda73203e2012-03-12 12:48:57 +01003763 /* add unique-id if "header-unique-id" is specified */
3764
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003765 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003766 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003767 goto return_bad_req;
3768 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003769 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003770 }
William Lallemanda73203e2012-03-12 12:48:57 +01003771
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003772 if (sess->fe->header_unique_id && s->unique_id) {
3773 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003774 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003775 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003776 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003777 goto return_bad_req;
3778 }
3779
Cyril Bontéb21570a2009-11-29 20:04:48 +01003780 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003781 * 9: add X-Forwarded-For if either the frontend or the backend
3782 * asks for it.
3783 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003784 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003785 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003786 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3787 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3788 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003789 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003790 /* The header is set to be added only if none is present
3791 * and we found it, so don't do anything.
3792 */
3793 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003794 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003795 /* Add an X-Forwarded-For header unless the source IP is
3796 * in the 'except' network range.
3797 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003798 if ((!sess->fe->except_mask.s_addr ||
3799 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3800 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003801 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003802 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003803 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003804 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003806 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003807
3808 /* Note: we rely on the backend to get the header name to be used for
3809 * x-forwarded-for, because the header is really meant for the backends.
3810 * However, if the backend did not specify any option, we have to rely
3811 * on the frontend's header name.
3812 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003813 if (s->be->fwdfor_hdr_len) {
3814 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003815 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003816 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003817 len = sess->fe->fwdfor_hdr_len;
3818 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003819 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003820 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003821
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003822 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003823 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003824 }
3825 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003826 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003827 /* FIXME: for the sake of completeness, we should also support
3828 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003829 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003830 int len;
3831 char pn[INET6_ADDRSTRLEN];
3832 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003833 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003834 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003835
Willy Tarreau59234e92008-11-30 23:51:27 +01003836 /* Note: we rely on the backend to get the header name to be used for
3837 * x-forwarded-for, because the header is really meant for the backends.
3838 * However, if the backend did not specify any option, we have to rely
3839 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003840 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003841 if (s->be->fwdfor_hdr_len) {
3842 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003843 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003844 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003845 len = sess->fe->fwdfor_hdr_len;
3846 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003847 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003848 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003849
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003850 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003851 goto return_bad_req;
3852 }
3853 }
3854
3855 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003856 * 10: add X-Original-To if either the frontend or the backend
3857 * asks for it.
3858 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003859 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003860
3861 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003862 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003863 /* Add an X-Original-To header unless the destination IP is
3864 * in the 'except' network range.
3865 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003866 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003867
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003868 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003869 ((!sess->fe->except_mask_to.s_addr ||
3870 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3871 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003872 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003873 (((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 +02003874 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003875 int len;
3876 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003877 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003878
3879 /* Note: we rely on the backend to get the header name to be used for
3880 * x-original-to, because the header is really meant for the backends.
3881 * However, if the backend did not specify any option, we have to rely
3882 * on the frontend's header name.
3883 */
3884 if (s->be->orgto_hdr_len) {
3885 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003886 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003887 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003888 len = sess->fe->orgto_hdr_len;
3889 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003890 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003891 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003892
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003893 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003894 goto return_bad_req;
3895 }
3896 }
3897 }
3898
Willy Tarreau50fc7772012-11-11 22:19:57 +01003899 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3900 * If an "Upgrade" token is found, the header is left untouched in order not to have
3901 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3902 * "Upgrade" is present in the Connection header.
3903 */
3904 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3905 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003906 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003907 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003908 unsigned int want_flags = 0;
3909
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003910 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003911 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003912 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003913 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003914 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003915 want_flags |= TX_CON_CLO_SET;
3916 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003917 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003918 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003919 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003920 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003921 want_flags |= TX_CON_KAL_SET;
3922 }
3923
3924 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003925 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003926 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003927
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003928
Willy Tarreau522d6c02009-12-06 18:49:18 +01003929 /* If we have no server assigned yet and we're balancing on url_param
3930 * with a POST request, we may be interested in checking the body for
3931 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003932 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003933 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003934 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003935 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003936 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003937 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003938 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003939
Christopher Fauletbe821b92017-03-30 11:21:53 +02003940 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3941 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003942#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003943 /* We expect some data from the client. Unless we know for sure
3944 * we already have a full request, we have to re-enable quick-ack
3945 * in case we previously disabled it, otherwise we might cause
3946 * the client to delay further data.
3947 */
3948 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3949 cli_conn && conn_ctrl_ready(cli_conn) &&
3950 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3951 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003952 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003953#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003954
Willy Tarreau59234e92008-11-30 23:51:27 +01003955 /*************************************************************
3956 * OK, that's finished for the headers. We have done what we *
3957 * could. Let's switch to the DATA state. *
3958 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003959 req->analyse_exp = TICK_ETERNITY;
3960 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003961
Willy Tarreau59234e92008-11-30 23:51:27 +01003962 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003963 /* OK let's go on with the BODY now */
3964 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003965
Willy Tarreau59234e92008-11-30 23:51:27 +01003966 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003967 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003968 /* we detected a parsing error. We want to archive this request
3969 * in the dedicated proxy area for later troubleshooting.
3970 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003971 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003972 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003973
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003974 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003975 txn->req.msg_state = HTTP_MSG_ERROR;
3976 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003977 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003978 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003979
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003980 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003981 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003982 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003983
Willy Tarreaue7dff022015-04-03 01:14:29 +02003984 if (!(s->flags & SF_ERR_MASK))
3985 s->flags |= SF_ERR_PRXCOND;
3986 if (!(s->flags & SF_FINST_MASK))
3987 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003988 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003989}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003990
Willy Tarreau60b85b02008-11-30 23:28:40 +01003991/* This function is an analyser which processes the HTTP tarpit. It always
3992 * returns zero, at the beginning because it prevents any other processing
3993 * from occurring, and at the end because it terminates the request.
3994 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003995int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003996{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003997 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003998
3999 /* This connection is being tarpitted. The CLIENT side has
4000 * already set the connect expiration date to the right
4001 * timeout. We just have to check that the client is still
4002 * there and that the timeout has not expired.
4003 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004004 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004005 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004006 !tick_is_expired(req->analyse_exp, now_ms))
4007 return 0;
4008
4009 /* We will set the queue timer to the time spent, just for
4010 * logging purposes. We fake a 500 server error, so that the
4011 * attacker will not suspect his connection has been tarpitted.
4012 * It will not cause trouble to the logs because we can exclude
4013 * the tarpitted connections by filtering on the 'PT' status flags.
4014 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004015 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4016
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004017 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004018 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004019
Christopher Faulet0184ea72017-01-05 14:06:34 +01004020 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004021 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004022
Willy Tarreaue7dff022015-04-03 01:14:29 +02004023 if (!(s->flags & SF_ERR_MASK))
4024 s->flags |= SF_ERR_PRXCOND;
4025 if (!(s->flags & SF_FINST_MASK))
4026 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004027 return 0;
4028}
4029
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004030/* This function is an analyser which waits for the HTTP request body. It waits
4031 * for either the buffer to be full, or the full advertised contents to have
4032 * reached the buffer. It must only be called after the standard HTTP request
4033 * processing has occurred, because it expects the request to be parsed and will
4034 * look for the Expect header. It may send a 100-Continue interim response. It
4035 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4036 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4037 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004038 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004039int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004040{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004041 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004042 struct http_txn *txn = s->txn;
4043 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004044
4045 /* We have to parse the HTTP request body to find any required data.
4046 * "balance url_param check_post" should have been the only way to get
4047 * into this. We were brought here after HTTP header analysis, so all
4048 * related structures are ready.
4049 */
4050
Willy Tarreau890988f2014-04-10 11:59:33 +02004051 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4052 /* This is the first call */
4053 if (msg->msg_state < HTTP_MSG_BODY)
4054 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004055
Willy Tarreau890988f2014-04-10 11:59:33 +02004056 if (msg->msg_state < HTTP_MSG_100_SENT) {
4057 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4058 * send an HTTP/1.1 100 Continue intermediate response.
4059 */
4060 if (msg->flags & HTTP_MSGF_VER_11) {
4061 struct hdr_ctx ctx;
4062 ctx.idx = 0;
4063 /* Expect is allowed in 1.1, look for it */
4064 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4065 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004066 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004067 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004068 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004069 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004070 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004072
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004073 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004074 * req->buf->p still points to the beginning of the message. We
4075 * must save the body in msg->next because it survives buffer
4076 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004077 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004078 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004079
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004080 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004081 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4082 else
4083 msg->msg_state = HTTP_MSG_DATA;
4084 }
4085
Willy Tarreau890988f2014-04-10 11:59:33 +02004086 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4087 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004088 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004089 goto missing_data;
4090
4091 /* OK we have everything we need now */
4092 goto http_end;
4093 }
4094
4095 /* OK here we're parsing a chunked-encoded message */
4096
Willy Tarreau522d6c02009-12-06 18:49:18 +01004097 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004098 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004099 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004100 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004101 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004102 unsigned int chunk;
4103 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004104
Willy Tarreau115acb92009-12-26 13:56:06 +01004105 if (!ret)
4106 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004107 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004108 msg->err_pos = req->buf->i + ret;
4109 if (msg->err_pos < 0)
4110 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004111 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004112 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004113 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004114
4115 msg->chunk_len = chunk;
4116 msg->body_len += chunk;
4117
4118 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004119 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004120 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004121 }
4122
Willy Tarreaud98cf932009-12-27 22:54:55 +01004123 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004124 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4125 * for at least a whole chunk or the whole content length bytes after
4126 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004127 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004128 if (msg->msg_state == HTTP_MSG_TRAILERS)
4129 goto http_end;
4130
Willy Tarreaue115b492015-05-01 23:05:14 +02004131 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004132 goto http_end;
4133
4134 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004135 /* we get here if we need to wait for more data. If the buffer is full,
4136 * we have the maximum we can expect.
4137 */
4138 if (buffer_full(req->buf, global.tune.maxrewrite))
4139 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004140
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004141 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004142 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004143 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004144
Willy Tarreaue7dff022015-04-03 01:14:29 +02004145 if (!(s->flags & SF_ERR_MASK))
4146 s->flags |= SF_ERR_CLITO;
4147 if (!(s->flags & SF_FINST_MASK))
4148 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004149 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004150 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004151
4152 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004153 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004154 /* Not enough data. We'll re-use the http-request
4155 * timeout here. Ideally, we should set the timeout
4156 * relative to the accept() date. We just set the
4157 * request timeout once at the beginning of the
4158 * request.
4159 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004160 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004161 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004162 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004163 return 0;
4164 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004165
4166 http_end:
4167 /* The situation will not evolve, so let's give up on the analysis. */
4168 s->logs.tv_request = now; /* update the request timer to reflect full request */
4169 req->analysers &= ~an_bit;
4170 req->analyse_exp = TICK_ETERNITY;
4171 return 1;
4172
4173 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004174 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004175 txn->req.msg_state = HTTP_MSG_ERROR;
4176 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004177 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004178
Willy Tarreaue7dff022015-04-03 01:14:29 +02004179 if (!(s->flags & SF_ERR_MASK))
4180 s->flags |= SF_ERR_PRXCOND;
4181 if (!(s->flags & SF_FINST_MASK))
4182 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004183
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004185 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004186 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004187 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004188 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004189 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004190}
4191
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004192/* send a server's name with an outgoing request over an established connection.
4193 * Note: this function is designed to be called once the request has been scheduled
4194 * for being forwarded. This is the reason why it rewinds the buffer before
4195 * proceeding.
4196 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004197int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004198
4199 struct hdr_ctx ctx;
4200
Mark Lamourinec2247f02012-01-04 13:02:01 -05004201 char *hdr_name = be->server_id_hdr_name;
4202 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004203 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004204 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004205 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004206
William Lallemandd9e90662012-01-30 17:27:17 +01004207 ctx.idx = 0;
4208
Willy Tarreau211cdec2014-04-17 20:18:08 +02004209 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004210 if (old_o) {
4211 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004212 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004213 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004214 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004215 }
4216
Willy Tarreau9b28e032012-10-12 23:49:43 +02004217 old_i = chn->buf->i;
4218 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004219 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004220 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004221 }
4222
4223 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004224 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004225 memcpy(hdr_val, hdr_name, hdr_name_len);
4226 hdr_val += hdr_name_len;
4227 *hdr_val++ = ':';
4228 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004229 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4230 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004231
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004232 if (old_o) {
4233 /* If this was a forwarded request, we must readjust the amount of
4234 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004235 * variations. Note that the current state is >= HTTP_MSG_BODY,
4236 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004237 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004238 old_o += chn->buf->i - old_i;
4239 b_adv(chn->buf, old_o);
4240 txn->req.next -= old_o;
4241 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004242 }
4243
Mark Lamourinec2247f02012-01-04 13:02:01 -05004244 return 0;
4245}
4246
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247/* Terminate current transaction and prepare a new one. This is very tricky
4248 * right now but it works.
4249 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004250void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004252 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004253 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004254 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004255 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004256 struct connection *srv_conn;
4257 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004258 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004259
Willy Tarreau610ecce2010-01-04 21:15:02 +01004260 /* FIXME: We need a more portable way of releasing a backend's and a
4261 * server's connections. We need a safer way to reinitialize buffer
4262 * flags. We also need a more accurate method for computing per-request
4263 * data.
4264 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004265 /*
4266 * XXX cognet: This is probably wrong, this is killing a whole
4267 * connection, in the new world order, we probably want to just kill
4268 * the stream, this is to be revisited the day we handle multiple
4269 * streams in one server connection.
4270 */
4271 cs = objt_cs(s->si[1].end);
4272 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273
Willy Tarreau4213a112013-12-15 10:25:42 +01004274 /* unless we're doing keep-alive, we want to quickly close the connection
4275 * to the server.
4276 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004277 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004278 !si_conn_ready(&s->si[1])) {
4279 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4280 si_shutr(&s->si[1]);
4281 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004282 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004283
Willy Tarreaue7dff022015-04-03 01:14:29 +02004284 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004285 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004286 if (unlikely(s->srv_conn))
4287 sess_change_server(s, NULL);
4288 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289
4290 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004291 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292
Willy Tarreaueee5b512015-04-03 23:46:31 +02004293 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 int n;
4295
Willy Tarreaueee5b512015-04-03 23:46:31 +02004296 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 if (n < 1 || n > 5)
4298 n = 0;
4299
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004300 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004301 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004302 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004303 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004304 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004305 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4306 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004307 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 }
4309
4310 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004311 s->logs.bytes_in -= s->req.buf->i;
4312 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313
4314 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004315 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004316 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004317 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004318 s->do_log(s);
4319 }
4320
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004321 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004322 stream_stop_content_counters(s);
4323 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004324
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325 s->logs.accept_date = date; /* user-visible date for logging */
4326 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004327 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4328 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 tv_zero(&s->logs.tv_request);
4330 s->logs.t_queue = -1;
4331 s->logs.t_connect = -1;
4332 s->logs.t_data = -1;
4333 s->logs.t_close = 0;
4334 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4335 s->logs.srv_queue_size = 0; /* we will get this number soon */
4336
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004337 s->logs.bytes_in = s->req.total = s->req.buf->i;
4338 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339
4340 if (s->pend_pos)
4341 pendconn_free(s->pend_pos);
4342
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004343 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004344 if (s->flags & SF_CURR_SESS) {
4345 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004346 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004348 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004349 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004350 }
4351
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004352 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004353
Willy Tarreau4213a112013-12-15 10:25:42 +01004354 /* only release our endpoint if we don't intend to reuse the
4355 * connection.
4356 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004357 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004358 !si_conn_ready(&s->si[1])) {
4359 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004360 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004361 }
4362
Willy Tarreau350f4872014-11-28 14:42:25 +01004363 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4364 s->si[1].err_type = SI_ET_NONE;
4365 s->si[1].conn_retries = 0; /* used for logging too */
4366 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004367 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 +01004368 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 +01004369 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 +02004370 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4371 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4372 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004373
Willy Tarreaueee5b512015-04-03 23:46:31 +02004374 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004375 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004376 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004377
4378 if (prev_status == 401 || prev_status == 407) {
4379 /* In HTTP keep-alive mode, if we receive a 401, we still have
4380 * a chance of being able to send the visitor again to the same
4381 * server over the same connection. This is required by some
4382 * broken protocols such as NTLM, and anyway whenever there is
4383 * an opportunity for sending the challenge to the proper place,
4384 * it's better to do it (at least it helps with debugging).
4385 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004386 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004387 if (srv_conn)
4388 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004389 }
4390
Willy Tarreau53f96852016-02-02 18:50:47 +01004391 /* Never ever allow to reuse a connection from a non-reuse backend */
4392 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4393 srv_conn->flags |= CO_FL_PRIVATE;
4394
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004395 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004396 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004398 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004399 s->req.flags |= CF_NEVER_WAIT;
4400 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004401 }
4402
Willy Tarreau714ea782015-11-25 20:11:11 +01004403 /* we're removing the analysers, we MUST re-enable events detection.
4404 * We don't enable close on the response channel since it's either
4405 * already closed, or in keep-alive with an idle connection handler.
4406 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004407 channel_auto_read(&s->req);
4408 channel_auto_close(&s->req);
4409 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004411 /* we're in keep-alive with an idle connection, monitor it if not already done */
4412 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004413 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004414 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004415 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004416 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004417 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004418 else if (prev_flags & TX_NOT_FIRST)
4419 /* note: we check the request, not the connection, but
4420 * this is valid for strategies SAFE and AGGR, and in
4421 * case of ALWS, we don't care anyway.
4422 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004423 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004424 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004425 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004426 }
Christopher Faulete6006242017-03-10 11:52:44 +01004427 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4428 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429}
4430
4431
4432/* This function updates the request state machine according to the response
4433 * state machine and buffer flags. It returns 1 if it changes anything (flag
4434 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4435 * it is only used to find when a request/response couple is complete. Both
4436 * this function and its equivalent should loop until both return zero. It
4437 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4438 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004439int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004440{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004441 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004442 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004443 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 unsigned int old_state = txn->req.msg_state;
4445
Christopher Faulet4be98032017-07-18 10:48:24 +02004446 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004447 return 0;
4448
4449 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004450 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004451 * We can shut the read side unless we want to abort_on_close,
4452 * or we have a POST request. The issue with POST requests is
4453 * that some browsers still send a CRLF after the request, and
4454 * this CRLF must be read so that it does not remain in the kernel
4455 * buffers, otherwise a close could cause an RST on some systems
4456 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004457 * Note that if we're using keep-alive on the client side, we'd
4458 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004459 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004460 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004461 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004462 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4463 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004464 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4465 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004466 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004467 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468
Willy Tarreau40f151a2012-12-20 12:10:09 +01004469 /* if the server closes the connection, we want to immediately react
4470 * and close the socket to save packets and syscalls.
4471 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004472 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004473
Willy Tarreau7f876a12015-11-18 11:59:55 +01004474 /* In any case we've finished parsing the request so we must
4475 * disable Nagle when sending data because 1) we're not going
4476 * to shut this side, and 2) the server is waiting for us to
4477 * send pending data.
4478 */
4479 chn->flags |= CF_NEVER_WAIT;
4480
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4482 goto wait_other_side;
4483
4484 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4485 /* The server has not finished to respond, so we
4486 * don't want to move in order not to upset it.
4487 */
4488 goto wait_other_side;
4489 }
4490
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 /* When we get here, it means that both the request and the
4492 * response have finished receiving. Depending on the connection
4493 * mode, we'll have to wait for the last bytes to leave in either
4494 * direction, and sometimes for a close to be effective.
4495 */
4496
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004497 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4498 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004499 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4500 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004501 }
4502 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4503 /* Option forceclose is set, or either side wants to close,
4504 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004505 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004506 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004507 *
4508 * However, there is an exception if the response
4509 * length is undefined. In this case, we need to wait
4510 * the close from the server. The response will be
4511 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004512 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004513 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4514 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4515 goto check_channel_flags;
4516
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004517 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4518 channel_shutr_now(chn);
4519 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004521 }
4522 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004523 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4524 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004525 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004526 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4527 channel_auto_read(chn);
4528 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004529 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004530 }
4531
Christopher Faulet4be98032017-07-18 10:48:24 +02004532 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 }
4534
4535 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4536 http_msg_closing:
4537 /* nothing else to forward, just waiting for the output buffer
4538 * to be empty and for the shutw_now to take effect.
4539 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004540 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004541 txn->req.msg_state = HTTP_MSG_CLOSED;
4542 goto http_msg_closed;
4543 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004544 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004545 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004546 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004548 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004549 }
4550
4551 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4552 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004553 /* if we don't know whether the server will close, we need to hard close */
4554 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4555 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4556
Willy Tarreau3988d932013-12-27 23:03:08 +01004557 /* see above in MSG_DONE why we only do this in these states */
4558 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4559 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004560 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4561 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004562 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 goto wait_other_side;
4564 }
4565
Christopher Faulet4be98032017-07-18 10:48:24 +02004566 check_channel_flags:
4567 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4568 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4569 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004570 txn->req.msg_state = HTTP_MSG_CLOSING;
4571 goto http_msg_closing;
4572 }
4573
4574
Willy Tarreau610ecce2010-01-04 21:15:02 +01004575 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004576 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004577}
4578
4579
4580/* This function updates the response state machine according to the request
4581 * state machine and buffer flags. It returns 1 if it changes anything (flag
4582 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4583 * it is only used to find when a request/response couple is complete. Both
4584 * this function and its equivalent should loop until both return zero. It
4585 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4586 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004587int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004589 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004590 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004591 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 unsigned int old_state = txn->rsp.msg_state;
4593
Christopher Faulet4be98032017-07-18 10:48:24 +02004594 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 return 0;
4596
4597 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4598 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004599 * still monitor the server connection for a possible close
4600 * while the request is being uploaded, so we don't disable
4601 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004603 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004604
4605 if (txn->req.msg_state == HTTP_MSG_ERROR)
4606 goto wait_other_side;
4607
4608 if (txn->req.msg_state < HTTP_MSG_DONE) {
4609 /* The client seems to still be sending data, probably
4610 * because we got an error response during an upload.
4611 * We have the choice of either breaking the connection
4612 * or letting it pass through. Let's do the later.
4613 */
4614 goto wait_other_side;
4615 }
4616
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617 /* When we get here, it means that both the request and the
4618 * response have finished receiving. Depending on the connection
4619 * mode, we'll have to wait for the last bytes to leave in either
4620 * direction, and sometimes for a close to be effective.
4621 */
4622
4623 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4624 /* Server-close mode : shut read and wait for the request
4625 * side to close its output buffer. The caller will detect
4626 * when we're in DONE and the other is in CLOSED and will
4627 * catch that for the final cleanup.
4628 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004629 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4630 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004631 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004632 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4633 /* Option forceclose is set, or either side wants to close,
4634 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004635 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004636 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004637 *
4638 * However, there is an exception if the response length
4639 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004640 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004641 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4642 channel_auto_read(chn);
4643 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4644 chn->flags |= CF_NEVER_WAIT;
4645 }
4646 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004647 channel_shutr_now(chn);
4648 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004649 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004650 }
4651 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004652 /* The last possible modes are keep-alive and tunnel. Tunnel will
4653 * need to forward remaining data. Keep-alive will need to monitor
4654 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004655 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004656 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004657 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004658 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4659 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 }
4661
Christopher Faulet4be98032017-07-18 10:48:24 +02004662 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004663 }
4664
4665 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4666 http_msg_closing:
4667 /* nothing else to forward, just waiting for the output buffer
4668 * to be empty and for the shutw_now to take effect.
4669 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004670 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4672 goto http_msg_closed;
4673 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004674 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004675 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004677 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004678 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004679 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004680 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004681 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004682 }
4683
4684 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4685 http_msg_closed:
4686 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004687 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004688 channel_auto_close(chn);
4689 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004690 goto wait_other_side;
4691 }
4692
Christopher Faulet4be98032017-07-18 10:48:24 +02004693 check_channel_flags:
4694 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4695 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4696 /* if we've just closed an output, let's switch */
4697 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4698 goto http_msg_closing;
4699 }
4700
Willy Tarreau610ecce2010-01-04 21:15:02 +01004701 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004702 /* We force the response to leave immediately if we're waiting for the
4703 * other side, since there is no pending shutdown to push it out.
4704 */
4705 if (!channel_is_empty(chn))
4706 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004707 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708}
4709
4710
Christopher Faulet894da4c2017-07-18 11:29:07 +02004711/* Resync the request and response state machines. */
4712void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004713{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004714 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004715#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 int old_req_state = txn->req.msg_state;
4717 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004718#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004719
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720 http_sync_req_state(s);
4721 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004722 if (!http_sync_res_state(s))
4723 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004724 if (!http_sync_req_state(s))
4725 break;
4726 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004727
Christopher Faulet894da4c2017-07-18 11:29:07 +02004728 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4729 "req->analysers=0x%08x res->analysers=0x%08x\n",
4730 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004731 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4732 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004733 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004734
4735
Willy Tarreau610ecce2010-01-04 21:15:02 +01004736 /* OK, both state machines agree on a compatible state.
4737 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4739 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004740 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4741 * means we must abort the request.
4742 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4743 * corresponding channel.
4744 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4745 * on the response with server-close mode means we've completed one
4746 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004747 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004748 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4749 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004750 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004751 channel_auto_close(&s->req);
4752 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004753 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004754 channel_auto_close(&s->res);
4755 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004756 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004757 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4758 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004759 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004760 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004761 channel_auto_close(&s->res);
4762 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004763 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004764 channel_abort(&s->req);
4765 channel_auto_close(&s->req);
4766 channel_auto_read(&s->req);
4767 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004768 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004769 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4770 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4771 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4772 s->req.analysers &= AN_REQ_FLT_END;
4773 if (HAS_REQ_DATA_FILTERS(s))
4774 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4775 }
4776 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4777 s->res.analysers &= AN_RES_FLT_END;
4778 if (HAS_RSP_DATA_FILTERS(s))
4779 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4780 }
4781 channel_auto_close(&s->req);
4782 channel_auto_read(&s->req);
4783 channel_auto_close(&s->res);
4784 channel_auto_read(&s->res);
4785 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004786 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4787 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004788 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004789 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4790 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4791 /* server-close/keep-alive: terminate this transaction,
4792 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004793 * a fresh-new transaction, but only once we're sure there's
4794 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004795 * another request. They must not hold any pending output data
4796 * and the response buffer must realigned
4797 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004799 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004800 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004801 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004802 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004803 else {
4804 s->req.analysers = AN_REQ_FLT_END;
4805 s->res.analysers = AN_RES_FLT_END;
4806 txn->flags |= TX_WAIT_CLEANUP;
4807 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004808 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809}
4810
Willy Tarreaud98cf932009-12-27 22:54:55 +01004811/* This function is an analyser which forwards request body (including chunk
4812 * sizes if any). It is called as soon as we must forward, even if we forward
4813 * zero byte. The only situation where it must not be called is when we're in
4814 * tunnel mode and we want to forward till the close. It's used both to forward
4815 * remaining data and to resync after end of body. It expects the msg_state to
4816 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004817 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004818 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004819 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004820 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004821int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004822{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004823 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004824 struct http_txn *txn = s->txn;
4825 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004826 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004827
Christopher Faulet814d2702017-03-30 11:33:44 +02004828 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4829 now_ms, __FUNCTION__,
4830 s,
4831 req,
4832 req->rex, req->wex,
4833 req->flags,
4834 req->buf->i,
4835 req->analysers);
4836
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004837 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4838 return 0;
4839
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004840 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004841 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004842 /* Output closed while we were sending data. We must abort and
4843 * wake the other side up.
4844 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004845 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004846 msg->msg_state = HTTP_MSG_ERROR;
4847 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004848 return 1;
4849 }
4850
Willy Tarreaud98cf932009-12-27 22:54:55 +01004851 /* Note that we don't have to send 100-continue back because we don't
4852 * need the data to complete our job, and it's up to the server to
4853 * decide whether to return 100, 417 or anything else in return of
4854 * an "Expect: 100-continue" header.
4855 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004856 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004857 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4858 ? HTTP_MSG_CHUNK_SIZE
4859 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004860
4861 /* TODO/filters: when http-buffer-request option is set or if a
4862 * rule on url_param exists, the first chunk size could be
4863 * already parsed. In that case, msg->next is after the chunk
4864 * size (including the CRLF after the size). So this case should
4865 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004866 }
4867
Willy Tarreau7ba23542014-04-17 21:50:00 +02004868 /* Some post-connect processing might want us to refrain from starting to
4869 * forward data. Currently, the only reason for this is "balance url_param"
4870 * whichs need to parse/process the request after we've enabled forwarding.
4871 */
4872 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004873 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004874 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004875 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004876 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004877 }
4878 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4879 }
4880
Willy Tarreau80a92c02014-03-12 10:41:13 +01004881 /* in most states, we should abort in case of early close */
4882 channel_auto_close(req);
4883
Willy Tarreauefdf0942014-04-24 20:08:57 +02004884 if (req->to_forward) {
4885 /* We can't process the buffer's contents yet */
4886 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004887 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004888 }
4889
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004890 if (msg->msg_state < HTTP_MSG_DONE) {
4891 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4892 ? http_msg_forward_chunked_body(s, msg)
4893 : http_msg_forward_body(s, msg));
4894 if (!ret)
4895 goto missing_data_or_waiting;
4896 if (ret < 0)
4897 goto return_bad_req;
4898 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004899
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004900 /* other states, DONE...TUNNEL */
4901 /* we don't want to forward closes on DONE except in tunnel mode. */
4902 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4903 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004904
Christopher Faulet894da4c2017-07-18 11:29:07 +02004905 http_resync_states(s);
4906 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004907 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4908 if (req->flags & CF_SHUTW) {
4909 /* request errors are most likely due to the
4910 * server aborting the transfer. */
4911 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004912 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004913 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004914 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004915 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004916 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004917 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004918 }
4919
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004920 /* If "option abortonclose" is set on the backend, we want to monitor
4921 * the client's connection and forward any shutdown notification to the
4922 * server, which will decide whether to close or to go on processing the
4923 * request. We only do that in tunnel mode, and not in other modes since
4924 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004925 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004926 channel_auto_read(req);
4927 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4928 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4929 s->si[1].flags |= SI_FL_NOLINGER;
4930 channel_auto_close(req);
4931 }
4932 else if (s->txn->meth == HTTP_METH_POST) {
4933 /* POST requests may require to read extra CRLF sent by broken
4934 * browsers and which could cause an RST to be sent upon close
4935 * on some systems (eg: Linux). */
4936 channel_auto_read(req);
4937 }
4938 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004939
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004940 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004941 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004942 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004943 if (!(s->flags & SF_ERR_MASK))
4944 s->flags |= SF_ERR_CLICL;
4945 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004946 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004947 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004948 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004949 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004950 }
4951
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004952 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4953 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004954 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004955 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004956
4957 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004958 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004959
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004960 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004961 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004962 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004964 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004965 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004966 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004967 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004968 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004969
Willy Tarreau5c620922011-05-11 19:56:11 +02004970 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004971 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004972 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004973 * modes are already handled by the stream sock layer. We must not do
4974 * this in content-length mode because it could present the MSG_MORE
4975 * flag with the last block of forwarded data, which would cause an
4976 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004977 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004978 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004979 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004980
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004981 return 0;
4982
Willy Tarreaud98cf932009-12-27 22:54:55 +01004983 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004984 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004985 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004986 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004987
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004988 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004989 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004990 txn->req.msg_state = HTTP_MSG_ERROR;
4991 if (txn->status) {
4992 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004993 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004994 } else {
4995 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004996 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004997 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004998 req->analysers &= AN_REQ_FLT_END;
4999 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 +01005000
Willy Tarreaue7dff022015-04-03 01:14:29 +02005001 if (!(s->flags & SF_ERR_MASK))
5002 s->flags |= SF_ERR_PRXCOND;
5003 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005004 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005005 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005006 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005007 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005008 }
5009 return 0;
5010
5011 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005012 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005013 txn->req.msg_state = HTTP_MSG_ERROR;
5014 if (txn->status) {
5015 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005016 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005017 } else {
5018 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005019 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005020 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005021 req->analysers &= AN_REQ_FLT_END;
5022 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 +01005023
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005024 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5025 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005026 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005027 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005028
Willy Tarreaue7dff022015-04-03 01:14:29 +02005029 if (!(s->flags & SF_ERR_MASK))
5030 s->flags |= SF_ERR_SRVCL;
5031 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005032 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005033 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005034 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005035 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005036 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005037 return 0;
5038}
5039
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005040/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5041 * processing can continue on next analysers, or zero if it either needs more
5042 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005043 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005044 * when it has nothing left to do, and may remove any analyser when it wants to
5045 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005046 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005047int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005048{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005049 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005050 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005051 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005052 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005053 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005055 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005056
Willy Tarreau87b09662015-04-03 00:22:06 +02005057 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005058 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005059 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005060 rep,
5061 rep->rex, rep->wex,
5062 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005063 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005064 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005065
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005066 /*
5067 * Now parse the partial (or complete) lines.
5068 * We will check the response syntax, and also join multi-line
5069 * headers. An index of all the lines will be elaborated while
5070 * parsing.
5071 *
5072 * For the parsing, we use a 28 states FSM.
5073 *
5074 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005075 * rep->buf->p = beginning of response
5076 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5077 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005078 * msg->eol = end of current header or line (LF or CRLF)
5079 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005080 */
5081
Willy Tarreau628c40c2014-04-24 19:11:26 +02005082 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005083 /* There's a protected area at the end of the buffer for rewriting
5084 * purposes. We don't want to start to parse the request if the
5085 * protected area is affected, because we may have to move processed
5086 * data later, which is much more complicated.
5087 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005088 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005089 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005090 /* some data has still not left the buffer, wake us once that's done */
5091 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5092 goto abort_response;
5093 channel_dont_close(rep);
5094 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005095 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005096 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005097 }
5098
Willy Tarreau379357a2013-06-08 12:55:46 +02005099 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5100 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5101 buffer_slow_realign(rep->buf);
5102
Willy Tarreau9b28e032012-10-12 23:49:43 +02005103 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005104 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005105 }
5106
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005107 /* 1: we might have to print this header in debug mode */
5108 if (unlikely((global.mode & MODE_DEBUG) &&
5109 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005110 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005111 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005112
Willy Tarreau9b28e032012-10-12 23:49:43 +02005113 sol = rep->buf->p;
5114 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005115 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005116
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005117 sol += hdr_idx_first_pos(&txn->hdr_idx);
5118 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 while (cur_idx) {
5121 eol = sol + txn->hdr_idx.v[cur_idx].len;
5122 debug_hdr("srvhdr", s, sol, eol);
5123 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5124 cur_idx = txn->hdr_idx.v[cur_idx].next;
5125 }
5126 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005127
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005128 /*
5129 * Now we quickly check if we have found a full valid response.
5130 * If not so, we check the FD and buffer states before leaving.
5131 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005132 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005133 * responses are checked first.
5134 *
5135 * Depending on whether the client is still there or not, we
5136 * may send an error response back or not. Note that normally
5137 * we should only check for HTTP status there, and check I/O
5138 * errors somewhere else.
5139 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140
Willy Tarreau655dce92009-11-08 13:10:58 +01005141 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142 /* Invalid response */
5143 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5144 /* we detected a parsing error. We want to archive this response
5145 * in the dedicated proxy area for later troubleshooting.
5146 */
5147 hdr_response_bad:
5148 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005149 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005150
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005151 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005152 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005153 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005154 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005155 }
Willy Tarreau64648412010-03-05 10:41:54 +01005156 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005157 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005158 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005159 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005160 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005161 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005162 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005163
Willy Tarreaue7dff022015-04-03 01:14:29 +02005164 if (!(s->flags & SF_ERR_MASK))
5165 s->flags |= SF_ERR_PRXCOND;
5166 if (!(s->flags & SF_FINST_MASK))
5167 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005168
5169 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005170 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005171
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005172 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005173 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005174 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005175 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005176 goto hdr_response_bad;
5177 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005178
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005179 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005180 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005181 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005182 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005183 else if (txn->flags & TX_NOT_FIRST)
5184 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005185
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005186 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005187 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005188 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005189 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005190 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005191
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005192 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005193 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005194 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005195
5196 /* Check to see if the server refused the early data.
5197 * If so, just send a 425
5198 */
5199 if (objt_cs(s->si[1].end)) {
5200 struct connection *conn = objt_cs(s->si[1].end)->conn;
5201
5202 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5203 txn->status = 425;
5204 }
5205
Willy Tarreau350f4872014-11-28 14:42:25 +01005206 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005207 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005208 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005209
Willy Tarreaue7dff022015-04-03 01:14:29 +02005210 if (!(s->flags & SF_ERR_MASK))
5211 s->flags |= SF_ERR_SRVCL;
5212 if (!(s->flags & SF_FINST_MASK))
5213 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005214 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005215 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005216
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005217 /* read timeout : return a 504 to the client. */
5218 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005220 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005221
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005222 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005223 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005224 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005225 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005226 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005227
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005228 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005229 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005230 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005231 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005232 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005233 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005234
Willy Tarreaue7dff022015-04-03 01:14:29 +02005235 if (!(s->flags & SF_ERR_MASK))
5236 s->flags |= SF_ERR_SRVTO;
5237 if (!(s->flags & SF_FINST_MASK))
5238 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005239 return 0;
5240 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005241
Willy Tarreauf003d372012-11-26 13:35:37 +01005242 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005243 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005244 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5245 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005246 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005247 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005248
Christopher Faulet0184ea72017-01-05 14:06:34 +01005249 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005250 channel_auto_close(rep);
5251
5252 txn->status = 400;
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 Tarreauf003d372012-11-26 13:35:37 +01005255
Willy Tarreaue7dff022015-04-03 01:14:29 +02005256 if (!(s->flags & SF_ERR_MASK))
5257 s->flags |= SF_ERR_CLICL;
5258 if (!(s->flags & SF_FINST_MASK))
5259 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005260
Willy Tarreau87b09662015-04-03 00:22:06 +02005261 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005262 return 0;
5263 }
5264
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005265 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005266 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005267 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005268 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005269 else if (txn->flags & TX_NOT_FIRST)
5270 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005271
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005272 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005273 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005274 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005275 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005276 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005277
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005278 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005279 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005280 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005281 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005282 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005283 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005284
Willy Tarreaue7dff022015-04-03 01:14:29 +02005285 if (!(s->flags & SF_ERR_MASK))
5286 s->flags |= SF_ERR_SRVCL;
5287 if (!(s->flags & SF_FINST_MASK))
5288 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005289 return 0;
5290 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005291
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005293 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005294 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005295 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005296 else if (txn->flags & TX_NOT_FIRST)
5297 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005298
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005299 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005300 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005301 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005302
Willy Tarreaue7dff022015-04-03 01:14:29 +02005303 if (!(s->flags & SF_ERR_MASK))
5304 s->flags |= SF_ERR_CLICL;
5305 if (!(s->flags & SF_FINST_MASK))
5306 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005307
Willy Tarreau87b09662015-04-03 00:22:06 +02005308 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005309 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005310 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005311
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005312 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005313 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005314 return 0;
5315 }
5316
5317 /* More interesting part now : we know that we have a complete
5318 * response which at least looks like HTTP. We have an indicator
5319 * of each header's length, so we can parse them quickly.
5320 */
5321
5322 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005323 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005324
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005325 /*
5326 * 1: get the status code
5327 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005328 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005329 if (n < 1 || n > 5)
5330 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005331 /* when the client triggers a 4xx from the server, it's most often due
5332 * to a missing object or permission. These events should be tracked
5333 * because if they happen often, it may indicate a brute force or a
5334 * vulnerability scan.
5335 */
5336 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005337 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005338
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005339 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005340 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005341
Willy Tarreau91852eb2015-05-01 13:26:00 +02005342 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5343 * exactly one digit "." one digit. This check may be disabled using
5344 * option accept-invalid-http-response.
5345 */
5346 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5347 if (msg->sl.st.v_l != 8) {
5348 msg->err_pos = 0;
5349 goto hdr_response_bad;
5350 }
5351
5352 if (rep->buf->p[4] != '/' ||
5353 !isdigit((unsigned char)rep->buf->p[5]) ||
5354 rep->buf->p[6] != '.' ||
5355 !isdigit((unsigned char)rep->buf->p[7])) {
5356 msg->err_pos = 4;
5357 goto hdr_response_bad;
5358 }
5359 }
5360
Willy Tarreau5b154472009-12-21 20:11:07 +01005361 /* check if the response is HTTP/1.1 or above */
5362 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005363 ((rep->buf->p[5] > '1') ||
5364 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005365 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005366
5367 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005368 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 +01005369
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005370 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005371 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005372
Willy Tarreau9b28e032012-10-12 23:49:43 +02005373 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374
Willy Tarreau39650402010-03-15 19:44:39 +01005375 /* Adjust server's health based on status code. Note: status codes 501
5376 * and 505 are triggered on demand by client request, so we must not
5377 * count them as server failures.
5378 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005379 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005380 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005381 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005382 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005383 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005384 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005385
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005386 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005387 * We may be facing a 100-continue response, or any other informational
5388 * 1xx response which is non-final, in which case this is not the right
5389 * response, and we're waiting for the next one. Let's allow this response
5390 * to go to the client and wait for the next one. There's an exception for
5391 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005392 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005393 if (txn->status < 200 &&
5394 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005395 hdr_idx_init(&txn->hdr_idx);
5396 msg->next -= channel_forward(rep, msg->next);
5397 msg->msg_state = HTTP_MSG_RPBEFORE;
5398 txn->status = 0;
5399 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005400 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005401 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005402 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005403
Willy Tarreaua14ad722017-07-07 11:36:32 +02005404 /*
5405 * 2: check for cacheability.
5406 */
5407
5408 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005409 case 200:
5410 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005411 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005412 case 206:
5413 case 300:
5414 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005415 case 404:
5416 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005417 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005418 case 414:
5419 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005420 break;
5421 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005422 /* RFC7231#6.1:
5423 * Responses with status codes that are defined as
5424 * cacheable by default (e.g., 200, 203, 204, 206,
5425 * 300, 301, 404, 405, 410, 414, and 501 in this
5426 * specification) can be reused by a cache with
5427 * heuristic expiration unless otherwise indicated
5428 * by the method definition or explicit cache
5429 * controls [RFC7234]; all other status codes are
5430 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005431 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005432 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433 break;
5434 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005435
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005436 /*
5437 * 3: we may need to capture headers
5438 */
5439 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005440 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005441 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005442 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005443
Willy Tarreau557f1992015-05-01 10:05:17 +02005444 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5445 * by RFC7230#3.3.3 :
5446 *
5447 * The length of a message body is determined by one of the following
5448 * (in order of precedence):
5449 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005450 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5451 * the connection will become a tunnel immediately after the empty
5452 * line that concludes the header fields. A client MUST ignore
5453 * any Content-Length or Transfer-Encoding header fields received
5454 * in such a message. Any 101 response (Switching Protocols) is
5455 * managed in the same manner.
5456 *
5457 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005458 * (Informational), 204 (No Content), or 304 (Not Modified) status
5459 * code is always terminated by the first empty line after the
5460 * header fields, regardless of the header fields present in the
5461 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005462 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005463 * 3. If a Transfer-Encoding header field is present and the chunked
5464 * transfer coding (Section 4.1) is the final encoding, the message
5465 * body length is determined by reading and decoding the chunked
5466 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005467 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005468 * If a Transfer-Encoding header field is present in a response and
5469 * the chunked transfer coding is not the final encoding, the
5470 * message body length is determined by reading the connection until
5471 * it is closed by the server. If a Transfer-Encoding header field
5472 * is present in a request and the chunked transfer coding is not
5473 * the final encoding, the message body length cannot be determined
5474 * reliably; the server MUST respond with the 400 (Bad Request)
5475 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005476 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005477 * If a message is received with both a Transfer-Encoding and a
5478 * Content-Length header field, the Transfer-Encoding overrides the
5479 * Content-Length. Such a message might indicate an attempt to
5480 * perform request smuggling (Section 9.5) or response splitting
5481 * (Section 9.4) and ought to be handled as an error. A sender MUST
5482 * remove the received Content-Length field prior to forwarding such
5483 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005484 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005485 * 4. If a message is received without Transfer-Encoding and with
5486 * either multiple Content-Length header fields having differing
5487 * field-values or a single Content-Length header field having an
5488 * invalid value, then the message framing is invalid and the
5489 * recipient MUST treat it as an unrecoverable error. If this is a
5490 * request message, the server MUST respond with a 400 (Bad Request)
5491 * status code and then close the connection. If this is a response
5492 * message received by a proxy, the proxy MUST close the connection
5493 * to the server, discard the received response, and send a 502 (Bad
5494 * Gateway) response to the client. If this is a response message
5495 * received by a user agent, the user agent MUST close the
5496 * connection to the server and discard the received response.
5497 *
5498 * 5. If a valid Content-Length header field is present without
5499 * Transfer-Encoding, its decimal value defines the expected message
5500 * body length in octets. If the sender closes the connection or
5501 * the recipient times out before the indicated number of octets are
5502 * received, the recipient MUST consider the message to be
5503 * incomplete and close the connection.
5504 *
5505 * 6. If this is a request message and none of the above are true, then
5506 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005507 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005508 * 7. Otherwise, this is a response message without a declared message
5509 * body length, so the message body length is determined by the
5510 * number of octets received prior to the server closing the
5511 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005512 */
5513
5514 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005515 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005516 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005517 * FIXME: should we parse anyway and return an error on chunked encoding ?
5518 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005519 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5520 txn->status == 101)) {
5521 /* Either we've established an explicit tunnel, or we're
5522 * switching the protocol. In both cases, we're very unlikely
5523 * to understand the next protocols. We have to switch to tunnel
5524 * mode, so that we transfer the request and responses then let
5525 * this protocol pass unmodified. When we later implement specific
5526 * parsers for such protocols, we'll want to check the Upgrade
5527 * header which contains information about that protocol for
5528 * responses with status 101 (eg: see RFC2817 about TLS).
5529 */
5530 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5531 msg->flags |= HTTP_MSGF_XFER_LEN;
5532 goto end;
5533 }
5534
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005535 if (txn->meth == HTTP_METH_HEAD ||
5536 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005537 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005538 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005539 goto skip_content_length;
5540 }
5541
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005542 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005543 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005544 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005545 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005546 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5547 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005548 /* bad transfer-encoding (chunked followed by something else) */
5549 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005550 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005551 break;
5552 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005553 }
5554
Willy Tarreau1c913912015-04-30 10:57:51 +02005555 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005556 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005557 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005558 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5559 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5560 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005561 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005562 signed long long cl;
5563
Willy Tarreauad14f752011-09-02 20:33:27 +02005564 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005565 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005566 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005567 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005568
Willy Tarreauad14f752011-09-02 20:33:27 +02005569 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005570 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005571 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005572 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005573
Willy Tarreauad14f752011-09-02 20:33:27 +02005574 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005575 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005576 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005577 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005578
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005579 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005580 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005581 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005582 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005583
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005584 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005585 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005586 }
5587
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005588 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005589 /* Now we have to check if we need to modify the Connection header.
5590 * This is more difficult on the response than it is on the request,
5591 * because we can have two different HTTP versions and we don't know
5592 * how the client will interprete a response. For instance, let's say
5593 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5594 * HTTP/1.1 response without any header. Maybe it will bound itself to
5595 * HTTP/1.0 because it only knows about it, and will consider the lack
5596 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5597 * the lack of header as a keep-alive. Thus we will use two flags
5598 * indicating how a request MAY be understood by the client. In case
5599 * of multiple possibilities, we'll fix the header to be explicit. If
5600 * ambiguous cases such as both close and keepalive are seen, then we
5601 * will fall back to explicit close. Note that we won't take risks with
5602 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005603 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005604 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005605 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5606 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5607 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5608 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005609 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005610
Willy Tarreau70dffda2014-01-30 03:07:23 +01005611 /* this situation happens when combining pretend-keepalive with httpclose. */
5612 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005613 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005614 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005615 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5616
Willy Tarreau60466522010-01-18 19:08:45 +01005617 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005618 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005619 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5620 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005621
Willy Tarreau60466522010-01-18 19:08:45 +01005622 /* now adjust header transformations depending on current state */
5623 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5624 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5625 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005626 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005627 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005628 }
Willy Tarreau60466522010-01-18 19:08:45 +01005629 else { /* SCL / KAL */
5630 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005631 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005632 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005633 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005634
Willy Tarreau60466522010-01-18 19:08:45 +01005635 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005636 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005637
Willy Tarreau60466522010-01-18 19:08:45 +01005638 /* Some keep-alive responses are converted to Server-close if
5639 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005640 */
Willy Tarreau60466522010-01-18 19:08:45 +01005641 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5642 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005643 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005644 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005645 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005646 }
5647
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005648 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005649 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005650 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005651
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005652 /* end of job, return OK */
5653 rep->analysers &= ~an_bit;
5654 rep->analyse_exp = TICK_ETERNITY;
5655 channel_auto_close(rep);
5656 return 1;
5657
5658 abort_keep_alive:
5659 /* A keep-alive request to the server failed on a network error.
5660 * The client is required to retry. We need to close without returning
5661 * any other information so that the client retries.
5662 */
5663 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005664 rep->analysers &= AN_RES_FLT_END;
5665 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005666 channel_auto_close(rep);
5667 s->logs.logwait = 0;
5668 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005669 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005670 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005671 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005672 return 0;
5673}
5674
5675/* This function performs all the processing enabled for the current response.
5676 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005677 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005678 * other functions. It works like process_request (see indications above).
5679 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005680int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005681{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005682 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005683 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005684 struct http_msg *msg = &txn->rsp;
5685 struct proxy *cur_proxy;
5686 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005687 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005688
Willy Tarreau87b09662015-04-03 00:22:06 +02005689 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005690 now_ms, __FUNCTION__,
5691 s,
5692 rep,
5693 rep->rex, rep->wex,
5694 rep->flags,
5695 rep->buf->i,
5696 rep->analysers);
5697
5698 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5699 return 0;
5700
Willy Tarreau70730dd2014-04-24 18:06:27 +02005701 /* The stats applet needs to adjust the Connection header but we don't
5702 * apply any filter there.
5703 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005704 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5705 rep->analysers &= ~an_bit;
5706 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005707 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005708 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005709
Willy Tarreau58975672014-04-24 21:13:57 +02005710 /*
5711 * We will have to evaluate the filters.
5712 * As opposed to version 1.2, now they will be evaluated in the
5713 * filters order and not in the header order. This means that
5714 * each filter has to be validated among all headers.
5715 *
5716 * Filters are tried with ->be first, then with ->fe if it is
5717 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005718 *
5719 * Maybe we are in resume condiion. In this case I choose the
5720 * "struct proxy" which contains the rule list matching the resume
5721 * pointer. If none of theses "struct proxy" match, I initialise
5722 * the process with the first one.
5723 *
5724 * In fact, I check only correspondance betwwen the current list
5725 * pointer and the ->fe rule list. If it doesn't match, I initialize
5726 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005727 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005728 if (s->current_rule_list == &sess->fe->http_res_rules)
5729 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005730 else
5731 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005732 while (1) {
5733 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005734
Willy Tarreau58975672014-04-24 21:13:57 +02005735 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005736 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005737 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005738
Willy Tarreau51d861a2015-05-22 17:30:48 +02005739 if (ret == HTTP_RULE_RES_BADREQ)
5740 goto return_srv_prx_502;
5741
5742 if (ret == HTTP_RULE_RES_DONE) {
5743 rep->analysers &= ~an_bit;
5744 rep->analyse_exp = TICK_ETERNITY;
5745 return 1;
5746 }
5747 }
5748
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005749 /* we need to be called again. */
5750 if (ret == HTTP_RULE_RES_YIELD) {
5751 channel_dont_close(rep);
5752 return 0;
5753 }
5754
Willy Tarreau58975672014-04-24 21:13:57 +02005755 /* try headers filters */
5756 if (rule_set->rsp_exp != NULL) {
5757 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5758 return_bad_resp:
5759 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005760 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005761 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005762 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005763 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005764 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005765 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005766 txn->status = 502;
5767 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005768 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005769 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005770 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005771 if (!(s->flags & SF_ERR_MASK))
5772 s->flags |= SF_ERR_PRXCOND;
5773 if (!(s->flags & SF_FINST_MASK))
5774 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005775 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005776 }
Willy Tarreau58975672014-04-24 21:13:57 +02005777 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005778
Willy Tarreau58975672014-04-24 21:13:57 +02005779 /* has the response been denied ? */
5780 if (txn->flags & TX_SVDENY) {
5781 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005782 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005783
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005784 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5785 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005786 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005787 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005788
Willy Tarreau58975672014-04-24 21:13:57 +02005789 goto return_srv_prx_502;
5790 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005791
Willy Tarreau58975672014-04-24 21:13:57 +02005792 /* add response headers from the rule sets in the same order */
5793 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005794 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005795 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005796 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005797 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005798 ret = acl_pass(ret);
5799 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5800 ret = !ret;
5801 if (!ret)
5802 continue;
5803 }
5804 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5805 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005806 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005807
Willy Tarreau58975672014-04-24 21:13:57 +02005808 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005809 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005810 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005811 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005812 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005813
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005814 /* After this point, this anayzer can't return yield, so we can
5815 * remove the bit corresponding to this analyzer from the list.
5816 *
5817 * Note that the intermediate returns and goto found previously
5818 * reset the analyzers.
5819 */
5820 rep->analysers &= ~an_bit;
5821 rep->analyse_exp = TICK_ETERNITY;
5822
Willy Tarreau58975672014-04-24 21:13:57 +02005823 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005824 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005825 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005826
Willy Tarreau58975672014-04-24 21:13:57 +02005827 /*
5828 * Now check for a server cookie.
5829 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005830 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005831 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005832
Willy Tarreau58975672014-04-24 21:13:57 +02005833 /*
5834 * Check for cache-control or pragma headers if required.
5835 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005836 if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101)
Willy Tarreau58975672014-04-24 21:13:57 +02005837 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005838
Willy Tarreau58975672014-04-24 21:13:57 +02005839 /*
5840 * Add server cookie in the response if needed
5841 */
5842 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5843 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005844 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005845 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5846 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5847 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5848 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5849 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005851 /* the server is known, it's not the one the client requested, or the
5852 * cookie's last seen date needs to be refreshed. We have to
5853 * insert a set-cookie here, except if we want to insert only on POST
5854 * requests and this one isn't. Note that servers which don't have cookies
5855 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005856 */
Willy Tarreau58975672014-04-24 21:13:57 +02005857 if (!objt_server(s->target)->cookie) {
5858 chunk_printf(&trash,
5859 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5860 s->be->cookie_name);
5861 }
5862 else {
5863 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005864
Willy Tarreau58975672014-04-24 21:13:57 +02005865 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5866 /* emit last_date, which is mandatory */
5867 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5868 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5869 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005870
Willy Tarreau58975672014-04-24 21:13:57 +02005871 if (s->be->cookie_maxlife) {
5872 /* emit first_date, which is either the original one or
5873 * the current date.
5874 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005875 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005876 s30tob64(txn->cookie_first_date ?
5877 txn->cookie_first_date >> 2 :
5878 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005879 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005880 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005881 }
Willy Tarreau58975672014-04-24 21:13:57 +02005882 chunk_appendf(&trash, "; path=/");
5883 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005884
Willy Tarreau58975672014-04-24 21:13:57 +02005885 if (s->be->cookie_domain)
5886 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005887
Willy Tarreau58975672014-04-24 21:13:57 +02005888 if (s->be->ck_opts & PR_CK_HTTPONLY)
5889 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005890
Willy Tarreau58975672014-04-24 21:13:57 +02005891 if (s->be->ck_opts & PR_CK_SECURE)
5892 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005893
Willy Tarreau58975672014-04-24 21:13:57 +02005894 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5895 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005896
Willy Tarreau58975672014-04-24 21:13:57 +02005897 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005898 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005899 /* the server did not change, only the date was updated */
5900 txn->flags |= TX_SCK_UPDATED;
5901 else
5902 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005903
Willy Tarreau58975672014-04-24 21:13:57 +02005904 /* Here, we will tell an eventual cache on the client side that we don't
5905 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5906 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5907 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005908 */
Willy Tarreau58975672014-04-24 21:13:57 +02005909 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005910
Willy Tarreau58975672014-04-24 21:13:57 +02005911 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005912
Willy Tarreau58975672014-04-24 21:13:57 +02005913 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5914 "Cache-control: private", 22) < 0))
5915 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005916 }
Willy Tarreau58975672014-04-24 21:13:57 +02005917 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005918
Willy Tarreau58975672014-04-24 21:13:57 +02005919 /*
5920 * Check if result will be cacheable with a cookie.
5921 * We'll block the response if security checks have caught
5922 * nasty things such as a cacheable cookie.
5923 */
5924 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5925 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5926 (s->be->options & PR_O_CHK_CACHE)) {
5927 /* we're in presence of a cacheable response containing
5928 * a set-cookie header. We'll block it as requested by
5929 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005930 */
Willy Tarreau58975672014-04-24 21:13:57 +02005931 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005932 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005933
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005934 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5935 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005936 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005937 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005938
Christopher Faulet767a84b2017-11-24 16:50:31 +01005939 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5940 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005941 send_log(s->be, LOG_ALERT,
5942 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5943 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5944 goto return_srv_prx_502;
5945 }
Willy Tarreau03945942009-12-22 16:50:27 +01005946
Willy Tarreau70730dd2014-04-24 18:06:27 +02005947 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005948 /*
5949 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5950 * If an "Upgrade" token is found, the header is left untouched in order
5951 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005952 * if anything but "Upgrade" is present in the Connection header. We don't
5953 * want to touch any 101 response either since it's switching to another
5954 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005955 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005956 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005957 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005958 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005959 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5960 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005961
Willy Tarreau58975672014-04-24 21:13:57 +02005962 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5963 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5964 /* we want a keep-alive response here. Keep-alive header
5965 * required if either side is not 1.1.
5966 */
5967 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5968 want_flags |= TX_CON_KAL_SET;
5969 }
5970 else {
5971 /* we want a close response here. Close header required if
5972 * the server is 1.1, regardless of the client.
5973 */
5974 if (msg->flags & HTTP_MSGF_VER_11)
5975 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005976 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005977
Willy Tarreau58975672014-04-24 21:13:57 +02005978 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5979 http_change_connection_header(txn, msg, want_flags);
5980 }
5981
5982 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005983 /* Always enter in the body analyzer */
5984 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5985 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005986
Willy Tarreau58975672014-04-24 21:13:57 +02005987 /* if the user wants to log as soon as possible, without counting
5988 * bytes from the server, then this is the right moment. We have
5989 * to temporarily assign bytes_out to log what we currently have.
5990 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005991 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005992 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5993 s->logs.bytes_out = txn->rsp.eoh;
5994 s->do_log(s);
5995 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005996 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005997 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005998}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005999
Willy Tarreaud98cf932009-12-27 22:54:55 +01006000/* This function is an analyser which forwards response body (including chunk
6001 * sizes if any). It is called as soon as we must forward, even if we forward
6002 * zero byte. The only situation where it must not be called is when we're in
6003 * tunnel mode and we want to forward till the close. It's used both to forward
6004 * remaining data and to resync after end of body. It expects the msg_state to
6005 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006006 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006007 *
6008 * It is capable of compressing response data both in content-length mode and
6009 * in chunked mode. The state machines follows different flows depending on
6010 * whether content-length and chunked modes are used, since there are no
6011 * trailers in content-length :
6012 *
6013 * chk-mode cl-mode
6014 * ,----- BODY -----.
6015 * / \
6016 * V size > 0 V chk-mode
6017 * .--> SIZE -------------> DATA -------------> CRLF
6018 * | | size == 0 | last byte |
6019 * | v final crlf v inspected |
6020 * | TRAILERS -----------> DONE |
6021 * | |
6022 * `----------------------------------------------'
6023 *
6024 * Compression only happens in the DATA state, and must be flushed in final
6025 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6026 * is performed at once on final states for all bytes parsed, or when leaving
6027 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006028 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006029int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006030{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006031 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006032 struct http_txn *txn = s->txn;
6033 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006034 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006035
Christopher Faulet814d2702017-03-30 11:33:44 +02006036 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6037 now_ms, __FUNCTION__,
6038 s,
6039 res,
6040 res->rex, res->wex,
6041 res->flags,
6042 res->buf->i,
6043 res->analysers);
6044
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006045 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6046 return 0;
6047
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006048 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006049 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006050 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006051 /* Output closed while we were sending data. We must abort and
6052 * wake the other side up.
6053 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006054 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006055 msg->msg_state = HTTP_MSG_ERROR;
6056 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006057 return 1;
6058 }
6059
Willy Tarreau4fe41902010-06-07 22:27:41 +02006060 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006061 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006062
Christopher Fauletd7c91962015-04-30 11:48:27 +02006063 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006064 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6065 ? HTTP_MSG_CHUNK_SIZE
6066 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006067 }
6068
Willy Tarreauefdf0942014-04-24 20:08:57 +02006069 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006070 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006071 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006072 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006073 }
6074
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006075 if (msg->msg_state < HTTP_MSG_DONE) {
6076 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6077 ? http_msg_forward_chunked_body(s, msg)
6078 : http_msg_forward_body(s, msg));
6079 if (!ret)
6080 goto missing_data_or_waiting;
6081 if (ret < 0)
6082 goto return_bad_res;
6083 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006084
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006085 /* other states, DONE...TUNNEL */
6086 /* for keep-alive we don't want to forward closes on DONE */
6087 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6088 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6089 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006090
Christopher Faulet894da4c2017-07-18 11:29:07 +02006091 http_resync_states(s);
6092 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006093 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6094 if (res->flags & CF_SHUTW) {
6095 /* response errors are most likely due to the
6096 * client aborting the transfer. */
6097 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006098 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006099 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006100 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006101 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006102 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006103 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006104 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006105 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006106
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006107 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006108 if (res->flags & CF_SHUTW)
6109 goto aborted_xfer;
6110
6111 /* stop waiting for data if the input is closed before the end. If the
6112 * client side was already closed, it means that the client has aborted,
6113 * so we don't want to count this as a server abort. Otherwise it's a
6114 * server abort.
6115 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006116 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006117 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006118 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006119 /* If we have some pending data, we continue the processing */
6120 if (!buffer_pending(res->buf)) {
6121 if (!(s->flags & SF_ERR_MASK))
6122 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006123 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006124 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006125 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006126 goto return_bad_res_stats_ok;
6127 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006128 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006129
Willy Tarreau40dba092010-03-04 18:14:51 +01006130 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006131 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006132 goto return_bad_res;
6133
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006134 /* When TE: chunked is used, we need to get there again to parse
6135 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006136 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6137 * or if there are filters registered on the stream, we don't want to
6138 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006139 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006140 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006141 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006142 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6143 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006144 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006145
Willy Tarreau5c620922011-05-11 19:56:11 +02006146 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006147 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006148 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006149 * modes are already handled by the stream sock layer. We must not do
6150 * this in content-length mode because it could present the MSG_MORE
6151 * flag with the last block of forwarded data, which would cause an
6152 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006153 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006154 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006155 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006156
Willy Tarreau87b09662015-04-03 00:22:06 +02006157 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006158 return 0;
6159
Willy Tarreau40dba092010-03-04 18:14:51 +01006160 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006161 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006162 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006163 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006164
6165 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006166 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006167 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006168 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006169 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006170 res->analysers &= AN_RES_FLT_END;
6171 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 +01006172 if (objt_server(s->target))
6173 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006174
Willy Tarreaue7dff022015-04-03 01:14:29 +02006175 if (!(s->flags & SF_ERR_MASK))
6176 s->flags |= SF_ERR_PRXCOND;
6177 if (!(s->flags & SF_FINST_MASK))
6178 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006179 return 0;
6180
6181 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006182 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006183 txn->rsp.msg_state = HTTP_MSG_ERROR;
6184 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006185 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006186 res->analysers &= AN_RES_FLT_END;
6187 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 +01006188
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006189 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6190 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006191 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006192 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006193
Willy Tarreaue7dff022015-04-03 01:14:29 +02006194 if (!(s->flags & SF_ERR_MASK))
6195 s->flags |= SF_ERR_CLICL;
6196 if (!(s->flags & SF_FINST_MASK))
6197 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006198 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006199}
6200
6201
6202static inline int
6203http_msg_forward_body(struct stream *s, struct http_msg *msg)
6204{
6205 struct channel *chn = msg->chn;
6206 int ret;
6207
6208 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6209
6210 if (msg->msg_state == HTTP_MSG_ENDING)
6211 goto ending;
6212
6213 /* Neither content-length, nor transfer-encoding was found, so we must
6214 * read the body until the server connection is closed. In that case, we
6215 * eat data as they come. Of course, this happens for response only. */
6216 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6217 unsigned long long len = (chn->buf->i - msg->next);
6218 msg->chunk_len += len;
6219 msg->body_len += len;
6220 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006221 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6222 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6223 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006224 msg->next += ret;
6225 msg->chunk_len -= ret;
6226 if (msg->chunk_len) {
6227 /* input empty or output full */
6228 if (chn->buf->i > msg->next)
6229 chn->flags |= CF_WAKE_WRITE;
6230 goto missing_data_or_waiting;
6231 }
6232
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006233 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6234 * always set for a request. */
6235 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6236 /* The server still sending data that should be filtered */
6237 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6238 goto missing_data_or_waiting;
6239 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006240
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006241 msg->msg_state = HTTP_MSG_ENDING;
6242
6243 ending:
6244 /* we may have some pending data starting at res->buf->p such as a last
6245 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006246 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6247 /* default_ret */ msg->next,
6248 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006249 b_adv(chn->buf, ret);
6250 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006251 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6252 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006253 if (msg->next)
6254 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006255
Christopher Fauletda02e172015-12-04 09:25:05 +01006256 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6257 /* default_ret */ 1,
6258 /* on_error */ goto error,
6259 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006260 msg->msg_state = HTTP_MSG_DONE;
6261 return 1;
6262
6263 missing_data_or_waiting:
6264 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006265 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6266 /* default_ret */ msg->next,
6267 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006268 b_adv(chn->buf, ret);
6269 msg->next -= ret;
6270 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6271 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006272 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006273 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006274 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006275 return 0;
6276 error:
6277 return -1;
6278}
6279
6280static inline int
6281http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6282{
6283 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006284 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006285 int ret;
6286
6287 /* Here we have the guarantee to be in one of the following state:
6288 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6289 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6290
6291 switch_states:
6292 switch (msg->msg_state) {
6293 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006294 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6295 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6296 /* on_error */ goto error);
6297 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006298 msg->chunk_len -= ret;
6299 if (msg->chunk_len) {
6300 /* input empty or output full */
6301 if (chn->buf->i > msg->next)
6302 chn->flags |= CF_WAKE_WRITE;
6303 goto missing_data_or_waiting;
6304 }
6305
6306 /* nothing left to forward for this chunk*/
6307 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6308 /* fall through for HTTP_MSG_CHUNK_CRLF */
6309
6310 case HTTP_MSG_CHUNK_CRLF:
6311 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006312 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006313 if (ret == 0)
6314 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006315 if (ret < 0) {
6316 msg->err_pos = chn->buf->i + ret;
6317 if (msg->err_pos < 0)
6318 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006319 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006320 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006321 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006322 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6323 /* fall through for HTTP_MSG_CHUNK_SIZE */
6324
6325 case HTTP_MSG_CHUNK_SIZE:
6326 /* read the chunk size and assign it to ->chunk_len,
6327 * then set ->next to point to the body and switch to
6328 * DATA or TRAILERS state.
6329 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006330 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006331 if (ret == 0)
6332 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006333 if (ret < 0) {
6334 msg->err_pos = chn->buf->i + ret;
6335 if (msg->err_pos < 0)
6336 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006337 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006338 }
6339
6340 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006341 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006342 msg->chunk_len = chunk;
6343 msg->body_len += chunk;
6344
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006345 if (msg->chunk_len) {
6346 msg->msg_state = HTTP_MSG_DATA;
6347 goto switch_states;
6348 }
6349 msg->msg_state = HTTP_MSG_TRAILERS;
6350 /* fall through for HTTP_MSG_TRAILERS */
6351
6352 case HTTP_MSG_TRAILERS:
6353 ret = http_forward_trailers(msg);
6354 if (ret < 0)
6355 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006356 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6357 /* default_ret */ 1,
6358 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006359 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006360 if (!ret)
6361 goto missing_data_or_waiting;
6362 break;
6363
6364 case HTTP_MSG_ENDING:
6365 goto ending;
6366
6367 default:
6368 /* This should no happen in this function */
6369 goto error;
6370 }
6371
6372 msg->msg_state = HTTP_MSG_ENDING;
6373 ending:
6374 /* we may have some pending data starting at res->buf->p such as a last
6375 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006376 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006377 /* default_ret */ msg->next,
6378 /* on_error */ goto error);
6379 b_adv(chn->buf, ret);
6380 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006381 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6382 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006383 if (msg->next)
6384 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006385
Christopher Fauletda02e172015-12-04 09:25:05 +01006386 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006387 /* default_ret */ 1,
6388 /* on_error */ goto error,
6389 /* on_wait */ goto waiting);
6390 msg->msg_state = HTTP_MSG_DONE;
6391 return 1;
6392
6393 missing_data_or_waiting:
6394 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006395 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006396 /* default_ret */ msg->next,
6397 /* on_error */ goto error);
6398 b_adv(chn->buf, ret);
6399 msg->next -= ret;
6400 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6401 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006402 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006403 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006404 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006405 return 0;
6406
6407 chunk_parsing_error:
6408 if (msg->err_pos >= 0) {
6409 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006410 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006411 msg->msg_state, strm_fe(s));
6412 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006413 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006414 msg, msg->msg_state, s->be);
6415 }
6416 error:
6417 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006418}
6419
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006421/* Iterate the same filter through all request headers.
6422 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423 * Since it can manage the switch to another backend, it updates the per-proxy
6424 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006426int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006427{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006428 char *cur_ptr, *cur_end, *cur_next;
6429 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006430 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006431 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006432 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006434 last_hdr = 0;
6435
Willy Tarreau9b28e032012-10-12 23:49:43 +02006436 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006437 old_idx = 0;
6438
6439 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006440 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006442 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006443 (exp->action == ACT_ALLOW ||
6444 exp->action == ACT_DENY ||
6445 exp->action == ACT_TARPIT))
6446 return 0;
6447
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006448 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 if (!cur_idx)
6450 break;
6451
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006452 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006453 cur_ptr = cur_next;
6454 cur_end = cur_ptr + cur_hdr->len;
6455 cur_next = cur_end + cur_hdr->cr + 1;
6456
6457 /* Now we have one header between cur_ptr and cur_end,
6458 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006459 */
6460
Willy Tarreau15a53a42015-01-21 13:39:42 +01006461 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006462 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006464 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006465 last_hdr = 1;
6466 break;
6467
6468 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006469 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006470 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006471 break;
6472
6473 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006474 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 break;
6477
6478 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006479 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6480 if (trash.len < 0)
6481 return -1;
6482
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006483 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006484 /* FIXME: if the user adds a newline in the replacement, the
6485 * index will not be recalculated for now, and the new line
6486 * will not be counted as a new header.
6487 */
6488
6489 cur_end += delta;
6490 cur_next += delta;
6491 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006492 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493 break;
6494
6495 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006496 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 cur_next += delta;
6498
Willy Tarreaufa355d42009-11-29 18:12:29 +01006499 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006500 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6501 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 cur_hdr->len = 0;
6503 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006504 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 break;
6506
6507 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006508 }
6509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006510 /* keep the link from this header to next one in case of later
6511 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006512 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006513 old_idx = cur_idx;
6514 }
6515 return 0;
6516}
6517
6518
6519/* Apply the filter to the request line.
6520 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6521 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006522 * Since it can manage the switch to another backend, it updates the per-proxy
6523 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006525int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006527 char *cur_ptr, *cur_end;
6528 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006529 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006530 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006531
Willy Tarreau3d300592007-03-18 18:34:41 +01006532 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006534 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006535 (exp->action == ACT_ALLOW ||
6536 exp->action == ACT_DENY ||
6537 exp->action == ACT_TARPIT))
6538 return 0;
6539 else if (exp->action == ACT_REMOVE)
6540 return 0;
6541
6542 done = 0;
6543
Willy Tarreau9b28e032012-10-12 23:49:43 +02006544 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006545 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006546
6547 /* Now we have the request line between cur_ptr and cur_end */
6548
Willy Tarreau15a53a42015-01-21 13:39:42 +01006549 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006551 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006552 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006553 done = 1;
6554 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006557 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006558 done = 1;
6559 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006562 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 done = 1;
6564 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006567 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6568 if (trash.len < 0)
6569 return -1;
6570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006571 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006572 /* FIXME: if the user adds a newline in the replacement, the
6573 * index will not be recalculated for now, and the new line
6574 * will not be counted as a new header.
6575 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006576
Willy Tarreaufa355d42009-11-29 18:12:29 +01006577 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006578 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006579 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580 HTTP_MSG_RQMETH,
6581 cur_ptr, cur_end + 1,
6582 NULL, NULL);
6583 if (unlikely(!cur_end))
6584 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006586 /* we have a full request and we know that we have either a CR
6587 * or an LF at <ptr>.
6588 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006589 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6590 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006591 /* there is no point trying this regex on headers */
6592 return 1;
6593 }
6594 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 return done;
6596}
Willy Tarreau97de6242006-12-27 17:18:38 +01006597
Willy Tarreau58f10d72006-12-04 02:26:12 +01006598
Willy Tarreau58f10d72006-12-04 02:26:12 +01006599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006600/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006601 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006602 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603 * unparsable request. Since it can manage the switch to another backend, it
6604 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006605 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006606int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006607{
Willy Tarreau192252e2015-04-04 01:47:55 +02006608 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006609 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006610 struct hdr_exp *exp;
6611
6612 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006613 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006615 /*
6616 * The interleaving of transformations and verdicts
6617 * makes it difficult to decide to continue or stop
6618 * the evaluation.
6619 */
6620
Willy Tarreau6c123b12010-01-28 20:22:06 +01006621 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6622 break;
6623
Willy Tarreau3d300592007-03-18 18:34:41 +01006624 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006625 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006626 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006627 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006628
6629 /* if this filter had a condition, evaluate it now and skip to
6630 * next filter if the condition does not match.
6631 */
6632 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006633 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006634 ret = acl_pass(ret);
6635 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6636 ret = !ret;
6637
6638 if (!ret)
6639 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006640 }
6641
6642 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006643 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006644 if (unlikely(ret < 0))
6645 return -1;
6646
6647 if (likely(ret == 0)) {
6648 /* The filter did not match the request, it can be
6649 * iterated through all headers.
6650 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006651 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6652 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006653 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006655 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656}
6657
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006658
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006659/* Find the end of a cookie value contained between <s> and <e>. It works the
6660 * same way as with headers above except that the semi-colon also ends a token.
6661 * See RFC2965 for more information. Note that it requires a valid header to
6662 * return a valid result.
6663 */
6664char *find_cookie_value_end(char *s, const char *e)
6665{
6666 int quoted, qdpair;
6667
6668 quoted = qdpair = 0;
6669 for (; s < e; s++) {
6670 if (qdpair) qdpair = 0;
6671 else if (quoted) {
6672 if (*s == '\\') qdpair = 1;
6673 else if (*s == '"') quoted = 0;
6674 }
6675 else if (*s == '"') quoted = 1;
6676 else if (*s == ',' || *s == ';') return s;
6677 }
6678 return s;
6679}
6680
6681/* Delete a value in a header between delimiters <from> and <next> in buffer
6682 * <buf>. The number of characters displaced is returned, and the pointer to
6683 * the first delimiter is updated if required. The function tries as much as
6684 * possible to respect the following principles :
6685 * - replace <from> delimiter by the <next> one unless <from> points to a
6686 * colon, in which case <next> is simply removed
6687 * - set exactly one space character after the new first delimiter, unless
6688 * there are not enough characters in the block being moved to do so.
6689 * - remove unneeded spaces before the previous delimiter and after the new
6690 * one.
6691 *
6692 * It is the caller's responsibility to ensure that :
6693 * - <from> points to a valid delimiter or the colon ;
6694 * - <next> points to a valid delimiter or the final CR/LF ;
6695 * - there are non-space chars before <from> ;
6696 * - there is a CR/LF at or after <next>.
6697 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006698int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006699{
6700 char *prev = *from;
6701
6702 if (*prev == ':') {
6703 /* We're removing the first value, preserve the colon and add a
6704 * space if possible.
6705 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006706 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006707 next++;
6708 prev++;
6709 if (prev < next)
6710 *prev++ = ' ';
6711
Willy Tarreau2235b262016-11-05 15:50:20 +01006712 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006713 next++;
6714 } else {
6715 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006716 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006717 prev--;
6718 *from = prev;
6719
6720 /* copy the delimiter and if possible a space if we're
6721 * not at the end of the line.
6722 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006723 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006724 *prev++ = *next++;
6725 if (prev + 1 < next)
6726 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006727 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 next++;
6729 }
6730 }
6731 return buffer_replace2(buf, prev, next, NULL, 0);
6732}
6733
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006734/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006735 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006736 * desirable to call it only when needed. This code is quite complex because
6737 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6738 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006739 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006740void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006741{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006742 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006743 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006744 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006745 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006746 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6747 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006748
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006749 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006750 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006751 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006753 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006755 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006757 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006758 hdr_beg = hdr_next;
6759 hdr_end = hdr_beg + cur_hdr->len;
6760 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006761
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006762 /* We have one full header between hdr_beg and hdr_end, and the
6763 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006764 * "Cookie:" headers.
6765 */
6766
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006768 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006769 old_idx = cur_idx;
6770 continue;
6771 }
6772
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006773 del_from = NULL; /* nothing to be deleted */
6774 preserve_hdr = 0; /* assume we may kill the whole header */
6775
Willy Tarreau58f10d72006-12-04 02:26:12 +01006776 /* Now look for cookies. Conforming to RFC2109, we have to support
6777 * attributes whose name begin with a '$', and associate them with
6778 * the right cookie, if we want to delete this cookie.
6779 * So there are 3 cases for each cookie read :
6780 * 1) it's a special attribute, beginning with a '$' : ignore it.
6781 * 2) it's a server id cookie that we *MAY* want to delete : save
6782 * some pointers on it (last semi-colon, beginning of cookie...)
6783 * 3) it's an application cookie : we *MAY* have to delete a previous
6784 * "special" cookie.
6785 * At the end of loop, if a "special" cookie remains, we may have to
6786 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006787 * *MUST* delete it.
6788 *
6789 * Note: RFC2965 is unclear about the processing of spaces around
6790 * the equal sign in the ATTR=VALUE form. A careful inspection of
6791 * the RFC explicitly allows spaces before it, and not within the
6792 * tokens (attrs or values). An inspection of RFC2109 allows that
6793 * too but section 10.1.3 lets one think that spaces may be allowed
6794 * after the equal sign too, resulting in some (rare) buggy
6795 * implementations trying to do that. So let's do what servers do.
6796 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6797 * allowed quoted strings in values, with any possible character
6798 * after a backslash, including control chars and delimitors, which
6799 * causes parsing to become ambiguous. Browsers also allow spaces
6800 * within values even without quotes.
6801 *
6802 * We have to keep multiple pointers in order to support cookie
6803 * removal at the beginning, middle or end of header without
6804 * corrupting the header. All of these headers are valid :
6805 *
6806 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6807 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6808 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6809 * | | | | | | | | |
6810 * | | | | | | | | hdr_end <--+
6811 * | | | | | | | +--> next
6812 * | | | | | | +----> val_end
6813 * | | | | | +-----------> val_beg
6814 * | | | | +--------------> equal
6815 * | | | +----------------> att_end
6816 * | | +---------------------> att_beg
6817 * | +--------------------------> prev
6818 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006819 */
6820
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006821 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6822 /* Iterate through all cookies on this line */
6823
6824 /* find att_beg */
6825 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006826 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006827 att_beg++;
6828
6829 /* find att_end : this is the first character after the last non
6830 * space before the equal. It may be equal to hdr_end.
6831 */
6832 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006833
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834 while (equal < hdr_end) {
6835 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006836 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006837 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006838 continue;
6839 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006840 }
6841
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006842 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6843 * is between <att_beg> and <equal>, both may be identical.
6844 */
6845
6846 /* look for end of cookie if there is an equal sign */
6847 if (equal < hdr_end && *equal == '=') {
6848 /* look for the beginning of the value */
6849 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006850 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006851 val_beg++;
6852
6853 /* find the end of the value, respecting quotes */
6854 next = find_cookie_value_end(val_beg, hdr_end);
6855
6856 /* make val_end point to the first white space or delimitor after the value */
6857 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006858 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006859 val_end--;
6860 } else {
6861 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006862 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006863
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006864 /* We have nothing to do with attributes beginning with '$'. However,
6865 * they will automatically be removed if a header before them is removed,
6866 * since they're supposed to be linked together.
6867 */
6868 if (*att_beg == '$')
6869 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006870
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 /* Ignore cookies with no equal sign */
6872 if (equal == next) {
6873 /* This is not our cookie, so we must preserve it. But if we already
6874 * scheduled another cookie for removal, we cannot remove the
6875 * complete header, but we can remove the previous block itself.
6876 */
6877 preserve_hdr = 1;
6878 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006879 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006880 val_end += delta;
6881 next += delta;
6882 hdr_end += delta;
6883 hdr_next += delta;
6884 cur_hdr->len += delta;
6885 http_msg_move_end(&txn->req, delta);
6886 prev = del_from;
6887 del_from = NULL;
6888 }
6889 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006890 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006891
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006892 /* if there are spaces around the equal sign, we need to
6893 * strip them otherwise we'll get trouble for cookie captures,
6894 * or even for rewrites. Since this happens extremely rarely,
6895 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006896 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006897 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6898 int stripped_before = 0;
6899 int stripped_after = 0;
6900
6901 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006902 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 equal += stripped_before;
6904 val_beg += stripped_before;
6905 }
6906
6907 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006908 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 val_beg += stripped_after;
6910 stripped_before += stripped_after;
6911 }
6912
6913 val_end += stripped_before;
6914 next += stripped_before;
6915 hdr_end += stripped_before;
6916 hdr_next += stripped_before;
6917 cur_hdr->len += stripped_before;
6918 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006919 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006922 /* First, let's see if we want to capture this cookie. We check
6923 * that we don't already have a client side cookie, because we
6924 * can only capture one. Also as an optimisation, we ignore
6925 * cookies shorter than the declared name.
6926 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006927 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6928 (val_end - att_beg >= sess->fe->capture_namelen) &&
6929 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006930 int log_len = val_end - att_beg;
6931
Willy Tarreaubafbe012017-11-24 17:34:44 +01006932 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006933 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006934 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006935 if (log_len > sess->fe->capture_len)
6936 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 memcpy(txn->cli_cookie, att_beg, log_len);
6938 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006939 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006940 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006941
Willy Tarreaubca99692010-10-06 19:25:55 +02006942 /* Persistence cookies in passive, rewrite or insert mode have the
6943 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006945 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006947 * For cookies in prefix mode, the form is :
6948 *
6949 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006951 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6952 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6953 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006954 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006955
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6957 * have the server ID between val_beg and delim, and the original cookie between
6958 * delim+1 and val_end. Otherwise, delim==val_end :
6959 *
6960 * Cookie: NAME=SRV; # in all but prefix modes
6961 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6962 * | || || | |+-> next
6963 * | || || | +--> val_end
6964 * | || || +---------> delim
6965 * | || |+------------> val_beg
6966 * | || +-------------> att_end = equal
6967 * | |+-----------------> att_beg
6968 * | +------------------> prev
6969 * +-------------------------> hdr_beg
6970 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006971
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006972 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006973 for (delim = val_beg; delim < val_end; delim++)
6974 if (*delim == COOKIE_DELIM)
6975 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006976 } else {
6977 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006978 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006979 /* Now check if the cookie contains a date field, which would
6980 * appear after a vertical bar ('|') just after the server name
6981 * and before the delimiter.
6982 */
6983 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6984 if (vbar1) {
6985 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006986 * right is the last seen date. It is a base64 encoded
6987 * 30-bit value representing the UNIX date since the
6988 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006989 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006990 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006991 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006992 if (val_end - vbar1 >= 5) {
6993 val = b64tos30(vbar1);
6994 if (val > 0)
6995 txn->cookie_last_date = val << 2;
6996 }
6997 /* look for a second vertical bar */
6998 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6999 if (vbar1 && (val_end - vbar1 > 5)) {
7000 val = b64tos30(vbar1 + 1);
7001 if (val > 0)
7002 txn->cookie_first_date = val << 2;
7003 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007004 }
7005 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007006
Willy Tarreauf64d1412010-10-07 20:06:11 +02007007 /* if the cookie has an expiration date and the proxy wants to check
7008 * it, then we do that now. We first check if the cookie is too old,
7009 * then only if it has expired. We detect strict overflow because the
7010 * time resolution here is not great (4 seconds). Cookies with dates
7011 * in the future are ignored if their offset is beyond one day. This
7012 * allows an admin to fix timezone issues without expiring everyone
7013 * and at the same time avoids keeping unwanted side effects for too
7014 * long.
7015 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007016 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7017 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007018 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007019 txn->flags &= ~TX_CK_MASK;
7020 txn->flags |= TX_CK_OLD;
7021 delim = val_beg; // let's pretend we have not found the cookie
7022 txn->cookie_first_date = 0;
7023 txn->cookie_last_date = 0;
7024 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007025 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7026 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007027 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007028 txn->flags &= ~TX_CK_MASK;
7029 txn->flags |= TX_CK_EXPIRED;
7030 delim = val_beg; // let's pretend we have not found the cookie
7031 txn->cookie_first_date = 0;
7032 txn->cookie_last_date = 0;
7033 }
7034
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007035 /* Here, we'll look for the first running server which supports the cookie.
7036 * This allows to share a same cookie between several servers, for example
7037 * to dedicate backup servers to specific servers only.
7038 * However, to prevent clients from sticking to cookie-less backup server
7039 * when they have incidentely learned an empty cookie, we simply ignore
7040 * empty cookies and mark them as invalid.
7041 * The same behaviour is applied when persistence must be ignored.
7042 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007043 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007044 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 while (srv) {
7047 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7048 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007049 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007050 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007051 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007052 /* we found the server and we can use it */
7053 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007054 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007055 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007056 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007057 break;
7058 } else {
7059 /* we found a server, but it's down,
7060 * mark it as such and go on in case
7061 * another one is available.
7062 */
7063 txn->flags &= ~TX_CK_MASK;
7064 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007067 srv = srv->next;
7068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069
Willy Tarreauf64d1412010-10-07 20:06:11 +02007070 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007071 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007073 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007074 txn->flags |= TX_CK_UNUSED;
7075 else
7076 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007077 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007078
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007079 /* depending on the cookie mode, we may have to either :
7080 * - delete the complete cookie if we're in insert+indirect mode, so that
7081 * the server never sees it ;
7082 * - remove the server id from the cookie value, and tag the cookie as an
7083 * application cookie so that it does not get accidentely removed later,
7084 * if we're in cookie prefix mode
7085 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007086 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007088
Willy Tarreau9b28e032012-10-12 23:49:43 +02007089 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007090 val_end += delta;
7091 next += delta;
7092 hdr_end += delta;
7093 hdr_next += delta;
7094 cur_hdr->len += delta;
7095 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 del_from = NULL;
7098 preserve_hdr = 1; /* we want to keep this cookie */
7099 }
7100 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007101 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007102 del_from = prev;
7103 }
7104 } else {
7105 /* This is not our cookie, so we must preserve it. But if we already
7106 * scheduled another cookie for removal, we cannot remove the
7107 * complete header, but we can remove the previous block itself.
7108 */
7109 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007111 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007112 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007113 if (att_beg >= del_from)
7114 att_beg += delta;
7115 if (att_end >= del_from)
7116 att_end += delta;
7117 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007118 val_end += delta;
7119 next += delta;
7120 hdr_end += delta;
7121 hdr_next += delta;
7122 cur_hdr->len += delta;
7123 http_msg_move_end(&txn->req, delta);
7124 prev = del_from;
7125 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007126 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007127 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007128
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007129 /* continue with next cookie on this header line */
7130 att_beg = next;
7131 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 /* There are no more cookies on this line.
7134 * We may still have one (or several) marked for deletion at the
7135 * end of the line. We must do this now in two ways :
7136 * - if some cookies must be preserved, we only delete from the
7137 * mark to the end of line ;
7138 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007139 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007140 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007141 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007142 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007143 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007144 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145 cur_hdr->len += delta;
7146 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007147 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007148
7149 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007150 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7151 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007153 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007155 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007156 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007157 }
7158
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007159 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007161 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162}
7163
7164
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165/* Iterate the same filter through all response headers contained in <rtr>.
7166 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7167 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007168int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170 char *cur_ptr, *cur_end, *cur_next;
7171 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007172 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007174 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175
7176 last_hdr = 0;
7177
Willy Tarreau9b28e032012-10-12 23:49:43 +02007178 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 old_idx = 0;
7180
7181 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007182 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007184 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 (exp->action == ACT_ALLOW ||
7186 exp->action == ACT_DENY))
7187 return 0;
7188
7189 cur_idx = txn->hdr_idx.v[old_idx].next;
7190 if (!cur_idx)
7191 break;
7192
7193 cur_hdr = &txn->hdr_idx.v[cur_idx];
7194 cur_ptr = cur_next;
7195 cur_end = cur_ptr + cur_hdr->len;
7196 cur_next = cur_end + cur_hdr->cr + 1;
7197
7198 /* Now we have one header between cur_ptr and cur_end,
7199 * and the next header starts at cur_next.
7200 */
7201
Willy Tarreau15a53a42015-01-21 13:39:42 +01007202 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 switch (exp->action) {
7204 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007205 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 last_hdr = 1;
7207 break;
7208
7209 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007210 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 last_hdr = 1;
7212 break;
7213
7214 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007215 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7216 if (trash.len < 0)
7217 return -1;
7218
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007219 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 /* FIXME: if the user adds a newline in the replacement, the
7221 * index will not be recalculated for now, and the new line
7222 * will not be counted as a new header.
7223 */
7224
7225 cur_end += delta;
7226 cur_next += delta;
7227 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007228 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 break;
7230
7231 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007232 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 cur_next += delta;
7234
Willy Tarreaufa355d42009-11-29 18:12:29 +01007235 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7237 txn->hdr_idx.used--;
7238 cur_hdr->len = 0;
7239 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007240 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241 break;
7242
7243 }
7244 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245
7246 /* keep the link from this header to next one in case of later
7247 * removal of next header.
7248 */
7249 old_idx = cur_idx;
7250 }
7251 return 0;
7252}
7253
7254
7255/* Apply the filter to the status line in the response buffer <rtr>.
7256 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7257 * or -1 if a replacement resulted in an invalid status line.
7258 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007259int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007261 char *cur_ptr, *cur_end;
7262 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007263 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007264 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265
7266
Willy Tarreau3d300592007-03-18 18:34:41 +01007267 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007269 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270 (exp->action == ACT_ALLOW ||
7271 exp->action == ACT_DENY))
7272 return 0;
7273 else if (exp->action == ACT_REMOVE)
7274 return 0;
7275
7276 done = 0;
7277
Willy Tarreau9b28e032012-10-12 23:49:43 +02007278 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007279 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280
7281 /* Now we have the status line between cur_ptr and cur_end */
7282
Willy Tarreau15a53a42015-01-21 13:39:42 +01007283 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 switch (exp->action) {
7285 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007286 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287 done = 1;
7288 break;
7289
7290 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007291 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007292 done = 1;
7293 break;
7294
7295 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007296 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7297 if (trash.len < 0)
7298 return -1;
7299
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007300 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 /* FIXME: if the user adds a newline in the replacement, the
7302 * index will not be recalculated for now, and the new line
7303 * will not be counted as a new header.
7304 */
7305
Willy Tarreaufa355d42009-11-29 18:12:29 +01007306 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007308 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007309 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310 cur_ptr, cur_end + 1,
7311 NULL, NULL);
7312 if (unlikely(!cur_end))
7313 return -1;
7314
7315 /* we have a full respnse and we know that we have either a CR
7316 * or an LF at <ptr>.
7317 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007318 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007319 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320 /* there is no point trying this regex on headers */
7321 return 1;
7322 }
7323 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 return done;
7325}
7326
7327
7328
7329/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007330 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7332 * unparsable response.
7333 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007334int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335{
Willy Tarreau192252e2015-04-04 01:47:55 +02007336 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007337 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007338 struct hdr_exp *exp;
7339
7340 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341 int ret;
7342
7343 /*
7344 * The interleaving of transformations and verdicts
7345 * makes it difficult to decide to continue or stop
7346 * the evaluation.
7347 */
7348
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007349 if (txn->flags & TX_SVDENY)
7350 break;
7351
Willy Tarreau3d300592007-03-18 18:34:41 +01007352 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7354 exp->action == ACT_PASS)) {
7355 exp = exp->next;
7356 continue;
7357 }
7358
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007359 /* if this filter had a condition, evaluate it now and skip to
7360 * next filter if the condition does not match.
7361 */
7362 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007363 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007364 ret = acl_pass(ret);
7365 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7366 ret = !ret;
7367 if (!ret)
7368 continue;
7369 }
7370
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007372 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007373 if (unlikely(ret < 0))
7374 return -1;
7375
7376 if (likely(ret == 0)) {
7377 /* The filter did not match the response, it can be
7378 * iterated through all headers.
7379 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007380 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7381 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007382 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007383 }
7384 return 0;
7385}
7386
7387
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007389 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007390 * desirable to call it only when needed. This function is also used when we
7391 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007393void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007395 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007396 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007397 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007398 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007399 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007400 char *hdr_beg, *hdr_end, *hdr_next;
7401 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007402
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 /* Iterate through the headers.
7404 * we start with the start line.
7405 */
7406 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007407 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408
7409 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7410 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007411 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412
7413 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007414 hdr_beg = hdr_next;
7415 hdr_end = hdr_beg + cur_hdr->len;
7416 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417
Willy Tarreau24581ba2010-08-31 22:39:35 +02007418 /* We have one full header between hdr_beg and hdr_end, and the
7419 * next header starts at hdr_next. We're only interested in
7420 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421 */
7422
Willy Tarreau24581ba2010-08-31 22:39:35 +02007423 is_cookie2 = 0;
7424 prev = hdr_beg + 10;
7425 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007426 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007427 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7428 if (!val) {
7429 old_idx = cur_idx;
7430 continue;
7431 }
7432 is_cookie2 = 1;
7433 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007434 }
7435
Willy Tarreau24581ba2010-08-31 22:39:35 +02007436 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7437 * <prev> points to the colon.
7438 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007439 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440
Willy Tarreau24581ba2010-08-31 22:39:35 +02007441 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7442 * check-cache is enabled) and we are not interested in checking
7443 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007444 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007445 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007446 return;
7447
Willy Tarreau24581ba2010-08-31 22:39:35 +02007448 /* OK so now we know we have to process this response cookie.
7449 * The format of the Set-Cookie header is slightly different
7450 * from the format of the Cookie header in that it does not
7451 * support the comma as a cookie delimiter (thus the header
7452 * cannot be folded) because the Expires attribute described in
7453 * the original Netscape's spec may contain an unquoted date
7454 * with a comma inside. We have to live with this because
7455 * many browsers don't support Max-Age and some browsers don't
7456 * support quoted strings. However the Set-Cookie2 header is
7457 * clean.
7458 *
7459 * We have to keep multiple pointers in order to support cookie
7460 * removal at the beginning, middle or end of header without
7461 * corrupting the header (in case of set-cookie2). A special
7462 * pointer, <scav> points to the beginning of the set-cookie-av
7463 * fields after the first semi-colon. The <next> pointer points
7464 * either to the end of line (set-cookie) or next unquoted comma
7465 * (set-cookie2). All of these headers are valid :
7466 *
7467 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7468 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7469 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7470 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7471 * | | | | | | | | | |
7472 * | | | | | | | | +-> next hdr_end <--+
7473 * | | | | | | | +------------> scav
7474 * | | | | | | +--------------> val_end
7475 * | | | | | +--------------------> val_beg
7476 * | | | | +----------------------> equal
7477 * | | | +------------------------> att_end
7478 * | | +----------------------------> att_beg
7479 * | +------------------------------> prev
7480 * +-----------------------------------------> hdr_beg
7481 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007482
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 for (; prev < hdr_end; prev = next) {
7484 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485
Willy Tarreau24581ba2010-08-31 22:39:35 +02007486 /* find att_beg */
7487 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007488 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007489 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490
Willy Tarreau24581ba2010-08-31 22:39:35 +02007491 /* find att_end : this is the first character after the last non
7492 * space before the equal. It may be equal to hdr_end.
7493 */
7494 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007495
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 while (equal < hdr_end) {
7497 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7498 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007499 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007500 continue;
7501 att_end = equal;
7502 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007503
Willy Tarreau24581ba2010-08-31 22:39:35 +02007504 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7505 * is between <att_beg> and <equal>, both may be identical.
7506 */
7507
7508 /* look for end of cookie if there is an equal sign */
7509 if (equal < hdr_end && *equal == '=') {
7510 /* look for the beginning of the value */
7511 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007512 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007513 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007514
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 /* find the end of the value, respecting quotes */
7516 next = find_cookie_value_end(val_beg, hdr_end);
7517
7518 /* make val_end point to the first white space or delimitor after the value */
7519 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007520 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007521 val_end--;
7522 } else {
7523 /* <equal> points to next comma, semi-colon or EOL */
7524 val_beg = val_end = next = equal;
7525 }
7526
7527 if (next < hdr_end) {
7528 /* Set-Cookie2 supports multiple cookies, and <next> points to
7529 * a colon or semi-colon before the end. So skip all attr-value
7530 * pairs and look for the next comma. For Set-Cookie, since
7531 * commas are permitted in values, skip to the end.
7532 */
7533 if (is_cookie2)
7534 next = find_hdr_value_end(next, hdr_end);
7535 else
7536 next = hdr_end;
7537 }
7538
7539 /* Now everything is as on the diagram above */
7540
7541 /* Ignore cookies with no equal sign */
7542 if (equal == val_end)
7543 continue;
7544
7545 /* If there are spaces around the equal sign, we need to
7546 * strip them otherwise we'll get trouble for cookie captures,
7547 * or even for rewrites. Since this happens extremely rarely,
7548 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007550 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7551 int stripped_before = 0;
7552 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007553
Willy Tarreau24581ba2010-08-31 22:39:35 +02007554 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007555 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007556 equal += stripped_before;
7557 val_beg += stripped_before;
7558 }
7559
7560 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007561 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007562 val_beg += stripped_after;
7563 stripped_before += stripped_after;
7564 }
7565
7566 val_end += stripped_before;
7567 next += stripped_before;
7568 hdr_end += stripped_before;
7569 hdr_next += stripped_before;
7570 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007571 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007572 }
7573
7574 /* First, let's see if we want to capture this cookie. We check
7575 * that we don't already have a server side cookie, because we
7576 * can only capture one. Also as an optimisation, we ignore
7577 * cookies shorter than the declared name.
7578 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007579 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007580 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007581 (val_end - att_beg >= sess->fe->capture_namelen) &&
7582 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007583 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007584 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007585 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007586 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007587 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007588 if (log_len > sess->fe->capture_len)
7589 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007590 memcpy(txn->srv_cookie, att_beg, log_len);
7591 txn->srv_cookie[log_len] = 0;
7592 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007593 }
7594
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007595 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007596 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007597 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007598 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7599 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007600 /* assume passive cookie by default */
7601 txn->flags &= ~TX_SCK_MASK;
7602 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603
7604 /* If the cookie is in insert mode on a known server, we'll delete
7605 * this occurrence because we'll insert another one later.
7606 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007607 * a direct access.
7608 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007609 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007610 /* The "preserve" flag was set, we don't want to touch the
7611 * server's cookie.
7612 */
7613 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007614 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007615 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007616 /* this cookie must be deleted */
7617 if (*prev == ':' && next == hdr_end) {
7618 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007619 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007620 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7621 txn->hdr_idx.used--;
7622 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007623 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007624 hdr_next += delta;
7625 http_msg_move_end(&txn->rsp, delta);
7626 /* note: while both invalid now, <next> and <hdr_end>
7627 * are still equal, so the for() will stop as expected.
7628 */
7629 } else {
7630 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007631 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007632 next = prev;
7633 hdr_end += delta;
7634 hdr_next += delta;
7635 cur_hdr->len += delta;
7636 http_msg_move_end(&txn->rsp, delta);
7637 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007638 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007639 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007640 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007641 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007642 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007643 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007644 * with this server since we know it.
7645 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007646 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007647 next += delta;
7648 hdr_end += delta;
7649 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007651 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007652
Willy Tarreauf1348312010-10-07 15:54:11 +02007653 txn->flags &= ~TX_SCK_MASK;
7654 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007656 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007658 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007660 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007661 next += delta;
7662 hdr_end += delta;
7663 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007665 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007666
Willy Tarreau827aee92011-03-10 16:55:02 +01007667 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007668 txn->flags &= ~TX_SCK_MASK;
7669 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 }
7671 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007672 /* that's done for this cookie, check the next one on the same
7673 * line when next != hdr_end (only if is_cookie2).
7674 */
7675 }
7676 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007678 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679}
7680
7681
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007683 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007684 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007685void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007687 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688 char *p1, *p2;
7689
7690 char *cur_ptr, *cur_end, *cur_next;
7691 int cur_idx;
7692
Willy Tarreau5df51872007-11-25 16:20:08 +01007693 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694 return;
7695
7696 /* Iterate through the headers.
7697 * we start with the start line.
7698 */
7699 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007700 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701
7702 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7703 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007704 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705
7706 cur_hdr = &txn->hdr_idx.v[cur_idx];
7707 cur_ptr = cur_next;
7708 cur_end = cur_ptr + cur_hdr->len;
7709 cur_next = cur_end + cur_hdr->cr + 1;
7710
7711 /* We have one full header between cur_ptr and cur_end, and the
7712 * next header starts at cur_next. We're only interested in
7713 * "Cookie:" headers.
7714 */
7715
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007716 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7717 if (val) {
7718 if ((cur_end - (cur_ptr + val) >= 8) &&
7719 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7720 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7721 return;
7722 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723 }
7724
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007725 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7726 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007727 continue;
7728
7729 /* OK, right now we know we have a cache-control header at cur_ptr */
7730
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007731 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007732
7733 if (p1 >= cur_end) /* no more info */
7734 continue;
7735
7736 /* p1 is at the beginning of the value */
7737 p2 = p1;
7738
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007739 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007740 p2++;
7741
7742 /* we have a complete value between p1 and p2 */
7743 if (p2 < cur_end && *p2 == '=') {
7744 /* we have something of the form no-cache="set-cookie" */
7745 if ((cur_end - p1 >= 21) &&
7746 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7747 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007748 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007749 continue;
7750 }
7751
7752 /* OK, so we know that either p2 points to the end of string or to a comma */
7753 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007754 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007755 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7756 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7757 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007758 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759 return;
7760 }
7761
7762 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007763 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007764 continue;
7765 }
7766 }
7767}
7768
Willy Tarreau58f10d72006-12-04 02:26:12 +01007769
Willy Tarreaub2513902006-12-17 14:52:38 +01007770/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007771 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007772 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007773 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007774 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007775 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007776 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007777 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007778 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007779int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007780{
7781 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007782 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007783 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007784
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007785 if (!uri_auth)
7786 return 0;
7787
Cyril Bonté70be45d2010-10-12 00:14:35 +02007788 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007789 return 0;
7790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007791 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007792 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007793 return 0;
7794
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007795 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007796 return 0;
7797
Willy Tarreaub2513902006-12-17 14:52:38 +01007798 return 1;
7799}
7800
Willy Tarreau4076a152009-04-02 15:18:36 +02007801/*
7802 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007803 * By default it tries to report the error position as msg->err_pos. However if
7804 * this one is not set, it will then report msg->next, which is the last known
7805 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007806 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007807 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007808void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007809 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007810 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007811{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007812 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007813 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007814 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007815
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007816 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007817 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007818 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007819 len1 = MIN(len1, es->len);
7820 len2 = es->len - len1; /* remaining data if buffer wraps */
7821
Willy Tarreauf3764b72016-03-31 13:45:10 +02007822 if (!es->buf)
7823 es->buf = malloc(global.tune.bufsize);
7824
7825 if (es->buf) {
7826 memcpy(es->buf, chn->buf->p, len1);
7827 if (len2)
7828 memcpy(es->buf + len1, chn->buf->data, len2);
7829 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007830
Willy Tarreau4076a152009-04-02 15:18:36 +02007831 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007832 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007833 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007834 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007835
Willy Tarreau4076a152009-04-02 15:18:36 +02007836 es->when = date; // user-visible date
7837 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007838 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007839 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007840 if (objt_conn(sess->origin))
7841 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007842 else
7843 memset(&es->src, 0, sizeof(es->src));
7844
Willy Tarreau078272e2010-12-12 12:46:33 +01007845 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007846 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007847 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007848 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007849 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007850 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007851 es->b_out = chn->buf->o;
7852 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007853 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007854 es->m_clen = msg->chunk_len;
7855 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007856 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007857}
Willy Tarreaub2513902006-12-17 14:52:38 +01007858
Willy Tarreau294c4732011-12-16 21:35:50 +01007859/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7860 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7861 * performed over the whole headers. Otherwise it must contain a valid header
7862 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7863 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7864 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7865 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007866 * -1. The value fetch stops at commas, so this function is suited for use with
7867 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007868 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007869 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007870unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007871 struct hdr_idx *idx, int occ,
7872 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007873{
Willy Tarreau294c4732011-12-16 21:35:50 +01007874 struct hdr_ctx local_ctx;
7875 char *ptr_hist[MAX_HDR_HISTORY];
7876 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007877 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007878 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007879
Willy Tarreau294c4732011-12-16 21:35:50 +01007880 if (!ctx) {
7881 local_ctx.idx = 0;
7882 ctx = &local_ctx;
7883 }
7884
Willy Tarreaubce70882009-09-07 11:51:47 +02007885 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007886 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007887 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007888 occ--;
7889 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007890 *vptr = ctx->line + ctx->val;
7891 *vlen = ctx->vlen;
7892 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007893 }
7894 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007895 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007896 }
7897
7898 /* negative occurrence, we scan all the list then walk back */
7899 if (-occ > MAX_HDR_HISTORY)
7900 return 0;
7901
Willy Tarreau294c4732011-12-16 21:35:50 +01007902 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007903 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007904 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7905 len_hist[hist_ptr] = ctx->vlen;
7906 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007907 hist_ptr = 0;
7908 found++;
7909 }
7910 if (-occ > found)
7911 return 0;
7912 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007913 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7914 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7915 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007916 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007917 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007918 if (hist_ptr >= MAX_HDR_HISTORY)
7919 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007920 *vptr = ptr_hist[hist_ptr];
7921 *vlen = len_hist[hist_ptr];
7922 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007923}
7924
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007925/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7926 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7927 * performed over the whole headers. Otherwise it must contain a valid header
7928 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7929 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7930 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7931 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7932 * -1. This function differs from http_get_hdr() in that it only returns full
7933 * line header values and does not stop at commas.
7934 * The return value is 0 if nothing was found, or non-zero otherwise.
7935 */
7936unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7937 struct hdr_idx *idx, int occ,
7938 struct hdr_ctx *ctx, char **vptr, int *vlen)
7939{
7940 struct hdr_ctx local_ctx;
7941 char *ptr_hist[MAX_HDR_HISTORY];
7942 int len_hist[MAX_HDR_HISTORY];
7943 unsigned int hist_ptr;
7944 int found;
7945
7946 if (!ctx) {
7947 local_ctx.idx = 0;
7948 ctx = &local_ctx;
7949 }
7950
7951 if (occ >= 0) {
7952 /* search from the beginning */
7953 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7954 occ--;
7955 if (occ <= 0) {
7956 *vptr = ctx->line + ctx->val;
7957 *vlen = ctx->vlen;
7958 return 1;
7959 }
7960 }
7961 return 0;
7962 }
7963
7964 /* negative occurrence, we scan all the list then walk back */
7965 if (-occ > MAX_HDR_HISTORY)
7966 return 0;
7967
7968 found = hist_ptr = 0;
7969 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7970 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7971 len_hist[hist_ptr] = ctx->vlen;
7972 if (++hist_ptr >= MAX_HDR_HISTORY)
7973 hist_ptr = 0;
7974 found++;
7975 }
7976 if (-occ > found)
7977 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007978
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007979 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007980 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7981 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7982 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007983 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007984 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007985 if (hist_ptr >= MAX_HDR_HISTORY)
7986 hist_ptr -= MAX_HDR_HISTORY;
7987 *vptr = ptr_hist[hist_ptr];
7988 *vlen = len_hist[hist_ptr];
7989 return 1;
7990}
7991
Willy Tarreaubaaee002006-06-26 02:48:02 +02007992/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007993 * Print a debug line with a header. Always stop at the first CR or LF char,
7994 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7995 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007996 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007997void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007998{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007999 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008000 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008001
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008002 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008003 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008004 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008005 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 +02008006
8007 for (max = 0; start + max < end; max++)
8008 if (start[max] == '\r' || start[max] == '\n')
8009 break;
8010
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008011 UBOUND(max, trash.size - trash.len - 3);
8012 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8013 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008014 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008015}
8016
Willy Tarreaueee5b512015-04-03 23:46:31 +02008017
8018/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8019 * The hdr_idx is allocated as well. In case of allocation failure, everything
8020 * allocated is freed and NULL is returned. Otherwise the new transaction is
8021 * assigned to the stream and returned.
8022 */
8023struct http_txn *http_alloc_txn(struct stream *s)
8024{
8025 struct http_txn *txn = s->txn;
8026
8027 if (txn)
8028 return txn;
8029
Willy Tarreaubafbe012017-11-24 17:34:44 +01008030 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008031 if (!txn)
8032 return txn;
8033
8034 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008035 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008036 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008037 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008038 return NULL;
8039 }
8040
8041 s->txn = txn;
8042 return txn;
8043}
8044
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008045void http_txn_reset_req(struct http_txn *txn)
8046{
8047 txn->req.flags = 0;
8048 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8049 txn->req.next = 0;
8050 txn->req.chunk_len = 0LL;
8051 txn->req.body_len = 0LL;
8052 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8053}
8054
8055void http_txn_reset_res(struct http_txn *txn)
8056{
8057 txn->rsp.flags = 0;
8058 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8059 txn->rsp.next = 0;
8060 txn->rsp.chunk_len = 0LL;
8061 txn->rsp.body_len = 0LL;
8062 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8063}
8064
Willy Tarreau0937bc42009-12-22 15:03:09 +01008065/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008066 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008067 * the required fields are properly allocated and that we only need to (re)init
8068 * them. This should be used before processing any new request.
8069 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008070void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008071{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008072 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008073 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008074
8075 txn->flags = 0;
8076 txn->status = -1;
8077
Willy Tarreauf64d1412010-10-07 20:06:11 +02008078 txn->cookie_first_date = 0;
8079 txn->cookie_last_date = 0;
8080
Willy Tarreaueee5b512015-04-03 23:46:31 +02008081 txn->srv_cookie = NULL;
8082 txn->cli_cookie = NULL;
8083 txn->uri = NULL;
8084
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008085 http_txn_reset_req(txn);
8086 http_txn_reset_res(txn);
8087
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008088 txn->req.chn = &s->req;
8089 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008090
8091 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008092
8093 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8094 if (fe->options2 & PR_O2_REQBUG_OK)
8095 txn->req.err_pos = -1; /* let buggy requests pass */
8096
Willy Tarreau0937bc42009-12-22 15:03:09 +01008097 if (txn->hdr_idx.v)
8098 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008099
8100 vars_init(&s->vars_txn, SCOPE_TXN);
8101 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008102}
8103
8104/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008105void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008106{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008107 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008108 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008109
8110 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008111 pool_free(pool_head_requri, txn->uri);
8112 pool_free(pool_head_capture, txn->cli_cookie);
8113 pool_free(pool_head_capture, txn->srv_cookie);
8114 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008115
William Lallemanda73203e2012-03-12 12:48:57 +01008116 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008117 txn->uri = NULL;
8118 txn->srv_cookie = NULL;
8119 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008120
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008121 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008122 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008123 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008124 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008125 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008126 }
8127
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008128 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008129 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008130 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008131 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008132 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008133 }
8134
Willy Tarreau6204cd92016-03-10 16:33:04 +01008135 vars_prune(&s->vars_txn, s->sess, s);
8136 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008137}
8138
8139/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008140void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008141{
8142 http_end_txn(s);
8143 http_init_txn(s);
8144
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008145 /* reinitialise the current rule list pointer to NULL. We are sure that
8146 * any rulelist match the NULL pointer.
8147 */
8148 s->current_rule_list = NULL;
8149
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008150 s->be = strm_fe(s);
8151 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008152 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008153 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008154 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008155 /* re-init store persistence */
8156 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008157 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008158
Willy Tarreau0937bc42009-12-22 15:03:09 +01008159 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008160
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008161 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008162
Willy Tarreau739cfba2010-01-25 23:11:14 +01008163 /* We must trim any excess data from the response buffer, because we
8164 * may have blocked an invalid response from a server that we don't
8165 * want to accidentely forward once we disable the analysers, nor do
8166 * we want those data to come along with next response. A typical
8167 * example of such data would be from a buggy server responding to
8168 * a HEAD with some data, or sending more than the advertised
8169 * content-length.
8170 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008171 if (unlikely(s->res.buf->i))
8172 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008173
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008174 /* Now we can realign the response buffer */
8175 buffer_realign(s->res.buf);
8176
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008177 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008178 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008179
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008180 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008181 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008182
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008183 s->req.rex = TICK_ETERNITY;
8184 s->req.wex = TICK_ETERNITY;
8185 s->req.analyse_exp = TICK_ETERNITY;
8186 s->res.rex = TICK_ETERNITY;
8187 s->res.wex = TICK_ETERNITY;
8188 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008189 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008190}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008191
Sasha Pachev218f0642014-06-16 12:05:59 -06008192void free_http_res_rules(struct list *r)
8193{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008194 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008195
8196 list_for_each_entry_safe(pr, tr, r, list) {
8197 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008198 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008199 free(pr);
8200 }
8201}
8202
8203void free_http_req_rules(struct list *r)
8204{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008205 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008206
8207 list_for_each_entry_safe(pr, tr, r, list) {
8208 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008209 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008210 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008211
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008212 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008213 free(pr);
8214 }
8215}
8216
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008217/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008218struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008219{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008220 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008221 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008222 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008223 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008224
Vincent Bernat02779b62016-04-03 13:48:43 +02008225 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008226 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008227 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008228 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008229 }
8230
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008231 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008232 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008233 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008234 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008235 int code;
8236 int hc;
8237
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008238 if (!strcmp(args[0], "tarpit")) {
8239 rule->action = ACT_HTTP_REQ_TARPIT;
8240 rule->deny_status = HTTP_ERR_500;
8241 }
8242 else {
8243 rule->action = ACT_ACTION_DENY;
8244 rule->deny_status = HTTP_ERR_403;
8245 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008246 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008247 if (strcmp(args[cur_arg], "deny_status") == 0) {
8248 cur_arg++;
8249 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008250 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8251 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008252 goto out_err;
8253 }
8254
8255 code = atol(args[cur_arg]);
8256 cur_arg++;
8257 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8258 if (http_err_codes[hc] == code) {
8259 rule->deny_status = hc;
8260 break;
8261 }
8262 }
8263
8264 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008265 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8266 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008267 }
8268 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008269 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008270 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008271 cur_arg = 1;
8272
8273 while(*args[cur_arg]) {
8274 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008275 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008276 cur_arg+=2;
8277 continue;
8278 } else
8279 break;
8280 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008281 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008282 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008283 cur_arg = 1;
8284
8285 if (!*args[cur_arg] ||
8286 (*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 +01008287 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8288 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008289 goto out_err;
8290 }
8291 rule->arg.nice = atoi(args[cur_arg]);
8292 if (rule->arg.nice < -1024)
8293 rule->arg.nice = -1024;
8294 else if (rule->arg.nice > 1024)
8295 rule->arg.nice = 1024;
8296 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008297 } else if (!strcmp(args[0], "set-tos")) {
8298#ifdef IP_TOS
8299 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008300 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008301 cur_arg = 1;
8302
8303 if (!*args[cur_arg] ||
8304 (*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 +01008305 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8306 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008307 goto out_err;
8308 }
8309
8310 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8311 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008312 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8313 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008314 goto out_err;
8315 }
8316 cur_arg++;
8317#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008318 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 +02008319 goto out_err;
8320#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008321 } else if (!strcmp(args[0], "set-mark")) {
8322#ifdef SO_MARK
8323 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008324 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008325 cur_arg = 1;
8326
8327 if (!*args[cur_arg] ||
8328 (*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 +01008329 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8330 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008331 goto out_err;
8332 }
8333
8334 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8335 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008336 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8337 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008338 goto out_err;
8339 }
8340 cur_arg++;
8341 global.last_checks |= LSTCHK_NETADM;
8342#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008343 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 +02008344 goto out_err;
8345#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008346 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008347 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008348 cur_arg = 1;
8349
8350 if (!*args[cur_arg] ||
8351 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8352 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008353 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8354 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008355 goto out_err;
8356 }
8357 if (strcmp(args[cur_arg], "silent") == 0)
8358 rule->arg.loglevel = -1;
8359 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8360 goto bad_log_level;
8361 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008362 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008363 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008364 cur_arg = 1;
8365
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008366 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8367 (*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 +01008368 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8369 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008370 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008371 }
8372
8373 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8374 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8375 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008376
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008377 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008378 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008379 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 +01008380 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008381 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8382 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008383 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008384 goto out_err;
8385 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008386 free(proxy->conf.lfs_file);
8387 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8388 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008389 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008390 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008391 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008392 cur_arg = 1;
8393
8394 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008395 (*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 +01008396 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8397 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008398 goto out_err;
8399 }
8400
8401 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8402 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8403 LIST_INIT(&rule->arg.hdr_add.fmt);
8404
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008405 error = NULL;
8406 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008407 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8408 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008409 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008410 goto out_err;
8411 }
8412
8413 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008414 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008415 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 +01008416 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008417 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8418 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008419 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008420 goto out_err;
8421 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008422
8423 free(proxy->conf.lfs_file);
8424 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8425 proxy->conf.lfs_line = proxy->conf.args.line;
8426 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008427 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008428 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008429 cur_arg = 1;
8430
8431 if (!*args[cur_arg] ||
8432 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008433 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8434 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008435 goto out_err;
8436 }
8437
8438 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8439 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8440
8441 proxy->conf.args.ctx = ARGC_HRQ;
8442 free(proxy->conf.lfs_file);
8443 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8444 proxy->conf.lfs_line = proxy->conf.args.line;
8445 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008446 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8447 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008448 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008449 struct sample_expr *expr;
8450 unsigned int where;
8451 char *err = NULL;
8452
8453 cur_arg = 1;
8454 proxy->conf.args.ctx = ARGC_TRK;
8455
8456 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8457 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008458 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8459 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008460 free(err);
8461 goto out_err;
8462 }
8463
8464 where = 0;
8465 if (proxy->cap & PR_CAP_FE)
8466 where |= SMP_VAL_FE_HRQ_HDR;
8467 if (proxy->cap & PR_CAP_BE)
8468 where |= SMP_VAL_BE_HRQ_HDR;
8469
8470 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008471 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8472 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8473 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8474 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008475 free(expr);
8476 goto out_err;
8477 }
8478
8479 if (strcmp(args[cur_arg], "table") == 0) {
8480 cur_arg++;
8481 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008482 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8483 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008484 free(expr);
8485 goto out_err;
8486 }
8487 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008488 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008489 cur_arg++;
8490 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008491 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008492 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02008493 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008494 } else if (strcmp(args[0], "redirect") == 0) {
8495 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008496 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008497
Willy Tarreaube4653b2015-05-28 15:26:58 +02008498 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008499 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8500 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008501 goto out_err;
8502 }
8503
8504 /* this redirect rule might already contain a parsed condition which
8505 * we'll pass to the http-request rule.
8506 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008507 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008508 rule->arg.redir = redir;
8509 rule->cond = redir->cond;
8510 redir->cond = NULL;
8511 cur_arg = 2;
8512 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008513 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8514 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008515 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008516 /*
8517 * '+ 8' for 'add-acl('
8518 * '- 9' for 'add-acl(' + trailing ')'
8519 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008520 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008521
8522 cur_arg = 1;
8523
8524 if (!*args[cur_arg] ||
8525 (*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 +01008526 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8527 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008528 goto out_err;
8529 }
8530
8531 LIST_INIT(&rule->arg.map.key);
8532 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008533 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008534 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008535 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008536 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8537 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008538 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008539 goto out_err;
8540 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008541 free(proxy->conf.lfs_file);
8542 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8543 proxy->conf.lfs_line = proxy->conf.args.line;
8544 cur_arg += 1;
8545 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8546 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008547 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008548 /*
8549 * '+ 8' for 'del-acl('
8550 * '- 9' for 'del-acl(' + trailing ')'
8551 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008552 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008553
8554 cur_arg = 1;
8555
8556 if (!*args[cur_arg] ||
8557 (*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 +01008558 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8559 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008560 goto out_err;
8561 }
8562
8563 LIST_INIT(&rule->arg.map.key);
8564 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008565 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008566 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008567 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008568 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8569 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008570 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008571 goto out_err;
8572 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008573 free(proxy->conf.lfs_file);
8574 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8575 proxy->conf.lfs_line = proxy->conf.args.line;
8576 cur_arg += 1;
8577 } else if (strncmp(args[0], "del-map", 7) == 0) {
8578 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008579 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008580 /*
8581 * '+ 8' for 'del-map('
8582 * '- 9' for 'del-map(' + trailing ')'
8583 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008584 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008585
8586 cur_arg = 1;
8587
8588 if (!*args[cur_arg] ||
8589 (*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 +01008590 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8591 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008592 goto out_err;
8593 }
8594
8595 LIST_INIT(&rule->arg.map.key);
8596 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008597 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008598 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008599 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008600 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8601 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008602 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008603 goto out_err;
8604 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008605 free(proxy->conf.lfs_file);
8606 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8607 proxy->conf.lfs_line = proxy->conf.args.line;
8608 cur_arg += 1;
8609 } else if (strncmp(args[0], "set-map", 7) == 0) {
8610 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008611 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008612 /*
8613 * '+ 8' for 'set-map('
8614 * '- 9' for 'set-map(' + trailing ')'
8615 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008616 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008617
8618 cur_arg = 1;
8619
8620 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8621 (*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 +01008622 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8623 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008624 goto out_err;
8625 }
8626
8627 LIST_INIT(&rule->arg.map.key);
8628 LIST_INIT(&rule->arg.map.value);
8629 proxy->conf.args.ctx = ARGC_HRQ;
8630
8631 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008632 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008633 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008634 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008635 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8636 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008637 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008638 goto out_err;
8639 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008640
8641 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008642 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008643 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 +01008644 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008645 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8646 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008647 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008648 goto out_err;
8649 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008650 free(proxy->conf.lfs_file);
8651 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8652 proxy->conf.lfs_line = proxy->conf.args.line;
8653
8654 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008655 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8656 char *errmsg = NULL;
8657 cur_arg = 1;
8658 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008659 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008660 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008661 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008662 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8663 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008664 free(errmsg);
8665 goto out_err;
8666 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008667 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008668 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008669 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8670 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8671 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8672 "%s%s, but got '%s'%s.\n",
8673 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008674 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008675 }
8676
8677 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8678 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008679 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008680
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008681 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008682 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8683 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008684 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008685 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008686 }
8687 rule->cond = cond;
8688 }
8689 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008690 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8691 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8692 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008693 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008694 }
8695
8696 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008697 out_err:
8698 free(rule);
8699 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008700}
8701
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008702/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008703struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008704{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008705 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008706 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008707 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008708 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008709
8710 rule = calloc(1, sizeof(*rule));
8711 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008712 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008713 goto out_err;
8714 }
8715
8716 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008717 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008718 cur_arg = 1;
8719 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008720 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008721 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008722 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008723 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008724 cur_arg = 1;
8725
8726 if (!*args[cur_arg] ||
8727 (*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 +01008728 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8729 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008730 goto out_err;
8731 }
8732 rule->arg.nice = atoi(args[cur_arg]);
8733 if (rule->arg.nice < -1024)
8734 rule->arg.nice = -1024;
8735 else if (rule->arg.nice > 1024)
8736 rule->arg.nice = 1024;
8737 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008738 } else if (!strcmp(args[0], "set-tos")) {
8739#ifdef IP_TOS
8740 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008741 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008742 cur_arg = 1;
8743
8744 if (!*args[cur_arg] ||
8745 (*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 +01008746 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8747 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008748 goto out_err;
8749 }
8750
8751 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8752 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008753 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8754 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008755 goto out_err;
8756 }
8757 cur_arg++;
8758#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008759 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 +02008760 goto out_err;
8761#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008762 } else if (!strcmp(args[0], "set-mark")) {
8763#ifdef SO_MARK
8764 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008765 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008766 cur_arg = 1;
8767
8768 if (!*args[cur_arg] ||
8769 (*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 +01008770 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8771 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008772 goto out_err;
8773 }
8774
8775 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8776 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008777 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8778 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008779 goto out_err;
8780 }
8781 cur_arg++;
8782 global.last_checks |= LSTCHK_NETADM;
8783#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008784 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 +02008785 goto out_err;
8786#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008787 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008788 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008789 cur_arg = 1;
8790
8791 if (!*args[cur_arg] ||
8792 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8793 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008794 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8795 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008796 goto out_err;
8797 }
8798 if (strcmp(args[cur_arg], "silent") == 0)
8799 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008800 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008801 goto bad_log_level;
8802 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008803 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008804 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008805 cur_arg = 1;
8806
8807 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8808 (*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 +01008809 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8810 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008811 goto out_err;
8812 }
8813
8814 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8815 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8816 LIST_INIT(&rule->arg.hdr_add.fmt);
8817
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008818 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008819 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008820 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 +01008821 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008822 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8823 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008824 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008825 goto out_err;
8826 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008827 free(proxy->conf.lfs_file);
8828 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8829 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008830 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008831 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008832 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008833 cur_arg = 1;
8834
8835 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008836 (*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 +01008837 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8838 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008839 goto out_err;
8840 }
8841
8842 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8843 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8844 LIST_INIT(&rule->arg.hdr_add.fmt);
8845
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008846 error = NULL;
8847 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008848 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8849 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008850 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008851 goto out_err;
8852 }
8853
8854 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008855 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008856 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 +01008857 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008858 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8859 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008860 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008861 goto out_err;
8862 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008863
8864 free(proxy->conf.lfs_file);
8865 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8866 proxy->conf.lfs_line = proxy->conf.args.line;
8867 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008868 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008869 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008870 cur_arg = 1;
8871
8872 if (!*args[cur_arg] ||
8873 (*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 +01008874 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8875 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008876 goto out_err;
8877 }
8878
8879 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8880 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8881
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008882 proxy->conf.args.ctx = ARGC_HRS;
8883 free(proxy->conf.lfs_file);
8884 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8885 proxy->conf.lfs_line = proxy->conf.args.line;
8886 cur_arg += 1;
8887 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8888 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008889 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008890 /*
8891 * '+ 8' for 'add-acl('
8892 * '- 9' for 'add-acl(' + trailing ')'
8893 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008894 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008895
8896 cur_arg = 1;
8897
8898 if (!*args[cur_arg] ||
8899 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008900 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8901 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008902 goto out_err;
8903 }
8904
8905 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008906 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008907 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008908 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008909 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008910 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8911 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008912 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008913 goto out_err;
8914 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008915 free(proxy->conf.lfs_file);
8916 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8917 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008918
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008919 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008920 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8921 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008922 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008923 /*
8924 * '+ 8' for 'del-acl('
8925 * '- 9' for 'del-acl(' + trailing ')'
8926 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008927 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008928
8929 cur_arg = 1;
8930
8931 if (!*args[cur_arg] ||
8932 (*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 +01008933 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8934 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008935 goto out_err;
8936 }
8937
8938 LIST_INIT(&rule->arg.map.key);
8939 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008940 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008941 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008942 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008943 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8944 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008945 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008946 goto out_err;
8947 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008948 free(proxy->conf.lfs_file);
8949 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8950 proxy->conf.lfs_line = proxy->conf.args.line;
8951 cur_arg += 1;
8952 } else if (strncmp(args[0], "del-map", 7) == 0) {
8953 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008954 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008955 /*
8956 * '+ 8' for 'del-map('
8957 * '- 9' for 'del-map(' + trailing ')'
8958 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008959 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008960
8961 cur_arg = 1;
8962
8963 if (!*args[cur_arg] ||
8964 (*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 +01008965 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8966 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008967 goto out_err;
8968 }
8969
8970 LIST_INIT(&rule->arg.map.key);
8971 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008972 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008973 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008974 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008975 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8976 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008977 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008978 goto out_err;
8979 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008980 free(proxy->conf.lfs_file);
8981 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8982 proxy->conf.lfs_line = proxy->conf.args.line;
8983 cur_arg += 1;
8984 } else if (strncmp(args[0], "set-map", 7) == 0) {
8985 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008986 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008987 /*
8988 * '+ 8' for 'set-map('
8989 * '- 9' for 'set-map(' + trailing ')'
8990 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008991 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008992
8993 cur_arg = 1;
8994
8995 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8996 (*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 +01008997 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8998 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008999 goto out_err;
9000 }
9001
9002 LIST_INIT(&rule->arg.map.key);
9003 LIST_INIT(&rule->arg.map.value);
9004
9005 proxy->conf.args.ctx = ARGC_HRS;
9006
9007 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009008 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009009 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009010 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009011 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9012 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009013 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009014 goto out_err;
9015 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009016
9017 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009018 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009019 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 +01009020 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009021 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9022 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009023 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009024 goto out_err;
9025 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009026
9027 free(proxy->conf.lfs_file);
9028 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9029 proxy->conf.lfs_line = proxy->conf.args.line;
9030
9031 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009032 } else if (strcmp(args[0], "redirect") == 0) {
9033 struct redirect_rule *redir;
9034 char *errmsg = NULL;
9035
9036 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009037 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9038 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009039 goto out_err;
9040 }
9041
9042 /* this redirect rule might already contain a parsed condition which
9043 * we'll pass to the http-request rule.
9044 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009045 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009046 rule->arg.redir = redir;
9047 rule->cond = redir->cond;
9048 redir->cond = NULL;
9049 cur_arg = 2;
9050 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009051 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9052 args[0][9] == '\0' && args[0][8] >= '0' &&
9053 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9054 struct sample_expr *expr;
9055 unsigned int where;
9056 char *err = NULL;
9057
9058 cur_arg = 1;
9059 proxy->conf.args.ctx = ARGC_TRK;
9060
9061 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9062 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009063 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9064 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009065 free(err);
9066 goto out_err;
9067 }
9068
9069 where = 0;
9070 if (proxy->cap & PR_CAP_FE)
9071 where |= SMP_VAL_FE_HRS_HDR;
9072 if (proxy->cap & PR_CAP_BE)
9073 where |= SMP_VAL_BE_HRS_HDR;
9074
9075 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009076 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9077 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9078 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9079 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009080 free(expr);
9081 goto out_err;
9082 }
9083
9084 if (strcmp(args[cur_arg], "table") == 0) {
9085 cur_arg++;
9086 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009087 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9088 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009089 free(expr);
9090 goto out_err;
9091 }
9092 /* we copy the table name for now, it will be resolved later */
9093 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9094 cur_arg++;
9095 }
9096 rule->arg.trk_ctr.expr = expr;
9097 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02009098 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009099 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9100 char *errmsg = NULL;
9101 cur_arg = 1;
9102 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009103 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009104 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009105 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009106 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9107 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009108 free(errmsg);
9109 goto out_err;
9110 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009111 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009112 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009113 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9114 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9115 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9116 "%s%s, but got '%s'%s.\n",
9117 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009118 goto out_err;
9119 }
9120
9121 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9122 struct acl_cond *cond;
9123 char *errmsg = NULL;
9124
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009125 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009126 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9127 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009128 free(errmsg);
9129 goto out_err;
9130 }
9131 rule->cond = cond;
9132 }
9133 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009134 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9135 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9136 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009137 goto out_err;
9138 }
9139
9140 return rule;
9141 out_err:
9142 free(rule);
9143 return NULL;
9144}
9145
Willy Tarreau4baae242012-12-27 12:00:31 +01009146/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009147 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009148 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9149 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009150 */
9151struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009152 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009153{
9154 struct redirect_rule *rule;
9155 int cur_arg;
9156 int type = REDIRECT_TYPE_NONE;
9157 int code = 302;
9158 const char *destination = NULL;
9159 const char *cookie = NULL;
9160 int cookie_set = 0;
9161 unsigned int flags = REDIRECT_FLAG_NONE;
9162 struct acl_cond *cond = NULL;
9163
9164 cur_arg = 0;
9165 while (*(args[cur_arg])) {
9166 if (strcmp(args[cur_arg], "location") == 0) {
9167 if (!*args[cur_arg + 1])
9168 goto missing_arg;
9169
9170 type = REDIRECT_TYPE_LOCATION;
9171 cur_arg++;
9172 destination = args[cur_arg];
9173 }
9174 else if (strcmp(args[cur_arg], "prefix") == 0) {
9175 if (!*args[cur_arg + 1])
9176 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009177 type = REDIRECT_TYPE_PREFIX;
9178 cur_arg++;
9179 destination = args[cur_arg];
9180 }
9181 else if (strcmp(args[cur_arg], "scheme") == 0) {
9182 if (!*args[cur_arg + 1])
9183 goto missing_arg;
9184
9185 type = REDIRECT_TYPE_SCHEME;
9186 cur_arg++;
9187 destination = args[cur_arg];
9188 }
9189 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9190 if (!*args[cur_arg + 1])
9191 goto missing_arg;
9192
9193 cur_arg++;
9194 cookie = args[cur_arg];
9195 cookie_set = 1;
9196 }
9197 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9198 if (!*args[cur_arg + 1])
9199 goto missing_arg;
9200
9201 cur_arg++;
9202 cookie = args[cur_arg];
9203 cookie_set = 0;
9204 }
9205 else if (strcmp(args[cur_arg], "code") == 0) {
9206 if (!*args[cur_arg + 1])
9207 goto missing_arg;
9208
9209 cur_arg++;
9210 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009211 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009212 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009213 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009214 args[cur_arg - 1], args[cur_arg]);
9215 return NULL;
9216 }
9217 }
9218 else if (!strcmp(args[cur_arg],"drop-query")) {
9219 flags |= REDIRECT_FLAG_DROP_QS;
9220 }
9221 else if (!strcmp(args[cur_arg],"append-slash")) {
9222 flags |= REDIRECT_FLAG_APPEND_SLASH;
9223 }
9224 else if (strcmp(args[cur_arg], "if") == 0 ||
9225 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009226 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009227 if (!cond) {
9228 memprintf(errmsg, "error in condition: %s", *errmsg);
9229 return NULL;
9230 }
9231 break;
9232 }
9233 else {
9234 memprintf(errmsg,
9235 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9236 args[cur_arg]);
9237 return NULL;
9238 }
9239 cur_arg++;
9240 }
9241
9242 if (type == REDIRECT_TYPE_NONE) {
9243 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9244 return NULL;
9245 }
9246
Willy Tarreaube4653b2015-05-28 15:26:58 +02009247 if (dir && type != REDIRECT_TYPE_LOCATION) {
9248 memprintf(errmsg, "response only supports redirect type 'location'");
9249 return NULL;
9250 }
9251
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009252 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009253 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009254 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009255
9256 if (!use_fmt) {
9257 /* old-style static redirect rule */
9258 rule->rdr_str = strdup(destination);
9259 rule->rdr_len = strlen(destination);
9260 }
9261 else {
9262 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009263
9264 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9265 * if prefix == "/", we don't want to add anything, otherwise it
9266 * makes it hard for the user to configure a self-redirection.
9267 */
Godbachd9722032014-12-18 15:44:58 +08009268 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009269 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009270 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9271 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 +01009272 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9273 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009274 return NULL;
9275 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009276 free(curproxy->conf.lfs_file);
9277 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9278 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009279 }
9280 }
9281
Willy Tarreau4baae242012-12-27 12:00:31 +01009282 if (cookie) {
9283 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9284 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9285 */
9286 rule->cookie_len = strlen(cookie);
9287 if (cookie_set) {
9288 rule->cookie_str = malloc(rule->cookie_len + 10);
9289 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9290 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9291 rule->cookie_len += 9;
9292 } else {
9293 rule->cookie_str = malloc(rule->cookie_len + 21);
9294 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9295 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9296 rule->cookie_len += 20;
9297 }
9298 }
9299 rule->type = type;
9300 rule->code = code;
9301 rule->flags = flags;
9302 LIST_INIT(&rule->list);
9303 return rule;
9304
9305 missing_arg:
9306 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9307 return NULL;
9308}
9309
Willy Tarreau8797c062007-05-07 00:55:35 +02009310/************************************************************************/
9311/* The code below is dedicated to ACL parsing and matching */
9312/************************************************************************/
9313
9314
Willy Tarreau14174bc2012-04-16 14:34:04 +02009315/* This function ensures that the prerequisites for an L7 fetch are ready,
9316 * which means that a request or response is ready. If some data is missing,
9317 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009318 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9319 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009320 *
9321 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009322 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9323 * decide whether or not an HTTP message is present ;
9324 * 0 if the requested data cannot be fetched or if it is certain that
9325 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009326 * 1 if an HTTP message is ready
9327 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009328int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009329 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009330{
Willy Tarreau192252e2015-04-04 01:47:55 +02009331 struct http_txn *txn;
9332 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009333
Willy Tarreaube508f12016-03-10 11:47:01 +01009334 /* Note: it is possible that <s> is NULL when called before stream
9335 * initialization (eg: tcp-request connection), so this function is the
9336 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009337 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009338 if (!s)
9339 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009340
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009341 if (!s->txn) {
9342 if (unlikely(!http_alloc_txn(s)))
9343 return 0; /* not enough memory */
9344 http_init_txn(s);
9345 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009346 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009347 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009348
9349 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009350 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009351
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009352 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009353 /* If the buffer does not leave enough free space at the end,
9354 * we must first realign it.
9355 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009356 if (s->req.buf->p > s->req.buf->data &&
9357 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9358 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009359
Willy Tarreau14174bc2012-04-16 14:34:04 +02009360 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009361 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009362 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009363
9364 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009365 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009366 http_msg_analyzer(msg, &txn->hdr_idx);
9367
9368 /* Still no valid request ? */
9369 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009370 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009371 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009372 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009373 }
9374 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009375 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009376 return 0;
9377 }
9378
9379 /* OK we just got a valid HTTP request. We have some minor
9380 * preparation to perform so that further checks can rely
9381 * on HTTP tests.
9382 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009383
9384 /* If the request was parsed but was too large, we must absolutely
9385 * return an error so that it is not processed. At the moment this
9386 * cannot happen, but if the parsers are to change in the future,
9387 * we want this check to be maintained.
9388 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009389 if (unlikely(s->req.buf->i + s->req.buf->p >
9390 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009391 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009392 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009393 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009394 return 1;
9395 }
9396
Willy Tarreau9b28e032012-10-12 23:49:43 +02009397 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009398 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009399 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009400
Willy Tarreau506d0502013-07-06 13:29:24 +02009401 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9402 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009403 }
9404
Willy Tarreau506d0502013-07-06 13:29:24 +02009405 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009406 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009407 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009408
9409 /* otherwise everything's ready for the request */
9410 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009411 else {
9412 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009413 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9414 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009415 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009416 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009417 }
9418
9419 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009420 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009421 return 1;
9422}
Willy Tarreau8797c062007-05-07 00:55:35 +02009423
Willy Tarreau8797c062007-05-07 00:55:35 +02009424/* 1. Check on METHOD
9425 * We use the pre-parsed method if it is known, and store its number as an
9426 * integer. If it is unknown, we use the pointer and the length.
9427 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009428static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009429{
9430 int len, meth;
9431
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009432 len = strlen(text);
9433 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009434
9435 pattern->val.i = meth;
9436 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009437 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009438 pattern->len = len;
9439 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009440 else {
9441 pattern->ptr.str = NULL;
9442 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009443 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009444 return 1;
9445}
9446
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009447/* This function fetches the method of current HTTP request and stores
9448 * it in the global pattern struct as a chunk. There are two possibilities :
9449 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9450 * in <len> and <ptr> is NULL ;
9451 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9452 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009453 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009454 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009455static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009456smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009457{
9458 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009459 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009460
Willy Tarreau24e32d82012-04-23 23:55:44 +02009461 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009462
Willy Tarreaube508f12016-03-10 11:47:01 +01009463 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009464 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009465 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009466 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009467 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009468 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9469 /* ensure the indexes are not affected */
9470 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009471 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009472 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9473 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009474 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009475 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009476 return 1;
9477}
9478
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009479/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009480static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009481{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009482 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009483 struct pattern_list *lst;
9484 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009485
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009486 list_for_each_entry(lst, &expr->patterns, list) {
9487 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009488
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009489 /* well-known method */
9490 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009491 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009492 return pattern;
9493 else
9494 continue;
9495 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009496
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009497 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009498 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009499 continue;
9500
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009501 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009502 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9503 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009504 return pattern;
9505 }
9506 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009507}
9508
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009509static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009510smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009511{
Willy Tarreaube508f12016-03-10 11:47:01 +01009512 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009513 char *ptr;
9514 int len;
9515
Willy Tarreauc0239e02012-04-16 14:42:55 +02009516 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009517
Willy Tarreaube508f12016-03-10 11:47:01 +01009518 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009519 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009520 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009521
9522 while ((len-- > 0) && (*ptr++ != '/'));
9523 if (len <= 0)
9524 return 0;
9525
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009526 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009527 smp->data.u.str.str = ptr;
9528 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009529
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009530 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009531 return 1;
9532}
9533
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009534static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009535smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009536{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009537 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009538 char *ptr;
9539 int len;
9540
Willy Tarreauc0239e02012-04-16 14:42:55 +02009541 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009542
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009543 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009544 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9545 return 0;
9546
Willy Tarreau8797c062007-05-07 00:55:35 +02009547 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009548 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009549
9550 while ((len-- > 0) && (*ptr++ != '/'));
9551 if (len <= 0)
9552 return 0;
9553
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009554 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009555 smp->data.u.str.str = ptr;
9556 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009557
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009558 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009559 return 1;
9560}
9561
9562/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009563static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009564smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009565{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009566 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009567 char *ptr;
9568 int len;
9569
Willy Tarreauc0239e02012-04-16 14:42:55 +02009570 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009571
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009572 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009573 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9574 return 0;
9575
Willy Tarreau8797c062007-05-07 00:55:35 +02009576 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009577 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009578
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009579 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009580 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009581 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009582 return 1;
9583}
9584
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009585static int
9586smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9587{
9588 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9589 return 0;
9590
9591 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009592 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009593 return 0;
9594 smp->strm->unique_id[0] = '\0';
9595 }
9596 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9597 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9598
9599 smp->data.type = SMP_T_STR;
9600 smp->data.u.str.str = smp->strm->unique_id;
9601 smp->flags = SMP_F_CONST;
9602 return 1;
9603}
9604
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009605/* Returns a string block containing all headers including the
9606 * empty line wich separes headers from the body. This is useful
9607 * form some headers analysis.
9608 */
9609static int
9610smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9611{
9612 struct http_msg *msg;
9613 struct hdr_idx *idx;
9614 struct http_txn *txn;
9615
9616 CHECK_HTTP_MESSAGE_FIRST();
9617
9618 txn = smp->strm->txn;
9619 idx = &txn->hdr_idx;
9620 msg = &txn->req;
9621
9622 smp->data.type = SMP_T_STR;
9623 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9624 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9625 (msg->chn->buf->p[msg->eoh] == '\r');
9626
9627 return 1;
9628}
9629
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009630/* Returns the header request in a length/value encoded format.
9631 * This is useful for exchanges with the SPOE.
9632 *
9633 * A "length value" is a multibyte code encoding numbers. It uses the
9634 * SPOE format. The encoding is the following:
9635 *
9636 * Each couple "header name" / "header value" is composed
9637 * like this:
9638 * "length value" "header name bytes"
9639 * "length value" "header value bytes"
9640 * When the last header is reached, the header name and the header
9641 * value are empty. Their length are 0
9642 */
9643static int
9644smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9645{
9646 struct http_msg *msg;
9647 struct chunk *temp;
9648 struct hdr_idx *idx;
9649 const char *cur_ptr, *cur_next, *p;
9650 int old_idx, cur_idx;
9651 struct hdr_idx_elem *cur_hdr;
9652 const char *hn, *hv;
9653 int hnl, hvl;
9654 int ret;
9655 struct http_txn *txn;
9656 char *buf;
9657 char *end;
9658
9659 CHECK_HTTP_MESSAGE_FIRST();
9660
9661 temp = get_trash_chunk();
9662 buf = temp->str;
9663 end = temp->str + temp->size;
9664
9665 txn = smp->strm->txn;
9666 idx = &txn->hdr_idx;
9667 msg = &txn->req;
9668
9669 /* Build array of headers. */
9670 old_idx = 0;
9671 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9672 while (1) {
9673 cur_idx = idx->v[old_idx].next;
9674 if (!cur_idx)
9675 break;
9676 old_idx = cur_idx;
9677
9678 cur_hdr = &idx->v[cur_idx];
9679 cur_ptr = cur_next;
9680 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9681
9682 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9683 * and the next header starts at cur_next. We'll check
9684 * this header in the list as well as against the default
9685 * rule.
9686 */
9687
9688 /* look for ': *'. */
9689 hn = cur_ptr;
9690 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9691 if (p >= cur_ptr+cur_hdr->len)
9692 continue;
9693 hnl = p - hn;
9694 p++;
9695 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9696 p++;
9697 if (p >= cur_ptr + cur_hdr->len)
9698 continue;
9699 hv = p;
9700 hvl = cur_ptr + cur_hdr->len-p;
9701
9702 /* encode the header name. */
9703 ret = encode_varint(hnl, &buf, end);
9704 if (ret == -1)
9705 return 0;
9706 if (buf + hnl > end)
9707 return 0;
9708 memcpy(buf, hn, hnl);
9709 buf += hnl;
9710
9711 /* encode and copy the value. */
9712 ret = encode_varint(hvl, &buf, end);
9713 if (ret == -1)
9714 return 0;
9715 if (buf + hvl > end)
9716 return 0;
9717 memcpy(buf, hv, hvl);
9718 buf += hvl;
9719 }
9720
9721 /* encode the end of the header list with empty
9722 * header name and header value.
9723 */
9724 ret = encode_varint(0, &buf, end);
9725 if (ret == -1)
9726 return 0;
9727 ret = encode_varint(0, &buf, end);
9728 if (ret == -1)
9729 return 0;
9730
9731 /* Initialise sample data which will be filled. */
9732 smp->data.type = SMP_T_BIN;
9733 smp->data.u.str.str = temp->str;
9734 smp->data.u.str.len = buf - temp->str;
9735 smp->data.u.str.size = temp->size;
9736
9737 return 1;
9738}
9739
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009740/* returns the longest available part of the body. This requires that the body
9741 * has been waited for using http-buffer-request.
9742 */
9743static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009744smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009745{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009746 struct http_msg *msg;
9747 unsigned long len;
9748 unsigned long block1;
9749 char *body;
9750 struct chunk *temp;
9751
9752 CHECK_HTTP_MESSAGE_FIRST();
9753
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009754 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009755 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009756 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009757 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009758
9759 len = http_body_bytes(msg);
9760 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9761
9762 block1 = len;
9763 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9764 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9765
9766 if (block1 == len) {
9767 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009768 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009769 smp->data.u.str.str = body;
9770 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009771 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9772 }
9773 else {
9774 /* buffer is wrapped, we need to defragment it */
9775 temp = get_trash_chunk();
9776 memcpy(temp->str, body, block1);
9777 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009778 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009779 smp->data.u.str.str = temp->str;
9780 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009781 smp->flags = SMP_F_VOL_TEST;
9782 }
9783 return 1;
9784}
9785
9786
9787/* returns the available length of the body. This requires that the body
9788 * has been waited for using http-buffer-request.
9789 */
9790static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009791smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009792{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009793 struct http_msg *msg;
9794
9795 CHECK_HTTP_MESSAGE_FIRST();
9796
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009797 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009798 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009799 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009800 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009801
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009802 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009803 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009804
9805 smp->flags = SMP_F_VOL_TEST;
9806 return 1;
9807}
9808
9809
9810/* returns the advertised length of the body, or the advertised size of the
9811 * chunks available in the buffer. This requires that the body has been waited
9812 * for using http-buffer-request.
9813 */
9814static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009815smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009816{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009817 struct http_msg *msg;
9818
9819 CHECK_HTTP_MESSAGE_FIRST();
9820
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009821 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009822 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009823 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009824 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009825
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009826 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009827 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009828
9829 smp->flags = SMP_F_VOL_TEST;
9830 return 1;
9831}
9832
9833
Willy Tarreau8797c062007-05-07 00:55:35 +02009834/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009835static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009836smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009837{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009838 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009839
Willy Tarreauc0239e02012-04-16 14:42:55 +02009840 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009841
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009842 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009843 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009844 smp->data.u.str.len = txn->req.sl.rq.u_l;
9845 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009846 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009847 return 1;
9848}
9849
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009850static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009851smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009852{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009853 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009854 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009855
Willy Tarreauc0239e02012-04-16 14:42:55 +02009856 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009857
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009858 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009859 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009860 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009861 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009862
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009863 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009864 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009865 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009866 return 1;
9867}
9868
9869static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009870smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009871{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009872 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009873 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009874
Willy Tarreauc0239e02012-04-16 14:42:55 +02009875 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009876
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009877 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009878 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009879 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9880 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009881
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009882 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009883 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009884 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009885 return 1;
9886}
9887
Willy Tarreau185b5c42012-04-26 15:11:51 +02009888/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9889 * Accepts an optional argument of type string containing the header field name,
9890 * and an optional argument of type signed or unsigned integer to request an
9891 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009892 * headers are considered from the first one. It does not stop on commas and
9893 * returns full lines instead (useful for User-Agent or Date for example).
9894 */
9895static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009896smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009897{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009898 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009899 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009900 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009901 int occ = 0;
9902 const char *name_str = NULL;
9903 int name_len = 0;
9904
9905 if (!ctx) {
9906 /* first call */
9907 ctx = &static_hdr_ctx;
9908 ctx->idx = 0;
9909 smp->ctx.a[0] = ctx;
9910 }
9911
9912 if (args) {
9913 if (args[0].type != ARGT_STR)
9914 return 0;
9915 name_str = args[0].data.str.str;
9916 name_len = args[0].data.str.len;
9917
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009918 if (args[1].type == ARGT_SINT)
9919 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009920 }
9921
9922 CHECK_HTTP_MESSAGE_FIRST();
9923
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009924 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009925 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 +02009926
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009927 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9928 /* search for header from the beginning */
9929 ctx->idx = 0;
9930
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009931 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009932 /* no explicit occurrence and single fetch => last header by default */
9933 occ = -1;
9934
9935 if (!occ)
9936 /* prepare to report multiple occurrences for ACL fetches */
9937 smp->flags |= SMP_F_NOT_LAST;
9938
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009939 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009940 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009941 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009942 return 1;
9943
9944 smp->flags &= ~SMP_F_NOT_LAST;
9945 return 0;
9946}
9947
9948/* 6. Check on HTTP header count. The number of occurrences is returned.
9949 * Accepts exactly 1 argument of type string. It does not stop on commas and
9950 * returns full lines instead (useful for User-Agent or Date for example).
9951 */
9952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009953smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009954{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009955 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009956 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009957 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009958 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009959 const char *name = NULL;
9960 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009961
Willy Tarreau601a4d12015-04-01 19:16:09 +02009962 if (args && args->type == ARGT_STR) {
9963 name = args->data.str.str;
9964 len = args->data.str.len;
9965 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009966
9967 CHECK_HTTP_MESSAGE_FIRST();
9968
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009969 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009970 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 +02009971
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009972 ctx.idx = 0;
9973 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009974 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009975 cnt++;
9976
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 = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009979 smp->flags = SMP_F_VOL_HDR;
9980 return 1;
9981}
9982
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009983static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009984smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009985{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009986 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009987 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009988 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009989 struct chunk *temp;
9990 char del = ',';
9991
9992 if (args && args->type == ARGT_STR)
9993 del = *args[0].data.str.str;
9994
9995 CHECK_HTTP_MESSAGE_FIRST();
9996
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009997 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009998 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 +02009999
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010000 temp = get_trash_chunk();
10001
10002 ctx.idx = 0;
10003 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10004 if (temp->len)
10005 temp->str[temp->len++] = del;
10006 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10007 temp->len += ctx.del;
10008 }
10009
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010010 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010011 smp->data.u.str.str = temp->str;
10012 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010013 smp->flags = SMP_F_VOL_HDR;
10014 return 1;
10015}
10016
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010017/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10018 * Accepts an optional argument of type string containing the header field name,
10019 * and an optional argument of type signed or unsigned integer to request an
10020 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010021 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010022 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010023static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010024smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010025{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010026 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010027 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010028 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010029 int occ = 0;
10030 const char *name_str = NULL;
10031 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010032
Willy Tarreaua890d072013-04-02 12:01:06 +020010033 if (!ctx) {
10034 /* first call */
10035 ctx = &static_hdr_ctx;
10036 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010037 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010038 }
10039
Willy Tarreau185b5c42012-04-26 15:11:51 +020010040 if (args) {
10041 if (args[0].type != ARGT_STR)
10042 return 0;
10043 name_str = args[0].data.str.str;
10044 name_len = args[0].data.str.len;
10045
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010046 if (args[1].type == ARGT_SINT)
10047 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010048 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010049
Willy Tarreaue333ec92012-04-16 16:26:40 +020010050 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010051
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010052 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010053 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 +020010054
Willy Tarreau185b5c42012-04-26 15:11:51 +020010055 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010056 /* search for header from the beginning */
10057 ctx->idx = 0;
10058
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010059 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010060 /* no explicit occurrence and single fetch => last header by default */
10061 occ = -1;
10062
10063 if (!occ)
10064 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010065 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010066
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010067 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010068 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010069 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010070 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010071
Willy Tarreau37406352012-04-23 16:16:37 +020010072 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010073 return 0;
10074}
10075
Willy Tarreauc11416f2007-06-17 16:58:38 +020010076/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010077 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010078 */
10079static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010080smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010081{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010082 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010083 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010084 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010085 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010086 const char *name = NULL;
10087 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010088
Willy Tarreau601a4d12015-04-01 19:16:09 +020010089 if (args && args->type == ARGT_STR) {
10090 name = args->data.str.str;
10091 len = args->data.str.len;
10092 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010093
Willy Tarreaue333ec92012-04-16 16:26:40 +020010094 CHECK_HTTP_MESSAGE_FIRST();
10095
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010096 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010097 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 +020010098
Willy Tarreau33a7e692007-06-10 19:45:56 +020010099 ctx.idx = 0;
10100 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010101 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010102 cnt++;
10103
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010104 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010105 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010106 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010107 return 1;
10108}
10109
Willy Tarreau185b5c42012-04-26 15:11:51 +020010110/* Fetch an HTTP header's integer value. The integer value is returned. It
10111 * takes a mandatory argument of type string and an optional one of type int
10112 * to designate a specific occurrence. It returns an unsigned integer, which
10113 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010114 */
10115static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010116smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010117{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010118 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010119
Willy Tarreauf853c462012-04-23 18:53:56 +020010120 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010121 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010122 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010123 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010124
Willy Tarreaud53e2422012-04-16 17:21:11 +020010125 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010126}
10127
Cyril Bonté69fa9922012-10-25 00:01:06 +020010128/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10129 * and an optional one of type int to designate a specific occurrence.
10130 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010131 */
10132static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010133smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010134{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010135 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010136
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010137 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010138 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010139 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010140 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010141 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010142 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010143 if (smp->data.u.str.len < temp->size - 1) {
10144 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10145 temp->str[smp->data.u.str.len] = '\0';
10146 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010147 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010148 break;
10149 }
10150 }
10151 }
10152
Willy Tarreaud53e2422012-04-16 17:21:11 +020010153 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010154 if (!(smp->flags & SMP_F_NOT_LAST))
10155 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010156 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010157 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010158}
10159
Willy Tarreau737b0c12007-06-10 21:28:46 +020010160/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10161 * the first '/' after the possible hostname, and ends before the possible '?'.
10162 */
10163static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010164smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010165{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010166 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010167 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010168
Willy Tarreauc0239e02012-04-16 14:42:55 +020010169 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010170
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010171 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010172 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010173 ptr = http_get_path(txn);
10174 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010175 return 0;
10176
10177 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010178 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010179 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010180
10181 while (ptr < end && *ptr != '?')
10182 ptr++;
10183
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010184 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010185 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010186 return 1;
10187}
10188
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010189/* This produces a concatenation of the first occurrence of the Host header
10190 * followed by the path component if it begins with a slash ('/'). This means
10191 * that '*' will not be added, resulting in exactly the first Host entry.
10192 * If no Host header is found, then the path is returned as-is. The returned
10193 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010194 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010195 */
10196static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010197smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010198{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010199 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010200 char *ptr, *end, *beg;
10201 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010202 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010203
10204 CHECK_HTTP_MESSAGE_FIRST();
10205
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010206 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010207 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010208 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010209 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010210
10211 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010212 temp = get_trash_chunk();
10213 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010214 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010215 smp->data.u.str.str = temp->str;
10216 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010217
10218 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010219 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010220 beg = http_get_path(txn);
10221 if (!beg)
10222 beg = end;
10223
10224 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10225
10226 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010227 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10228 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010229 }
10230
10231 smp->flags = SMP_F_VOL_1ST;
10232 return 1;
10233}
10234
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010235/* This produces a 32-bit hash of the concatenation of the first occurrence of
10236 * the Host header followed by the path component if it begins with a slash ('/').
10237 * This means that '*' will not be added, resulting in exactly the first Host
10238 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010239 * is hashed using the path hash followed by a full avalanche hash and provides a
10240 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010241 * high-traffic sites without having to store whole paths.
10242 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010243int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010244smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010245{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010246 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010247 struct hdr_ctx ctx;
10248 unsigned int hash = 0;
10249 char *ptr, *beg, *end;
10250 int len;
10251
10252 CHECK_HTTP_MESSAGE_FIRST();
10253
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010254 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010255 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010256 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010257 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10258 ptr = ctx.line + ctx.val;
10259 len = ctx.vlen;
10260 while (len--)
10261 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10262 }
10263
10264 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010265 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010266 beg = http_get_path(txn);
10267 if (!beg)
10268 beg = end;
10269
10270 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10271
10272 if (beg < ptr && *beg == '/') {
10273 while (beg < ptr)
10274 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10275 }
10276 hash = full_hash(hash);
10277
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010278 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010279 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010280 smp->flags = SMP_F_VOL_1ST;
10281 return 1;
10282}
10283
Willy Tarreau4a550602012-12-09 14:53:32 +010010284/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010285 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10286 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10287 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010288 * that in environments where IPv6 is insignificant, truncating the output to
10289 * 8 bytes would still work.
10290 */
10291static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010292smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010293{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010294 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010295 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010296
10297 if (!cli_conn)
10298 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010299
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010300 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010301 return 0;
10302
Willy Tarreau47ca5452012-12-23 20:22:19 +010010303 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010304 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010305 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010306
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010307 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010308 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010309 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010310 temp->len += 4;
10311 break;
10312 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010313 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010314 temp->len += 16;
10315 break;
10316 default:
10317 return 0;
10318 }
10319
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010320 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010321 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010322 return 1;
10323}
10324
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010325/* Extracts the query string, which comes after the question mark '?'. If no
10326 * question mark is found, nothing is returned. Otherwise it returns a sample
10327 * of type string carrying the whole query string.
10328 */
10329static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010330smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010331{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010332 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010333 char *ptr, *end;
10334
10335 CHECK_HTTP_MESSAGE_FIRST();
10336
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010337 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010338 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10339 end = ptr + txn->req.sl.rq.u_l;
10340
10341 /* look up the '?' */
10342 do {
10343 if (ptr == end)
10344 return 0;
10345 } while (*ptr++ != '?');
10346
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010347 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010348 smp->data.u.str.str = ptr;
10349 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010350 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10351 return 1;
10352}
10353
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010354static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010355smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010356{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010357 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10358 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10359 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010360
Willy Tarreau24e32d82012-04-23 23:55:44 +020010361 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010362
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010363 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010364 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010365 return 1;
10366}
10367
Willy Tarreau7f18e522010-10-22 20:04:13 +020010368/* return a valid test if the current request is the first one on the connection */
10369static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010370smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010371{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010372 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010373 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010374 return 1;
10375}
10376
Willy Tarreau34db1082012-04-19 17:16:54 +020010377/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010378static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010379smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010380{
10381
Willy Tarreau24e32d82012-04-23 23:55:44 +020010382 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010383 return 0;
10384
Willy Tarreauc0239e02012-04-16 14:42:55 +020010385 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010386
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010387 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010388 return 0;
10389
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010390 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010391 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010392 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010393 return 1;
10394}
Willy Tarreau8797c062007-05-07 00:55:35 +020010395
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010396/* Accepts exactly 1 argument of type userlist */
10397static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010398smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010399{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010400 if (!args || args->type != ARGT_USR)
10401 return 0;
10402
10403 CHECK_HTTP_MESSAGE_FIRST();
10404
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010405 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010406 return 0;
10407
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010408 /* if the user does not belong to the userlist or has a wrong password,
10409 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010410 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010411 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010412 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10413 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010414 return 0;
10415
10416 /* pat_match_auth() will need the user list */
10417 smp->ctx.a[0] = args->data.usr;
10418
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010419 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010420 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010421 smp->data.u.str.str = smp->strm->txn->auth.user;
10422 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010423
10424 return 1;
10425}
10426
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010427/* Try to find the next occurrence of a cookie name in a cookie header value.
10428 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10429 * the cookie value is returned into *value and *value_l, and the function
10430 * returns a pointer to the next pointer to search from if the value was found.
10431 * Otherwise if the cookie was not found, NULL is returned and neither value
10432 * nor value_l are touched. The input <hdr> string should first point to the
10433 * header's value, and the <hdr_end> pointer must point to the first character
10434 * not part of the value. <list> must be non-zero if value may represent a list
10435 * of values (cookie headers). This makes it faster to abort parsing when no
10436 * list is expected.
10437 */
David Carlier4686f792015-09-25 14:10:50 +010010438char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010439extract_cookie_value(char *hdr, const char *hdr_end,
10440 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010441 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010442{
10443 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10444 char *next;
10445
10446 /* we search at least a cookie name followed by an equal, and more
10447 * generally something like this :
10448 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10449 */
10450 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10451 /* Iterate through all cookies on this line */
10452
Willy Tarreau2235b262016-11-05 15:50:20 +010010453 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010454 att_beg++;
10455
10456 /* find att_end : this is the first character after the last non
10457 * space before the equal. It may be equal to hdr_end.
10458 */
10459 equal = att_end = att_beg;
10460
10461 while (equal < hdr_end) {
10462 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10463 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010464 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010465 continue;
10466 att_end = equal;
10467 }
10468
10469 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10470 * is between <att_beg> and <equal>, both may be identical.
10471 */
10472
10473 /* look for end of cookie if there is an equal sign */
10474 if (equal < hdr_end && *equal == '=') {
10475 /* look for the beginning of the value */
10476 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010477 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010478 val_beg++;
10479
10480 /* find the end of the value, respecting quotes */
10481 next = find_cookie_value_end(val_beg, hdr_end);
10482
10483 /* make val_end point to the first white space or delimitor after the value */
10484 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010485 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010486 val_end--;
10487 } else {
10488 val_beg = val_end = next = equal;
10489 }
10490
10491 /* We have nothing to do with attributes beginning with '$'. However,
10492 * they will automatically be removed if a header before them is removed,
10493 * since they're supposed to be linked together.
10494 */
10495 if (*att_beg == '$')
10496 continue;
10497
10498 /* Ignore cookies with no equal sign */
10499 if (equal == next)
10500 continue;
10501
10502 /* Now we have the cookie name between att_beg and att_end, and
10503 * its value between val_beg and val_end.
10504 */
10505
10506 if (att_end - att_beg == cookie_name_l &&
10507 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10508 /* let's return this value and indicate where to go on from */
10509 *value = val_beg;
10510 *value_l = val_end - val_beg;
10511 return next + 1;
10512 }
10513
10514 /* Set-Cookie headers only have the name in the first attr=value part */
10515 if (!list)
10516 break;
10517 }
10518
10519 return NULL;
10520}
10521
William Lallemanda43ba4e2014-01-28 18:14:25 +010010522/* Fetch a captured HTTP request header. The index is the position of
10523 * the "capture" option in the configuration file
10524 */
10525static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010526smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010527{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010528 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010529 int idx;
10530
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010531 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010532 return 0;
10533
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010534 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010535
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010536 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 +010010537 return 0;
10538
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010539 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010540 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010541 smp->data.u.str.str = smp->strm->req_cap[idx];
10542 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010543
10544 return 1;
10545}
10546
10547/* Fetch a captured HTTP response header. The index is the position of
10548 * the "capture" option in the configuration file
10549 */
10550static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010551smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010552{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010553 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010554 int idx;
10555
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010556 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010557 return 0;
10558
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010559 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010560
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010561 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 +010010562 return 0;
10563
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010564 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010565 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010566 smp->data.u.str.str = smp->strm->res_cap[idx];
10567 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010568
10569 return 1;
10570}
10571
William Lallemand65ad6e12014-01-31 15:08:02 +010010572/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10573static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010574smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010575{
10576 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010577 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010578 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010579
Willy Tarreau15e91e12015-04-04 00:52:09 +020010580 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010581 return 0;
10582
William Lallemand96a77852014-02-05 00:30:02 +010010583 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010584
William Lallemand96a77852014-02-05 00:30:02 +010010585 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10586 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010587
William Lallemand96a77852014-02-05 00:30:02 +010010588 temp = get_trash_chunk();
10589 temp->str = txn->uri;
10590 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010591 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010592 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010593 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010594
10595 return 1;
10596
10597}
10598
10599/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10600static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010601smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010602{
10603 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010604 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010605 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010606
Willy Tarreau15e91e12015-04-04 00:52:09 +020010607 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010608 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010609
William Lallemand65ad6e12014-01-31 15:08:02 +010010610 ptr = txn->uri;
10611
10612 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10613 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010614
William Lallemand65ad6e12014-01-31 15:08:02 +010010615 if (!*ptr)
10616 return 0;
10617
10618 ptr++; /* skip the space */
10619
10620 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010621 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010622 if (!ptr)
10623 return 0;
10624 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10625 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010626
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010627 smp->data.u.str = *temp;
10628 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010629 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010630 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010631
10632 return 1;
10633}
10634
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010635/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10636 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10637 */
10638static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010639smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010640{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010641 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010642
Willy Tarreau15e91e12015-04-04 00:52:09 +020010643 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010644 return 0;
10645
10646 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010647 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010648 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010649 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010650
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010651 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010652 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010653 smp->flags = SMP_F_CONST;
10654 return 1;
10655
10656}
10657
10658/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10659 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10660 */
10661static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010663{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010664 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010665
Willy Tarreau15e91e12015-04-04 00:52:09 +020010666 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010667 return 0;
10668
10669 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010670 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010671 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010672 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010673
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010674 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010675 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010676 smp->flags = SMP_F_CONST;
10677 return 1;
10678
10679}
10680
William Lallemand65ad6e12014-01-31 15:08:02 +010010681
Willy Tarreaue333ec92012-04-16 16:26:40 +020010682/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010683 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010684 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010685 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010686 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010687 * Accepts exactly 1 argument of type string. If the input options indicate
10688 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010689 * The returned sample is of type CSTR. Can be used to parse cookies in other
10690 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010691 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010692int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010693{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010694 struct http_txn *txn;
10695 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010696 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010697 const struct http_msg *msg;
10698 const char *hdr_name;
10699 int hdr_name_len;
10700 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010701 int occ = 0;
10702 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010703
Willy Tarreau24e32d82012-04-23 23:55:44 +020010704 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010705 return 0;
10706
Willy Tarreaua890d072013-04-02 12:01:06 +020010707 if (!ctx) {
10708 /* first call */
10709 ctx = &static_hdr_ctx;
10710 ctx->idx = 0;
10711 smp->ctx.a[2] = ctx;
10712 }
10713
Willy Tarreaue333ec92012-04-16 16:26:40 +020010714 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010715
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010716 txn = smp->strm->txn;
10717 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010718
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010719 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010720 msg = &txn->req;
10721 hdr_name = "Cookie";
10722 hdr_name_len = 6;
10723 } else {
10724 msg = &txn->rsp;
10725 hdr_name = "Set-Cookie";
10726 hdr_name_len = 10;
10727 }
10728
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010729 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010730 /* no explicit occurrence and single fetch => last cookie by default */
10731 occ = -1;
10732
10733 /* OK so basically here, either we want only one value and it's the
10734 * last one, or we want to iterate over all of them and we fetch the
10735 * next one.
10736 */
10737
Willy Tarreau9b28e032012-10-12 23:49:43 +020010738 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010739 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010740 /* search for the header from the beginning, we must first initialize
10741 * the search parameters.
10742 */
Willy Tarreau37406352012-04-23 16:16:37 +020010743 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010744 ctx->idx = 0;
10745 }
10746
Willy Tarreau28376d62012-04-26 21:26:10 +020010747 smp->flags |= SMP_F_VOL_HDR;
10748
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010749 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010750 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10751 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010752 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10753 goto out;
10754
Willy Tarreau24e32d82012-04-23 23:55:44 +020010755 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010756 continue;
10757
Willy Tarreau37406352012-04-23 16:16:37 +020010758 smp->ctx.a[0] = ctx->line + ctx->val;
10759 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010760 }
10761
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010762 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010763 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010764 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010765 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010766 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010767 &smp->data.u.str.str,
10768 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010769 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010770 found = 1;
10771 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010772 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010773 smp->flags |= SMP_F_NOT_LAST;
10774 return 1;
10775 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010776 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010777 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010778 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010779 /* all cookie headers and values were scanned. If we're looking for the
10780 * last occurrence, we may return it now.
10781 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010782 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010783 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010784 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010785}
10786
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010787/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010788 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010789 * multiple cookies may be parsed on the same line. The returned sample is of
10790 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010791 */
10792static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010793smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010794{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010795 struct http_txn *txn;
10796 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010797 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010798 const struct http_msg *msg;
10799 const char *hdr_name;
10800 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010801 int cnt;
10802 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010803 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010804
Willy Tarreau24e32d82012-04-23 23:55:44 +020010805 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010806 return 0;
10807
Willy Tarreaue333ec92012-04-16 16:26:40 +020010808 CHECK_HTTP_MESSAGE_FIRST();
10809
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010810 txn = smp->strm->txn;
10811 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010812
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010813 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010814 msg = &txn->req;
10815 hdr_name = "Cookie";
10816 hdr_name_len = 6;
10817 } else {
10818 msg = &txn->rsp;
10819 hdr_name = "Set-Cookie";
10820 hdr_name_len = 10;
10821 }
10822
Willy Tarreau9b28e032012-10-12 23:49:43 +020010823 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010824 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010825 ctx.idx = 0;
10826 cnt = 0;
10827
10828 while (1) {
10829 /* Note: val_beg == NULL every time we need to fetch a new header */
10830 if (!val_beg) {
10831 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10832 break;
10833
Willy Tarreau24e32d82012-04-23 23:55:44 +020010834 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010835 continue;
10836
10837 val_beg = ctx.line + ctx.val;
10838 val_end = val_beg + ctx.vlen;
10839 }
10840
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010841 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010842 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010843 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010844 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010845 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010846 &smp->data.u.str.str,
10847 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010848 cnt++;
10849 }
10850 }
10851
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010852 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010853 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010854 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010855 return 1;
10856}
10857
Willy Tarreau51539362012-05-08 12:46:28 +020010858/* Fetch an cookie's integer value. The integer value is returned. It
10859 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10860 */
10861static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010862smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010863{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010864 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010865
10866 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010867 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010868 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010869 }
10870
10871 return ret;
10872}
10873
Willy Tarreau8797c062007-05-07 00:55:35 +020010874/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010875/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010876/************************************************************************/
10877
David Cournapeau16023ee2010-12-23 20:55:41 +090010878/*
10879 * Given a path string and its length, find the position of beginning of the
10880 * query string. Returns NULL if no query string is found in the path.
10881 *
10882 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10883 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010884 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010885 */
bedis4c75cca2012-10-05 08:38:24 +020010886static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010887{
10888 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010889
bedis4c75cca2012-10-05 08:38:24 +020010890 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010891 return p ? p + 1 : NULL;
10892}
10893
bedis4c75cca2012-10-05 08:38:24 +020010894static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010895{
bedis4c75cca2012-10-05 08:38:24 +020010896 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010897}
10898
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010899/* after increasing a pointer value, it can exceed the first buffer
10900 * size. This function transform the value of <ptr> according with
10901 * the expected position. <chunks> is an array of the one or two
10902 * avalaible chunks. The first value is the start of the first chunk,
10903 * the second value if the end+1 of the first chunks. The third value
10904 * is NULL or the start of the second chunk and the fourth value is
10905 * the end+1 of the second chunk. The function returns 1 if does a
10906 * wrap, else returns 0.
10907 */
10908static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10909{
10910 if (*ptr < chunks[1])
10911 return 0;
10912 if (!chunks[2])
10913 return 0;
10914 *ptr = chunks[2] + ( *ptr - chunks[1] );
10915 return 1;
10916}
10917
David Cournapeau16023ee2010-12-23 20:55:41 +090010918/*
10919 * Given a url parameter, find the starting position of the first occurence,
10920 * or NULL if the parameter is not found.
10921 *
10922 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10923 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010924 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010925 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010926 * or the second chunk. The caller must be check the position before using the
10927 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010928 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010929static const char *
10930find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010931 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010932 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010933{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010934 const char *pos, *last, *equal;
10935 const char **bufs = chunks;
10936 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010937
David Cournapeau16023ee2010-12-23 20:55:41 +090010938
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010939 pos = bufs[0];
10940 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010941 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010942 /* Check the equal. */
10943 equal = pos + url_param_name_l;
10944 if (fix_pointer_if_wrap(chunks, &equal)) {
10945 if (equal >= chunks[3])
10946 return NULL;
10947 } else {
10948 if (equal >= chunks[1])
10949 return NULL;
10950 }
10951 if (*equal == '=') {
10952 if (pos + url_param_name_l > last) {
10953 /* process wrap case, we detect a wrap. In this case, the
10954 * comparison is performed in two parts.
10955 */
10956
10957 /* This is the end, we dont have any other chunk. */
10958 if (bufs != chunks || !bufs[2])
10959 return NULL;
10960
10961 /* Compute the length of each part of the comparison. */
10962 l1 = last - pos;
10963 l2 = url_param_name_l - l1;
10964
10965 /* The second buffer is too short to contain the compared string. */
10966 if (bufs[2] + l2 > bufs[3])
10967 return NULL;
10968
10969 if (memcmp(pos, url_param_name, l1) == 0 &&
10970 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10971 return pos;
10972
10973 /* Perform wrapping and jump the string who fail the comparison. */
10974 bufs += 2;
10975 pos = bufs[0] + l2;
10976 last = bufs[1];
10977
10978 } else {
10979 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010980 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10981 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010982 pos += url_param_name_l + 1;
10983 if (fix_pointer_if_wrap(chunks, &pos))
10984 last = bufs[2];
10985 }
10986 }
10987
10988 while (1) {
10989 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010990 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010991 pos++;
10992 if (pos < last)
10993 break;
10994 /* process buffer wrapping. */
10995 if (bufs != chunks || !bufs[2])
10996 return NULL;
10997 bufs += 2;
10998 pos = bufs[0];
10999 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011000 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011001 pos++;
11002 }
11003 return NULL;
11004}
11005
11006/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011007 * Given a url parameter name and a query string, find the next value.
11008 * An empty url_param_name matches the first available parameter.
11009 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11010 * respectively provide a pointer to the value and its end.
11011 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011012 */
11013static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011014find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011015 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011016 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011017{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011018 const char *arg_start, *qs_end;
11019 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011020
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011021 arg_start = chunks[0];
11022 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011023 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011024 /* Looks for an argument name. */
11025 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011026 url_param_name, url_param_name_l,
11027 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011028 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011029 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011030 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011031 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011032 if (!arg_start)
11033 return 0;
11034
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011035 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011036 while (1) {
11037 /* looks for the first argument. */
11038 value_start = memchr(arg_start, '=', qs_end - arg_start);
11039 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011040 /* Check for wrapping. */
11041 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011042 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011043 chunks[2]) {
11044 arg_start = chunks[2];
11045 qs_end = chunks[3];
11046 continue;
11047 }
11048 return 0;
11049 }
11050 break;
11051 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011052 value_start++;
11053 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011054 else {
11055 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011056 value_start = arg_start + url_param_name_l + 1;
11057
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011058 /* Check for pointer wrapping. */
11059 if (fix_pointer_if_wrap(chunks, &value_start)) {
11060 /* Update the end pointer. */
11061 qs_end = chunks[3];
11062
11063 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011064 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011065 return 0;
11066 }
11067 }
11068
David Cournapeau16023ee2010-12-23 20:55:41 +090011069 value_end = value_start;
11070
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011071 while (1) {
11072 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11073 value_end++;
11074 if (value_end < qs_end)
11075 break;
11076 /* process buffer wrapping. */
11077 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011078 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011079 chunks[2]) {
11080 value_end = chunks[2];
11081 qs_end = chunks[3];
11082 continue;
11083 }
11084 break;
11085 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011086
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011087 *vstart = value_start;
11088 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011089 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011090}
11091
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011092/* This scans a URL-encoded query string. It takes an optionally wrapping
11093 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11094 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11095 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011096 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011097static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011098smp_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 +090011099{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011100 const char *vstart, *vend;
11101 struct chunk *temp;
11102 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011103
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011104 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011105 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011106 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011107 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011108 return 0;
11109
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011110 /* Create sample. If the value is contiguous, return the pointer as CONST,
11111 * if the value is wrapped, copy-it in a buffer.
11112 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011113 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011114 if (chunks[2] &&
11115 vstart >= chunks[0] && vstart <= chunks[1] &&
11116 vend >= chunks[2] && vend <= chunks[3]) {
11117 /* Wrapped case. */
11118 temp = get_trash_chunk();
11119 memcpy(temp->str, vstart, chunks[1] - vstart);
11120 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011121 smp->data.u.str.str = temp->str;
11122 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011123 } else {
11124 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011125 smp->data.u.str.str = (char *)vstart;
11126 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011127 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11128 }
11129
11130 /* Update context, check wrapping. */
11131 chunks[0] = vend;
11132 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11133 chunks[1] = chunks[3];
11134 chunks[2] = NULL;
11135 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011136
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011137 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011138 smp->flags |= SMP_F_NOT_LAST;
11139
David Cournapeau16023ee2010-12-23 20:55:41 +090011140 return 1;
11141}
11142
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011143/* This function iterates over each parameter of the query string. It uses
11144 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011145 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11146 * An optional parameter name is passed in args[0], otherwise any parameter is
11147 * considered. It supports an optional delimiter argument for the beginning of
11148 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011149 */
11150static int
11151smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11152{
11153 struct http_msg *msg;
11154 char delim = '?';
11155 const char *name;
11156 int name_len;
11157
Dragan Dosen26f77e52015-05-25 10:02:11 +020011158 if (!args ||
11159 (args[0].type && args[0].type != ARGT_STR) ||
11160 (args[1].type && args[1].type != ARGT_STR))
11161 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011162
Dragan Dosen26f77e52015-05-25 10:02:11 +020011163 name = "";
11164 name_len = 0;
11165 if (args->type == ARGT_STR) {
11166 name = args->data.str.str;
11167 name_len = args->data.str.len;
11168 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011169
Dragan Dosen26f77e52015-05-25 10:02:11 +020011170 if (args[1].type)
11171 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011172
Dragan Dosen26f77e52015-05-25 10:02:11 +020011173 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011174 CHECK_HTTP_MESSAGE_FIRST();
11175
11176 msg = &smp->strm->txn->req;
11177
11178 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11179 msg->sl.rq.u_l, delim);
11180 if (!smp->ctx.a[0])
11181 return 0;
11182
11183 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011184
11185 /* Assume that the context is filled with NULL pointer
11186 * before the first call.
11187 * smp->ctx.a[2] = NULL;
11188 * smp->ctx.a[3] = NULL;
11189 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011190 }
11191
11192 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11193}
11194
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011195/* This function iterates over each parameter of the body. This requires
11196 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011197 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11198 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11199 * optional second part if the body wraps at the end of the buffer. An optional
11200 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011201 */
11202static int
11203smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11204{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011205 struct http_msg *msg;
11206 unsigned long len;
11207 unsigned long block1;
11208 char *body;
11209 const char *name;
11210 int name_len;
11211
11212 if (!args || (args[0].type && args[0].type != ARGT_STR))
11213 return 0;
11214
11215 name = "";
11216 name_len = 0;
11217 if (args[0].type == ARGT_STR) {
11218 name = args[0].data.str.str;
11219 name_len = args[0].data.str.len;
11220 }
11221
11222 if (!smp->ctx.a[0]) { // first call, find the query string
11223 CHECK_HTTP_MESSAGE_FIRST();
11224
11225 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011226 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011227 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011228 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011229
11230 len = http_body_bytes(msg);
11231 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11232
11233 block1 = len;
11234 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11235 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11236
11237 if (block1 == len) {
11238 /* buffer is not wrapped (or empty) */
11239 smp->ctx.a[0] = body;
11240 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011241
11242 /* Assume that the context is filled with NULL pointer
11243 * before the first call.
11244 * smp->ctx.a[2] = NULL;
11245 * smp->ctx.a[3] = NULL;
11246 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011247 }
11248 else {
11249 /* buffer is wrapped, we need to defragment it */
11250 smp->ctx.a[0] = body;
11251 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011252 smp->ctx.a[2] = msg->chn->buf->data;
11253 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011254 }
11255 }
11256 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11257}
11258
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011259/* Return the signed integer value for the specified url parameter (see url_param
11260 * above).
11261 */
11262static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011263smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011264{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011265 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011266
11267 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011268 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011269 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011270 }
11271
11272 return ret;
11273}
11274
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011275/* This produces a 32-bit hash of the concatenation of the first occurrence of
11276 * the Host header followed by the path component if it begins with a slash ('/').
11277 * This means that '*' will not be added, resulting in exactly the first Host
11278 * entry. If no Host header is found, then the path is used. The resulting value
11279 * is hashed using the url hash followed by a full avalanche hash and provides a
11280 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11281 * high-traffic sites without having to store whole paths.
11282 * this differs from the base32 functions in that it includes the url parameters
11283 * as well as the path
11284 */
11285static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011286smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011287{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011288 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011289 struct hdr_ctx ctx;
11290 unsigned int hash = 0;
11291 char *ptr, *beg, *end;
11292 int len;
11293
11294 CHECK_HTTP_MESSAGE_FIRST();
11295
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011296 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011297 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011298 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011299 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11300 ptr = ctx.line + ctx.val;
11301 len = ctx.vlen;
11302 while (len--)
11303 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11304 }
11305
11306 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011307 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011308 beg = http_get_path(txn);
11309 if (!beg)
11310 beg = end;
11311
11312 for (ptr = beg; ptr < end ; ptr++);
11313
11314 if (beg < ptr && *beg == '/') {
11315 while (beg < ptr)
11316 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11317 }
11318 hash = full_hash(hash);
11319
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011320 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011321 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011322 smp->flags = SMP_F_VOL_1ST;
11323 return 1;
11324}
11325
11326/* This concatenates the source address with the 32-bit hash of the Host and
11327 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11328 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11329 * on the source address length. The URL hash is stored before the address so
11330 * that in environments where IPv6 is insignificant, truncating the output to
11331 * 8 bytes would still work.
11332 */
11333static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011334smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011335{
11336 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011337 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011338
Dragan Dosendb5af612016-06-16 11:23:01 +020011339 if (!cli_conn)
11340 return 0;
11341
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011342 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011343 return 0;
11344
11345 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011346 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11347 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011348
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011349 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011350 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011351 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011352 temp->len += 4;
11353 break;
11354 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011355 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011356 temp->len += 16;
11357 break;
11358 default:
11359 return 0;
11360 }
11361
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011362 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011363 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011364 return 1;
11365}
11366
Willy Tarreau185b5c42012-04-26 15:11:51 +020011367/* This function is used to validate the arguments passed to any "hdr" fetch
11368 * keyword. These keywords support an optional positive or negative occurrence
11369 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11370 * is assumed that the types are already the correct ones. Returns 0 on error,
11371 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11372 * error message in case of error, that the caller is responsible for freeing.
11373 * The initial location must either be freeable or NULL.
11374 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011375int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011376{
11377 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011378 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011379 return 0;
11380 }
11381 return 1;
11382}
11383
Willy Tarreau276fae92013-07-25 14:36:01 +020011384/* takes an UINT value on input supposed to represent the time since EPOCH,
11385 * adds an optional offset found in args[0] and emits a string representing
11386 * the date in RFC-1123/5322 format.
11387 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011388static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011389{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011390 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011391 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11392 struct chunk *temp;
11393 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011394 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011395 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011396
11397 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011398 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011399 curr_date += args[0].data.sint;
11400
11401 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011402 if (!tm)
11403 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011404
11405 temp = get_trash_chunk();
11406 temp->len = snprintf(temp->str, temp->size - temp->len,
11407 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11408 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11409 tm->tm_hour, tm->tm_min, tm->tm_sec);
11410
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011411 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011412 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011413 return 1;
11414}
11415
Thierry FOURNIERad903512014-04-11 17:51:01 +020011416/* Match language range with language tag. RFC2616 14.4:
11417 *
11418 * A language-range matches a language-tag if it exactly equals
11419 * the tag, or if it exactly equals a prefix of the tag such
11420 * that the first tag character following the prefix is "-".
11421 *
11422 * Return 1 if the strings match, else return 0.
11423 */
11424static inline int language_range_match(const char *range, int range_len,
11425 const char *tag, int tag_len)
11426{
11427 const char *end = range + range_len;
11428 const char *tend = tag + tag_len;
11429 while (range < end) {
11430 if (*range == '-' && tag == tend)
11431 return 1;
11432 if (*range != *tag || tag == tend)
11433 return 0;
11434 range++;
11435 tag++;
11436 }
11437 /* Return true only if the last char of the tag is matched. */
11438 return tag == tend;
11439}
11440
11441/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011442static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011443{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011444 const char *al = smp->data.u.str.str;
11445 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011446 const char *token;
11447 int toklen;
11448 int qvalue;
11449 const char *str;
11450 const char *w;
11451 int best_q = 0;
11452
11453 /* Set the constant to the sample, because the output of the
11454 * function will be peek in the constant configuration string.
11455 */
11456 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011457 smp->data.u.str.size = 0;
11458 smp->data.u.str.str = "";
11459 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011460
11461 /* Parse the accept language */
11462 while (1) {
11463
11464 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011465 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011466 al++;
11467 if (al >= end)
11468 break;
11469
11470 /* Start of the fisrt word. */
11471 token = al;
11472
11473 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011474 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011475 al++;
11476 if (al == token)
11477 goto expect_comma;
11478
11479 /* Length of the token. */
11480 toklen = al - token;
11481 qvalue = 1000;
11482
11483 /* Check if the token exists in the list. If the token not exists,
11484 * jump to the next token.
11485 */
11486 str = args[0].data.str.str;
11487 w = str;
11488 while (1) {
11489 if (*str == ';' || *str == '\0') {
11490 if (language_range_match(token, toklen, w, str-w))
11491 goto look_for_q;
11492 if (*str == '\0')
11493 goto expect_comma;
11494 w = str + 1;
11495 }
11496 str++;
11497 }
11498 goto expect_comma;
11499
11500look_for_q:
11501
11502 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011503 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011504 al++;
11505 if (al >= end)
11506 goto process_value;
11507
11508 /* If ',' is found, process the result */
11509 if (*al == ',')
11510 goto process_value;
11511
11512 /* If the character is different from ';', look
11513 * for the end of the header part in best effort.
11514 */
11515 if (*al != ';')
11516 goto expect_comma;
11517
11518 /* Assumes that the char is ';', now expect "q=". */
11519 al++;
11520
11521 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011522 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011523 al++;
11524 if (al >= end)
11525 goto process_value;
11526
11527 /* Expect 'q'. If no 'q', continue in best effort */
11528 if (*al != 'q')
11529 goto process_value;
11530 al++;
11531
11532 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011533 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011534 al++;
11535 if (al >= end)
11536 goto process_value;
11537
11538 /* Expect '='. If no '=', continue in best effort */
11539 if (*al != '=')
11540 goto process_value;
11541 al++;
11542
11543 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011544 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011545 al++;
11546 if (al >= end)
11547 goto process_value;
11548
11549 /* Parse the q value. */
11550 qvalue = parse_qvalue(al, &al);
11551
11552process_value:
11553
11554 /* If the new q value is the best q value, then store the associated
11555 * language in the response. If qvalue is the biggest value (1000),
11556 * break the process.
11557 */
11558 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011559 smp->data.u.str.str = (char *)w;
11560 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011561 if (qvalue >= 1000)
11562 break;
11563 best_q = qvalue;
11564 }
11565
11566expect_comma:
11567
11568 /* Expect comma or end. If the end is detected, quit the loop. */
11569 while (al < end && *al != ',')
11570 al++;
11571 if (al >= end)
11572 break;
11573
11574 /* Comma is found, jump it and restart the analyzer. */
11575 al++;
11576 }
11577
11578 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011579 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11580 smp->data.u.str.str = args[1].data.str.str;
11581 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011582 }
11583
11584 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011585 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011586}
11587
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011588/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011589static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011590{
11591 /* If the constant flag is set or if not size is avalaible at
11592 * the end of the buffer, copy the string in other buffer
11593 * before decoding.
11594 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011595 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011596 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011597 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11598 smp->data.u.str.str = str->str;
11599 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011600 smp->flags &= ~SMP_F_CONST;
11601 }
11602
11603 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011604 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11605 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011606 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011607}
11608
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011609static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11610{
11611 struct proxy *fe = strm_fe(smp->strm);
11612 int idx, i;
11613 struct cap_hdr *hdr;
11614 int len;
11615
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011616 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011617 return 0;
11618
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011619 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011620
11621 /* Check the availibity of the capture id. */
11622 if (idx > fe->nb_req_cap - 1)
11623 return 0;
11624
11625 /* Look for the original configuration. */
11626 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11627 hdr != NULL && i != idx ;
11628 i--, hdr = hdr->next);
11629 if (!hdr)
11630 return 0;
11631
11632 /* check for the memory allocation */
11633 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011634 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011635 if (smp->strm->req_cap[hdr->index] == NULL)
11636 return 0;
11637
11638 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011639 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011640 if (len > hdr->len)
11641 len = hdr->len;
11642
11643 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011644 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011645 smp->strm->req_cap[idx][len] = '\0';
11646
11647 return 1;
11648}
11649
11650static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11651{
11652 struct proxy *fe = strm_fe(smp->strm);
11653 int idx, i;
11654 struct cap_hdr *hdr;
11655 int len;
11656
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011657 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011658 return 0;
11659
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011660 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011661
11662 /* Check the availibity of the capture id. */
11663 if (idx > fe->nb_rsp_cap - 1)
11664 return 0;
11665
11666 /* Look for the original configuration. */
11667 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11668 hdr != NULL && i != idx ;
11669 i--, hdr = hdr->next);
11670 if (!hdr)
11671 return 0;
11672
11673 /* check for the memory allocation */
11674 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011675 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011676 if (smp->strm->res_cap[hdr->index] == NULL)
11677 return 0;
11678
11679 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011680 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011681 if (len > hdr->len)
11682 len = hdr->len;
11683
11684 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011685 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011686 smp->strm->res_cap[idx][len] = '\0';
11687
11688 return 1;
11689}
11690
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011691/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011692 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011693 * the relevant part of the request line accordingly. Then it updates various
11694 * pointers to the next elements which were moved, and the total buffer length.
11695 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011696 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11697 * error, though this can be revisited when this code is finally exploited.
11698 *
11699 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11700 * query string and 3 to replace uri.
11701 *
11702 * In query string case, the mark question '?' must be set at the start of the
11703 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011704 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011705int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011706 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011707{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011708 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011709 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011710 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011711 int delta;
11712
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011713 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011714 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011715 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011716 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11717
11718 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011719 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011720 txn->req.sl.rq.m_l += delta;
11721 txn->req.sl.rq.u += delta;
11722 txn->req.sl.rq.v += delta;
11723 break;
11724
11725 case 1: // path
11726 cur_ptr = http_get_path(txn);
11727 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011728 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011729
11730 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011731 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011732 cur_end++;
11733
11734 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011735 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011736 txn->req.sl.rq.u_l += delta;
11737 txn->req.sl.rq.v += delta;
11738 break;
11739
11740 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011741 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011742 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011743 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11744 while (cur_ptr < cur_end && *cur_ptr != '?')
11745 cur_ptr++;
11746
11747 /* skip the question mark or indicate that we must insert it
11748 * (but only if the format string is not empty then).
11749 */
11750 if (cur_ptr < cur_end)
11751 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011752 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011753 offset = 0;
11754
11755 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011756 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011757 txn->req.sl.rq.u_l += delta;
11758 txn->req.sl.rq.v += delta;
11759 break;
11760
11761 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011762 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011763 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11764
11765 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011766 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011767 txn->req.sl.rq.u_l += delta;
11768 txn->req.sl.rq.v += delta;
11769 break;
11770
11771 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011772 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011773 }
11774
11775 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011776 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011777 txn->req.sl.rq.l += delta;
11778 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011779 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011780 return 0;
11781}
11782
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011783/* This function replace the HTTP status code and the associated message. The
11784 * variable <status> contains the new status code. This function never fails.
11785 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011786void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011787{
11788 struct http_txn *txn = s->txn;
11789 char *cur_ptr, *cur_end;
11790 int delta;
11791 char *res;
11792 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011793 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011794 int msg_len;
11795
11796 chunk_reset(&trash);
11797
11798 res = ultoa_o(status, trash.str, trash.size);
11799 c_l = res - trash.str;
11800
11801 trash.str[c_l] = ' ';
11802 trash.len = c_l + 1;
11803
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011804 /* Do we have a custom reason format string? */
11805 if (msg == NULL)
11806 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011807 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011808 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11809 trash.len += msg_len;
11810
11811 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11812 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11813
11814 /* commit changes and adjust message */
11815 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11816
11817 /* adjust res line offsets and lengths */
11818 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11819 txn->rsp.sl.st.c_l = c_l;
11820 txn->rsp.sl.st.r_l = msg_len;
11821
11822 delta = trash.len - (cur_end - cur_ptr);
11823 txn->rsp.sl.st.l += delta;
11824 txn->hdr_idx.v[0].len += delta;
11825 http_msg_move_end(&txn->rsp, delta);
11826}
11827
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011828/* This function executes one of the set-{method,path,query,uri} actions. It
11829 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011830 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011831 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011832 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11833 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011834 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011835enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011836 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011837{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011838 struct chunk *replace;
11839 enum act_return ret = ACT_RET_ERR;
11840
11841 replace = alloc_trash_chunk();
11842 if (!replace)
11843 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011844
11845 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011846 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011847 replace->str[replace->len++] = '?';
11848 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11849 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011850
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011851 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11852
11853 ret = ACT_RET_CONT;
11854
11855leave:
11856 free_trash_chunk(replace);
11857 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011858}
11859
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011860/* This function is just a compliant action wrapper for "set-status". */
11861enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011862 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011863{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011864 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011865 return ACT_RET_CONT;
11866}
11867
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011868/* parse an http-request action among :
11869 * set-method
11870 * set-path
11871 * set-query
11872 * set-uri
11873 *
11874 * All of them accept a single argument of type string representing a log-format.
11875 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11876 * 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 +020011877 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011878 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011879enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11880 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011881{
11882 int cur_arg = *orig_arg;
11883
Thierry FOURNIER42148732015-09-02 17:17:33 +020011884 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011885
11886 switch (args[0][4]) {
11887 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011888 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011889 rule->action_ptr = http_action_set_req_line;
11890 break;
11891 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011892 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011893 rule->action_ptr = http_action_set_req_line;
11894 break;
11895 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011896 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011897 rule->action_ptr = http_action_set_req_line;
11898 break;
11899 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011900 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011901 rule->action_ptr = http_action_set_req_line;
11902 break;
11903 default:
11904 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011905 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011906 }
11907
11908 if (!*args[cur_arg] ||
11909 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11910 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011911 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011912 }
11913
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011914 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011915 px->conf.args.ctx = ARGC_HRQ;
11916 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
11917 (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 +010011918 return ACT_RET_PRS_ERR;
11919 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011920
11921 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011922 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011923}
11924
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011925/* parse set-status action:
11926 * This action accepts a single argument of type int representing
11927 * an http status code. It returns ACT_RET_PRS_OK on success,
11928 * ACT_RET_PRS_ERR on error.
11929 */
11930enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11931 struct act_rule *rule, char **err)
11932{
11933 char *error;
11934
Thierry FOURNIER42148732015-09-02 17:17:33 +020011935 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011936 rule->action_ptr = action_http_set_status;
11937
11938 /* Check if an argument is available */
11939 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011940 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011941 return ACT_RET_PRS_ERR;
11942 }
11943
11944 /* convert status code as integer */
11945 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11946 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11947 memprintf(err, "expects an integer status code between 100 and 999");
11948 return ACT_RET_PRS_ERR;
11949 }
11950
11951 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011952
11953 /* set custom reason string */
11954 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11955 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11956 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11957 (*orig_arg)++;
11958 rule->arg.status.reason = strdup(args[*orig_arg]);
11959 (*orig_arg)++;
11960 }
11961
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011962 return ACT_RET_PRS_OK;
11963}
11964
Willy Tarreau53275e82017-11-24 07:52:01 +010011965/* This function executes the "reject" HTTP action. It clears the request and
11966 * response buffer without sending any response. It can be useful as an HTTP
11967 * alternative to the silent-drop action to defend against DoS attacks, and may
11968 * also be used with HTTP/2 to close a connection instead of just a stream.
11969 * The txn status is unchanged, indicating no response was sent. The termination
11970 * flags will indicate "PR". It always returns ACT_RET_STOP.
11971 */
11972enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
11973 struct session *sess, struct stream *s, int flags)
11974{
11975 channel_abort(&s->req);
11976 channel_abort(&s->res);
11977 s->req.analysers = 0;
11978 s->res.analysers = 0;
11979
11980 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
11981 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
11982 if (sess->listener && sess->listener->counters)
11983 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
11984
11985 if (!(s->flags & SF_ERR_MASK))
11986 s->flags |= SF_ERR_PRXCOND;
11987 if (!(s->flags & SF_FINST_MASK))
11988 s->flags |= SF_FINST_R;
11989
11990 return ACT_RET_CONT;
11991}
11992
11993/* parse the "reject" action:
11994 * This action takes no argument and returns ACT_RET_PRS_OK on success,
11995 * ACT_RET_PRS_ERR on error.
11996 */
11997enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
11998 struct act_rule *rule, char **err)
11999{
12000 rule->action = ACT_CUSTOM;
12001 rule->action_ptr = http_action_reject;
12002 return ACT_RET_PRS_OK;
12003}
12004
Willy Tarreaua9083d02015-05-08 15:27:59 +020012005/* This function executes the "capture" action. It executes a fetch expression,
12006 * turns the result into a string and puts it in a capture slot. It always
12007 * returns 1. If an error occurs the action is cancelled, but the rule
12008 * processing continues.
12009 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012010enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012011 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012012{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012013 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012014 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012015 char **cap = s->req_cap;
12016 int len;
12017
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012018 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 +020012019 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012020 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012021
12022 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012023 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012024
12025 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012026 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012027
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012028 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012029 if (len > h->len)
12030 len = h->len;
12031
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012032 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012033 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012034 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012035}
12036
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012037/* This function executes the "capture" action and store the result in a
12038 * capture slot if exists. It executes a fetch expression, turns the result
12039 * into a string and puts it in a capture slot. It always returns 1. If an
12040 * error occurs the action is cancelled, but the rule processing continues.
12041 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012042enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012043 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012044{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012045 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012046 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012047 char **cap = s->req_cap;
12048 struct proxy *fe = strm_fe(s);
12049 int len;
12050 int i;
12051
12052 /* Look for the original configuration. */
12053 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012054 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012055 i--, h = h->next);
12056 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012057 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012058
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012059 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 +020012060 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012061 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012062
12063 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012064 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012065
12066 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012067 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012068
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012069 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012070 if (len > h->len)
12071 len = h->len;
12072
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012073 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012074 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012075 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012076}
12077
Christopher Faulet29730ba2017-09-18 15:26:32 +020012078/* Check an "http-request capture" action.
12079 *
12080 * The function returns 1 in success case, otherwise, it returns 0 and err is
12081 * filled.
12082 */
12083int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12084{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012085 if (rule->action_ptr != http_action_req_capture_by_id)
12086 return 1;
12087
Christopher Faulet29730ba2017-09-18 15:26:32 +020012088 if (rule->arg.capid.idx >= px->nb_req_cap) {
12089 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12090 rule->arg.capid.idx);
12091 return 0;
12092 }
12093
12094 return 1;
12095}
12096
Willy Tarreaua9083d02015-05-08 15:27:59 +020012097/* parse an "http-request capture" action. It takes a single argument which is
12098 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012099 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012100 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012101 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012102enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12103 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012104{
12105 struct sample_expr *expr;
12106 struct cap_hdr *hdr;
12107 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012108 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012109
12110 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12111 if (strcmp(args[cur_arg], "if") == 0 ||
12112 strcmp(args[cur_arg], "unless") == 0)
12113 break;
12114
12115 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012116 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012117 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012118 }
12119
Willy Tarreaua9083d02015-05-08 15:27:59 +020012120 cur_arg = *orig_arg;
12121 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12122 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012123 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012124
12125 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12126 memprintf(err,
12127 "fetch method '%s' extracts information from '%s', none of which is available here",
12128 args[cur_arg-1], sample_src_names(expr->fetch->use));
12129 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012130 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012131 }
12132
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012133 if (!args[cur_arg] || !*args[cur_arg]) {
12134 memprintf(err, "expects 'len or 'id'");
12135 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012136 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012137 }
12138
Willy Tarreaua9083d02015-05-08 15:27:59 +020012139 if (strcmp(args[cur_arg], "len") == 0) {
12140 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012141
12142 if (!(px->cap & PR_CAP_FE)) {
12143 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012144 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012145 }
12146
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012147 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012148
Willy Tarreaua9083d02015-05-08 15:27:59 +020012149 if (!args[cur_arg]) {
12150 memprintf(err, "missing length value");
12151 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012152 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012153 }
12154 /* we copy the table name for now, it will be resolved later */
12155 len = atoi(args[cur_arg]);
12156 if (len <= 0) {
12157 memprintf(err, "length must be > 0");
12158 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012159 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012160 }
12161 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012162
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012163 if (!len) {
12164 memprintf(err, "a positive 'len' argument is mandatory");
12165 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012166 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012167 }
12168
Vincent Bernat02779b62016-04-03 13:48:43 +020012169 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012170 hdr->next = px->req_cap;
12171 hdr->name = NULL; /* not a header capture */
12172 hdr->namelen = 0;
12173 hdr->len = len;
12174 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12175 hdr->index = px->nb_req_cap++;
12176
12177 px->req_cap = hdr;
12178 px->to_log |= LW_REQHDR;
12179
Thierry FOURNIER42148732015-09-02 17:17:33 +020012180 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012181 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012182 rule->arg.cap.expr = expr;
12183 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012184 }
12185
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012186 else if (strcmp(args[cur_arg], "id") == 0) {
12187 int id;
12188 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012189
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012190 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012191
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012192 if (!args[cur_arg]) {
12193 memprintf(err, "missing id value");
12194 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012195 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012196 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012197
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012198 id = strtol(args[cur_arg], &error, 10);
12199 if (*error != '\0') {
12200 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12201 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012202 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012203 }
12204 cur_arg++;
12205
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012206 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012207
Thierry FOURNIER42148732015-09-02 17:17:33 +020012208 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012209 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012210 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012211 rule->arg.capid.expr = expr;
12212 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012213 }
12214
12215 else {
12216 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12217 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012218 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012219 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012220
12221 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012222 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012223}
12224
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012225/* This function executes the "capture" action and store the result in a
12226 * capture slot if exists. It executes a fetch expression, turns the result
12227 * into a string and puts it in a capture slot. It always returns 1. If an
12228 * error occurs the action is cancelled, but the rule processing continues.
12229 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012230enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012231 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012232{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012233 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012234 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012235 char **cap = s->res_cap;
12236 struct proxy *fe = strm_fe(s);
12237 int len;
12238 int i;
12239
12240 /* Look for the original configuration. */
12241 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012242 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012243 i--, h = h->next);
12244 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012245 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012246
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012247 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 +020012248 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012249 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012250
12251 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012252 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012253
12254 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012255 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012256
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012257 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012258 if (len > h->len)
12259 len = h->len;
12260
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012261 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012262 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012263 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012264}
12265
Christopher Faulet29730ba2017-09-18 15:26:32 +020012266/* Check an "http-response capture" action.
12267 *
12268 * The function returns 1 in success case, otherwise, it returns 0 and err is
12269 * filled.
12270 */
12271int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12272{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012273 if (rule->action_ptr != http_action_res_capture_by_id)
12274 return 1;
12275
Christopher Faulet29730ba2017-09-18 15:26:32 +020012276 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12277 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12278 rule->arg.capid.idx);
12279 return 0;
12280 }
12281
12282 return 1;
12283}
12284
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012285/* parse an "http-response capture" action. It takes a single argument which is
12286 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12287 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012288 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012289 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012290enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12291 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012292{
12293 struct sample_expr *expr;
12294 int cur_arg;
12295 int id;
12296 char *error;
12297
12298 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12299 if (strcmp(args[cur_arg], "if") == 0 ||
12300 strcmp(args[cur_arg], "unless") == 0)
12301 break;
12302
12303 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012304 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012305 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012306 }
12307
12308 cur_arg = *orig_arg;
12309 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12310 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012311 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012312
12313 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12314 memprintf(err,
12315 "fetch method '%s' extracts information from '%s', none of which is available here",
12316 args[cur_arg-1], sample_src_names(expr->fetch->use));
12317 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012318 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012319 }
12320
12321 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012322 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012323 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012324 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012325 }
12326
12327 if (strcmp(args[cur_arg], "id") != 0) {
12328 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12329 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012330 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012331 }
12332
12333 cur_arg++;
12334
12335 if (!args[cur_arg]) {
12336 memprintf(err, "missing id value");
12337 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012338 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012339 }
12340
12341 id = strtol(args[cur_arg], &error, 10);
12342 if (*error != '\0') {
12343 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12344 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012345 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012346 }
12347 cur_arg++;
12348
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012349 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012350
Thierry FOURNIER42148732015-09-02 17:17:33 +020012351 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012352 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012353 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012354 rule->arg.capid.expr = expr;
12355 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012356
12357 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012358 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012359}
12360
William Lallemand73025dd2014-04-24 14:38:37 +020012361/*
12362 * Return the struct http_req_action_kw associated to a keyword.
12363 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012364struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012365{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012366 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012367}
12368
12369/*
12370 * Return the struct http_res_action_kw associated to a keyword.
12371 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012372struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012373{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012374 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012375}
12376
Willy Tarreau12207b32016-11-22 19:48:51 +010012377
12378/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12379 * now.
12380 */
12381static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12382{
12383 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12384 return 1;
12385
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012386 if (*args[2]) {
12387 struct proxy *px;
12388
12389 px = proxy_find_by_name(args[2], 0, 0);
12390 if (px)
12391 appctx->ctx.errors.iid = px->uuid;
12392 else
12393 appctx->ctx.errors.iid = atoi(args[2]);
12394
12395 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012396 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012397 appctx->ctx.cli.msg = "No such proxy.\n";
12398 appctx->st0 = CLI_ST_PRINT;
12399 return 1;
12400 }
12401 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012402 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012403 appctx->ctx.errors.iid = -1; // dump all proxies
12404
Willy Tarreau35069f82016-11-25 09:16:37 +010012405 appctx->ctx.errors.flag = 0;
12406 if (strcmp(args[3], "request") == 0)
12407 appctx->ctx.errors.flag |= 4; // ignore response
12408 else if (strcmp(args[3], "response") == 0)
12409 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012410 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012411 return 0;
12412}
12413
12414/* This function dumps all captured errors onto the stream interface's
12415 * read buffer. It returns 0 if the output buffer is full and it needs
12416 * to be called again, otherwise non-zero.
12417 */
12418static int cli_io_handler_show_errors(struct appctx *appctx)
12419{
12420 struct stream_interface *si = appctx->owner;
12421 extern const char *monthname[12];
12422
12423 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12424 return 1;
12425
12426 chunk_reset(&trash);
12427
12428 if (!appctx->ctx.errors.px) {
12429 /* the function had not been called yet, let's prepare the
12430 * buffer for a response.
12431 */
12432 struct tm tm;
12433
12434 get_localtime(date.tv_sec, &tm);
12435 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12436 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12437 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12438 error_snapshot_id);
12439
Willy Tarreau06d80a92017-10-19 14:32:15 +020012440 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012441 /* Socket buffer full. Let's try again later from the same point */
12442 si_applet_cant_put(si);
12443 return 0;
12444 }
12445
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012446 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012447 appctx->ctx.errors.bol = 0;
12448 appctx->ctx.errors.ptr = -1;
12449 }
12450
12451 /* we have two inner loops here, one for the proxy, the other one for
12452 * the buffer.
12453 */
12454 while (appctx->ctx.errors.px) {
12455 struct error_snapshot *es;
12456
Willy Tarreau35069f82016-11-25 09:16:37 +010012457 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012458 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012459 if (appctx->ctx.errors.flag & 2) // skip req
12460 goto next;
12461 }
12462 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012463 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012464 if (appctx->ctx.errors.flag & 4) // skip resp
12465 goto next;
12466 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012467
12468 if (!es->when.tv_sec)
12469 goto next;
12470
12471 if (appctx->ctx.errors.iid >= 0 &&
12472 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12473 es->oe->uuid != appctx->ctx.errors.iid)
12474 goto next;
12475
12476 if (appctx->ctx.errors.ptr < 0) {
12477 /* just print headers now */
12478
12479 char pn[INET6_ADDRSTRLEN];
12480 struct tm tm;
12481 int port;
12482
12483 get_localtime(es->when.tv_sec, &tm);
12484 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12485 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12486 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12487
12488 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12489 case AF_INET:
12490 case AF_INET6:
12491 port = get_host_port(&es->src);
12492 break;
12493 default:
12494 port = 0;
12495 }
12496
Willy Tarreau35069f82016-11-25 09:16:37 +010012497 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012498 case 0:
12499 chunk_appendf(&trash,
12500 " frontend %s (#%d): invalid request\n"
12501 " backend %s (#%d)",
12502 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12503 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12504 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12505 break;
12506 case 1:
12507 chunk_appendf(&trash,
12508 " backend %s (#%d): invalid response\n"
12509 " frontend %s (#%d)",
12510 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12511 es->oe->id, es->oe->uuid);
12512 break;
12513 }
12514
12515 chunk_appendf(&trash,
12516 ", server %s (#%d), event #%u\n"
12517 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012518 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012519 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12520 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12521 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12522 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12523 es->ev_id,
12524 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012525 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012526 es->m_clen, es->m_blen,
12527 es->b_flags, es->b_out, es->b_tot,
12528 es->len, es->b_wrap, es->pos);
12529
Willy Tarreau06d80a92017-10-19 14:32:15 +020012530 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012531 /* Socket buffer full. Let's try again later from the same point */
12532 si_applet_cant_put(si);
12533 return 0;
12534 }
12535 appctx->ctx.errors.ptr = 0;
12536 appctx->ctx.errors.sid = es->sid;
12537 }
12538
12539 if (appctx->ctx.errors.sid != es->sid) {
12540 /* the snapshot changed while we were dumping it */
12541 chunk_appendf(&trash,
12542 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012543 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012544 si_applet_cant_put(si);
12545 return 0;
12546 }
12547 goto next;
12548 }
12549
12550 /* OK, ptr >= 0, so we have to dump the current line */
12551 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12552 int newptr;
12553 int newline;
12554
12555 newline = appctx->ctx.errors.bol;
12556 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12557 if (newptr == appctx->ctx.errors.ptr)
12558 return 0;
12559
Willy Tarreau06d80a92017-10-19 14:32:15 +020012560 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012561 /* Socket buffer full. Let's try again later from the same point */
12562 si_applet_cant_put(si);
12563 return 0;
12564 }
12565 appctx->ctx.errors.ptr = newptr;
12566 appctx->ctx.errors.bol = newline;
12567 };
12568 next:
12569 appctx->ctx.errors.bol = 0;
12570 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012571 appctx->ctx.errors.flag ^= 1;
12572 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012573 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012574 }
12575
12576 /* dump complete */
12577 return 1;
12578}
12579
12580/* register cli keywords */
12581static struct cli_kw_list cli_kws = {{ },{
12582 { { "show", "errors", NULL },
12583 "show errors : report last request and response errors for each proxy",
12584 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12585 },
12586 {{},}
12587}};
12588
Willy Tarreau4a568972010-05-12 08:08:50 +020012589/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012590/* All supported ACL keywords must be declared here. */
12591/************************************************************************/
12592
12593/* Note: must not be declared <const> as its list will be overwritten.
12594 * Please take care of keeping this list alphabetically sorted.
12595 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012596static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012597 { "base", "base", PAT_MATCH_STR },
12598 { "base_beg", "base", PAT_MATCH_BEG },
12599 { "base_dir", "base", PAT_MATCH_DIR },
12600 { "base_dom", "base", PAT_MATCH_DOM },
12601 { "base_end", "base", PAT_MATCH_END },
12602 { "base_len", "base", PAT_MATCH_LEN },
12603 { "base_reg", "base", PAT_MATCH_REG },
12604 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012605
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012606 { "cook", "req.cook", PAT_MATCH_STR },
12607 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12608 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12609 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12610 { "cook_end", "req.cook", PAT_MATCH_END },
12611 { "cook_len", "req.cook", PAT_MATCH_LEN },
12612 { "cook_reg", "req.cook", PAT_MATCH_REG },
12613 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012614
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012615 { "hdr", "req.hdr", PAT_MATCH_STR },
12616 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12617 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12618 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12619 { "hdr_end", "req.hdr", PAT_MATCH_END },
12620 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12621 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12622 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012623
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012624 /* these two declarations uses strings with list storage (in place
12625 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12626 * and delete functions are relative to the list management. The parse
12627 * and match method are related to the corresponding fetch methods. This
12628 * is very particular ACL declaration mode.
12629 */
12630 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12631 { "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 +020012632
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012633 { "path", "path", PAT_MATCH_STR },
12634 { "path_beg", "path", PAT_MATCH_BEG },
12635 { "path_dir", "path", PAT_MATCH_DIR },
12636 { "path_dom", "path", PAT_MATCH_DOM },
12637 { "path_end", "path", PAT_MATCH_END },
12638 { "path_len", "path", PAT_MATCH_LEN },
12639 { "path_reg", "path", PAT_MATCH_REG },
12640 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012641
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012642 { "req_ver", "req.ver", PAT_MATCH_STR },
12643 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012644
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012645 { "scook", "res.cook", PAT_MATCH_STR },
12646 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12647 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12648 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12649 { "scook_end", "res.cook", PAT_MATCH_END },
12650 { "scook_len", "res.cook", PAT_MATCH_LEN },
12651 { "scook_reg", "res.cook", PAT_MATCH_REG },
12652 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012653
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012654 { "shdr", "res.hdr", PAT_MATCH_STR },
12655 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12656 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12657 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12658 { "shdr_end", "res.hdr", PAT_MATCH_END },
12659 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12660 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12661 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012662
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012663 { "url", "url", PAT_MATCH_STR },
12664 { "url_beg", "url", PAT_MATCH_BEG },
12665 { "url_dir", "url", PAT_MATCH_DIR },
12666 { "url_dom", "url", PAT_MATCH_DOM },
12667 { "url_end", "url", PAT_MATCH_END },
12668 { "url_len", "url", PAT_MATCH_LEN },
12669 { "url_reg", "url", PAT_MATCH_REG },
12670 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012671
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012672 { "urlp", "urlp", PAT_MATCH_STR },
12673 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12674 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12675 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12676 { "urlp_end", "urlp", PAT_MATCH_END },
12677 { "urlp_len", "urlp", PAT_MATCH_LEN },
12678 { "urlp_reg", "urlp", PAT_MATCH_REG },
12679 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012680
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012681 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012682}};
12683
12684/************************************************************************/
12685/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012686/************************************************************************/
12687/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012688static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012689 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012690 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012691 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12692
Willy Tarreau87b09662015-04-03 00:22:06 +020012693 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012694 { "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 +020012695
12696 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012697 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12698 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12699 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012700
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012701 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12702 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012703
Willy Tarreau409bcde2013-01-08 00:31:00 +010012704 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12705 * are only here to match the ACL's name, are request-only and are used
12706 * for ACL compatibility only.
12707 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012708 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12709 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012710 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12711 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012712
12713 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12714 * only here to match the ACL's name, are request-only and are used for
12715 * ACL compatibility only.
12716 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012717 { "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 +020012718 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012719 { "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 +020012720 { "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 +010012721
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012722 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012723 { "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 +010012724 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012725 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012726 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012727 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012728
12729 /* HTTP protocol on the request path */
12730 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012731 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012732
12733 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012734 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12735 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012736
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012737 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012738 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12739 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012740 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012741
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012742 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012743 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12744
Willy Tarreau18ed2562013-01-14 15:56:36 +010012745 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012746 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12747 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012748
Willy Tarreau18ed2562013-01-14 15:56:36 +010012749 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012750 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012751 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12752 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012753
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012754 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012755 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012756 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012757 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012758 { "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 +010012759 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012760 { "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 +010012761
12762 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012763 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012764 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12765 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012766
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012767 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012768 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012769 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012770 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012771 { "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 +010012772 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012773 { "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 +010012774
Willy Tarreau409bcde2013-01-08 00:31:00 +010012775 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012776 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012777 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12778 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012779 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012780
12781 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012782 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012783 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012784 { "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 +020012785 { "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 +010012786
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012787 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012788 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012789 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012790 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012791 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012792 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012793 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012794 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12795 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012796 { "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 +010012797 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012798}};
12799
Willy Tarreau8797c062007-05-07 00:55:35 +020012800
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012801/************************************************************************/
12802/* All supported converter keywords must be declared here. */
12803/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012804/* Note: must not be declared <const> as its list will be overwritten */
12805static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012806 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012807 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012808 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12809 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012810 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012811 { NULL, NULL, 0, 0, 0 },
12812}};
12813
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012814
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012815/************************************************************************/
12816/* All supported http-request action keywords must be declared here. */
12817/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012818struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012819 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012820 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012821 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012822 { "set-method", parse_set_req_line },
12823 { "set-path", parse_set_req_line },
12824 { "set-query", parse_set_req_line },
12825 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012826 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012827 }
12828};
12829
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012830struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012831 .kw = {
12832 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012833 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012834 { NULL, NULL }
12835 }
12836};
12837
Willy Tarreau8797c062007-05-07 00:55:35 +020012838__attribute__((constructor))
12839static void __http_protocol_init(void)
12840{
12841 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012842 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012843 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012844 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012845 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012846 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012847}
12848
12849
Willy Tarreau58f10d72006-12-04 02:26:12 +010012850/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012851 * Local variables:
12852 * c-indent-level: 8
12853 * c-basic-offset: 8
12854 * End:
12855 */