blob: 04771b78d80f04690205fbe4421994b3a017158a [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 Tarreaud787e662009-07-07 10:14:51 +02002166 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002167 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002168 req->analyse_exp = TICK_ETERNITY;
2169 return 1;
2170
2171 return_bad_req:
2172 /* We centralize bad requests processing here */
2173 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2174 /* we detected a parsing error. We want to archive this request
2175 * in the dedicated proxy area for later troubleshooting.
2176 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002177 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002178 }
2179
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002180 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002181 txn->req.msg_state = HTTP_MSG_ERROR;
2182 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002183 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002184
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002185 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002186 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002187 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002188
2189 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002190 if (!(s->flags & SF_ERR_MASK))
2191 s->flags |= SF_ERR_PRXCOND;
2192 if (!(s->flags & SF_FINST_MASK))
2193 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002194
Christopher Faulet0184ea72017-01-05 14:06:34 +01002195 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002196 req->analyse_exp = TICK_ETERNITY;
2197 return 0;
2198}
2199
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002200
Willy Tarreau347a35d2013-11-22 17:51:09 +01002201/* This function prepares an applet to handle the stats. It can deal with the
2202 * "100-continue" expectation, check that admin rules are met for POST requests,
2203 * and program a response message if something was unexpected. It cannot fail
2204 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002205 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002206 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002207 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002208 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002209int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002210{
2211 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002212 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002213 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002214 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002215 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002216 struct uri_auth *uri_auth = s->be->uri_auth;
2217 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002218 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002219
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002220 appctx = si_appctx(si);
2221 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2222 appctx->st1 = appctx->st2 = 0;
2223 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2224 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002225 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002226 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002227
2228 uri = msg->chn->buf->p + msg->sl.rq.u;
2229 lookup = uri + uri_auth->uri_len;
2230
2231 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2232 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002233 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002234 break;
2235 }
2236 }
2237
2238 if (uri_auth->refresh) {
2239 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2240 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002241 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002242 break;
2243 }
2244 }
2245 }
2246
2247 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2248 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002249 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002250 break;
2251 }
2252 }
2253
Willy Tarreau1e62df92016-01-11 18:57:53 +01002254 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2255 if (memcmp(h, ";typed", 6) == 0) {
2256 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2257 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2258 break;
2259 }
2260 }
2261
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002262 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2263 if (memcmp(h, ";st=", 4) == 0) {
2264 int i;
2265 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002266 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002267 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2268 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002269 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002270 break;
2271 }
2272 }
2273 break;
2274 }
2275 }
2276
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002277 appctx->ctx.stats.scope_str = 0;
2278 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002279 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2280 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2281 int itx = 0;
2282 const char *h2;
2283 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2284 const char *err;
2285
2286 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2287 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002288 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002289 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2290 itx++;
2291 h++;
2292 }
2293
2294 if (itx > STAT_SCOPE_TXT_MAXLEN)
2295 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002296 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002297
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002298 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002299 memcpy(scope_txt, h2, itx);
2300 scope_txt[itx] = '\0';
2301 err = invalid_char(scope_txt);
2302 if (err) {
2303 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002304 appctx->ctx.stats.scope_str = 0;
2305 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002306 }
2307 break;
2308 }
2309 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002310
2311 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002312 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002313 int ret = 1;
2314
2315 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002316 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 +01002317 ret = acl_pass(ret);
2318 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2319 ret = !ret;
2320 }
2321
2322 if (ret) {
2323 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002325 break;
2326 }
2327 }
2328
2329 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002330 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002331 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002332 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002333 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2334 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002335 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002336 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002337 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002338 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2339 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002340 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002341 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002342 else {
2343 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002345 }
2346
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002347 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002348 return 1;
2349}
2350
Lukas Tribus67db8df2013-06-23 17:37:13 +02002351/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2352 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2353 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002354void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002355{
2356#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002357 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002358 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2359#endif
2360#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002361 if (from->ss_family == AF_INET6) {
2362 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002363 /* v4-mapped addresses need IP_TOS */
2364 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2365 else
2366 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2367 }
2368#endif
2369}
2370
Willy Tarreau87b09662015-04-03 00:22:06 +02002371int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002372 const char* name, unsigned int name_len,
2373 const char *str, struct my_regex *re,
2374 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002375{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002376 struct hdr_ctx ctx;
2377 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002378 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002379 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2380 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002381 struct chunk *output = get_trash_chunk();
2382
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002383 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002384
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002385 /* Choose the header browsing function. */
2386 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002387 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002388 http_find_hdr_func = http_find_header2;
2389 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002390 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002391 http_find_hdr_func = http_find_full_header2;
2392 break;
2393 default: /* impossible */
2394 return -1;
2395 }
2396
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002397 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2398 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002399 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002400 char *val = ctx.line + ctx.val;
2401 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002402
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002403 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2404 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002405
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002406 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002407 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002408 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002409
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002410 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002411
2412 hdr->len += delta;
2413 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002414
2415 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002416 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002417 }
2418
2419 return 0;
2420}
2421
Willy Tarreau87b09662015-04-03 00:22:06 +02002422static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002423 const char* name, unsigned int name_len,
2424 struct list *fmt, struct my_regex *re,
2425 int action)
2426{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002427 struct chunk *replace;
2428 int ret = -1;
2429
2430 replace = alloc_trash_chunk();
2431 if (!replace)
2432 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002433
2434 replace->len = build_logline(s, replace->str, replace->size, fmt);
2435 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002436 goto leave;
2437
2438 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002439
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002440 leave:
2441 free_trash_chunk(replace);
2442 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002443}
2444
Willy Tarreau87b09662015-04-03 00:22:06 +02002445/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002446 * transaction <txn>. Returns the verdict of the first rule that prevents
2447 * further processing of the request (auth, deny, ...), and defaults to
2448 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2449 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002450 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2451 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2452 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002453 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002454enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002455http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002456{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002457 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002458 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002459 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002460 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002461 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002462 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002463 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002464 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002465
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002466 /* If "the current_rule_list" match the executed rule list, we are in
2467 * resume condition. If a resume is needed it is always in the action
2468 * and never in the ACL or converters. In this case, we initialise the
2469 * current rule, and go to the action execution point.
2470 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002471 if (s->current_rule) {
2472 rule = s->current_rule;
2473 s->current_rule = NULL;
2474 if (s->current_rule_list == rules)
2475 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002476 }
2477 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002478
Willy Tarreauff011f22011-01-06 17:51:27 +01002479 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002480
Willy Tarreau96257ec2012-12-27 10:46:37 +01002481 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002482 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002483 int ret;
2484
Willy Tarreau192252e2015-04-04 01:47:55 +02002485 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002486 ret = acl_pass(ret);
2487
Willy Tarreauff011f22011-01-06 17:51:27 +01002488 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002489 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002490
2491 if (!ret) /* condition not matched */
2492 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002493 }
2494
Willy Tarreauacc98002015-09-27 23:34:39 +02002495 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002496resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002497 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002498 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002499 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002500
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002501 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002502 if (deny_status)
2503 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002504 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002505
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002506 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002507 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002508 if (deny_status)
2509 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002510 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002511
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002512 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002513 /* Auth might be performed on regular http-req rules as well as on stats */
2514 auth_realm = rule->arg.auth.realm;
2515 if (!auth_realm) {
2516 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2517 auth_realm = STATS_DEFAULT_REALM;
2518 else
2519 auth_realm = px->id;
2520 }
2521 /* send 401/407 depending on whether we use a proxy or not. We still
2522 * count one error, because normal browsing won't significantly
2523 * increase the counter but brute force attempts will.
2524 */
2525 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2526 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002527 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002528 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002529 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002530
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002531 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002532 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2533 return HTTP_RULE_RES_BADREQ;
2534 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002535
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002536 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002537 s->task->nice = rule->arg.nice;
2538 break;
2539
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002540 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002541 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002542 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002543 break;
2544
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002545 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002546#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002547 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002548 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002549#endif
2550 break;
2551
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002552 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002553 s->logs.level = rule->arg.loglevel;
2554 break;
2555
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002556 case ACT_HTTP_REPLACE_HDR:
2557 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002558 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2559 rule->arg.hdr_add.name_len,
2560 &rule->arg.hdr_add.fmt,
2561 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002562 return HTTP_RULE_RES_BADREQ;
2563 break;
2564
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002565 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002566 ctx.idx = 0;
2567 /* remove all occurrences of the header */
2568 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2569 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2570 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002571 }
Willy Tarreau85603282015-01-21 20:39:27 +01002572 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002574 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002575 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002576 /* The scope of the trash buffer must be limited to this function. The
2577 * build_logline() function can execute a lot of other function which
2578 * can use the trash buffer. So for limiting the scope of this global
2579 * buffer, we build first the header value using build_logline, and
2580 * after we store the header name.
2581 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002582 struct chunk *replace;
2583
2584 replace = alloc_trash_chunk();
2585 if (!replace)
2586 return HTTP_RULE_RES_BADREQ;
2587
Thierry Fournier4b788f72016-06-01 13:35:36 +02002588 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002589 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2590 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2591 replace->str[rule->arg.hdr_add.name_len] = ':';
2592 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2593 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002595 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002596 /* remove all occurrences of the header */
2597 ctx.idx = 0;
2598 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2599 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2600 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2601 }
2602 }
2603
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002604 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2605
2606 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002607 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002608 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002610 case ACT_HTTP_DEL_ACL:
2611 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002612 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002613 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002614
2615 /* collect reference */
2616 ref = pat_ref_lookup(rule->arg.map.ref);
2617 if (!ref)
2618 continue;
2619
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002620 /* allocate key */
2621 key = alloc_trash_chunk();
2622 if (!key)
2623 return HTTP_RULE_RES_BADREQ;
2624
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002625 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002626 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2627 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002628
2629 /* perform update */
2630 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002631 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002632 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002633 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002634
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002635 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002636 break;
2637 }
2638
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002639 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002640 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002641 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002642
2643 /* collect reference */
2644 ref = pat_ref_lookup(rule->arg.map.ref);
2645 if (!ref)
2646 continue;
2647
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002648 /* allocate key */
2649 key = alloc_trash_chunk();
2650 if (!key)
2651 return HTTP_RULE_RES_BADREQ;
2652
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002653 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002654 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2655 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002656
2657 /* perform update */
2658 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002659 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002660 if (pat_ref_find_elt(ref, key->str) == NULL)
2661 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002662 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002663
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002664 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002665 break;
2666 }
2667
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002668 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002669 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002670 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002671
2672 /* collect reference */
2673 ref = pat_ref_lookup(rule->arg.map.ref);
2674 if (!ref)
2675 continue;
2676
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002677 /* allocate key */
2678 key = alloc_trash_chunk();
2679 if (!key)
2680 return HTTP_RULE_RES_BADREQ;
2681
2682 /* allocate value */
2683 value = alloc_trash_chunk();
2684 if (!value) {
2685 free_trash_chunk(key);
2686 return HTTP_RULE_RES_BADREQ;
2687 }
2688
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002689 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002690 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2691 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002692
2693 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002694 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2695 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002696
2697 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002698 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002699 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002700 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002701 else
2702 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002703 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002704
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002705 free_trash_chunk(key);
2706 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002707 break;
2708 }
William Lallemand73025dd2014-04-24 14:38:37 +02002709
Thierry FOURNIER42148732015-09-02 17:17:33 +02002710 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02002711 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
2712 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002713
Willy Tarreauacc98002015-09-27 23:34:39 +02002714 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002715 case ACT_RET_ERR:
2716 case ACT_RET_CONT:
2717 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002718 case ACT_RET_STOP:
2719 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002720 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002721 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002722 return HTTP_RULE_RES_YIELD;
2723 }
William Lallemand73025dd2014-04-24 14:38:37 +02002724 break;
2725
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002726 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002727 /* Note: only the first valid tracking parameter of each
2728 * applies.
2729 */
2730
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002731 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002732 struct stktable *t;
2733 struct stksess *ts;
2734 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002735 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002736
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002737 t = rule->arg.trk_ctr.table.t;
2738 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 +02002739
2740 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002741 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002742
2743 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002744 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2745 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2746 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002747 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002748
Emeric Brun819fc6f2017-06-13 19:37:32 +02002749 if (ptr1)
2750 stktable_data_cast(ptr1, http_req_cnt)++;
2751
2752 if (ptr2)
2753 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2754 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2755
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002756 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002757 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002758
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002759 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002760 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002761 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002762 }
2763 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002764 break;
2765
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002766 /* other flags exists, but normaly, they never be matched. */
2767 default:
2768 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002769 }
2770 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002771
2772 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002773 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002774}
2775
Willy Tarreau71241ab2012-12-27 11:30:54 +01002776
Willy Tarreau51d861a2015-05-22 17:30:48 +02002777/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2778 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2779 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2780 * is returned, the process can continue the evaluation of next rule list. If
2781 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2782 * is returned, it means the operation could not be processed and a server error
2783 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2784 * deny rule. If *YIELD is returned, the caller must call again the function
2785 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002786 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002787static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002788http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002789{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002790 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002791 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002792 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002793 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002794 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002795 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002796
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002797 /* If "the current_rule_list" match the executed rule list, we are in
2798 * resume condition. If a resume is needed it is always in the action
2799 * and never in the ACL or converters. In this case, we initialise the
2800 * current rule, and go to the action execution point.
2801 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002802 if (s->current_rule) {
2803 rule = s->current_rule;
2804 s->current_rule = NULL;
2805 if (s->current_rule_list == rules)
2806 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002807 }
2808 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002809
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002810 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002811
2812 /* check optional condition */
2813 if (rule->cond) {
2814 int ret;
2815
Willy Tarreau192252e2015-04-04 01:47:55 +02002816 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002817 ret = acl_pass(ret);
2818
2819 if (rule->cond->pol == ACL_COND_UNLESS)
2820 ret = !ret;
2821
2822 if (!ret) /* condition not matched */
2823 continue;
2824 }
2825
Willy Tarreauacc98002015-09-27 23:34:39 +02002826 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002827resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002828 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002829 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002830 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002831
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002832 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002833 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002834 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002835
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002836 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002837 s->task->nice = rule->arg.nice;
2838 break;
2839
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002840 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002841 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002842 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002843 break;
2844
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002845 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002846#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002847 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002848 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002849#endif
2850 break;
2851
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002852 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002853 s->logs.level = rule->arg.loglevel;
2854 break;
2855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002856 case ACT_HTTP_REPLACE_HDR:
2857 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002858 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2859 rule->arg.hdr_add.name_len,
2860 &rule->arg.hdr_add.fmt,
2861 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002862 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002863 break;
2864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002865 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002866 ctx.idx = 0;
2867 /* remove all occurrences of the header */
2868 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2869 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2870 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2871 }
Willy Tarreau85603282015-01-21 20:39:27 +01002872 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002873
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002874 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002875 case ACT_HTTP_ADD_HDR: {
2876 struct chunk *replace;
2877
2878 replace = alloc_trash_chunk();
2879 if (!replace)
2880 return HTTP_RULE_RES_BADREQ;
2881
2882 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2883 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2884 replace->len = rule->arg.hdr_add.name_len;
2885 replace->str[replace->len++] = ':';
2886 replace->str[replace->len++] = ' ';
2887 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2888 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002889
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002890 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002891 /* remove all occurrences of the header */
2892 ctx.idx = 0;
2893 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2894 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2895 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2896 }
2897 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002898 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2899
2900 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002901 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002902 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002903
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002904 case ACT_HTTP_DEL_ACL:
2905 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002906 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002907 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002908
2909 /* collect reference */
2910 ref = pat_ref_lookup(rule->arg.map.ref);
2911 if (!ref)
2912 continue;
2913
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002914 /* allocate key */
2915 key = alloc_trash_chunk();
2916 if (!key)
2917 return HTTP_RULE_RES_BADREQ;
2918
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002919 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002920 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2921 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002922
2923 /* perform update */
2924 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002925 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002926 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002927 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002928
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002929 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002930 break;
2931 }
2932
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002933 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002934 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002935 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002936
2937 /* collect reference */
2938 ref = pat_ref_lookup(rule->arg.map.ref);
2939 if (!ref)
2940 continue;
2941
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002942 /* allocate key */
2943 key = alloc_trash_chunk();
2944 if (!key)
2945 return HTTP_RULE_RES_BADREQ;
2946
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002947 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002948 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2949 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002950
2951 /* perform update */
2952 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002953 if (pat_ref_find_elt(ref, key->str) == NULL)
2954 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002955
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002956 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002957 break;
2958 }
2959
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002960 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002961 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002962 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002963
2964 /* collect reference */
2965 ref = pat_ref_lookup(rule->arg.map.ref);
2966 if (!ref)
2967 continue;
2968
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002969 /* allocate key */
2970 key = alloc_trash_chunk();
2971 if (!key)
2972 return HTTP_RULE_RES_BADREQ;
2973
2974 /* allocate value */
2975 value = alloc_trash_chunk();
2976 if (!value) {
2977 free_trash_chunk(key);
2978 return HTTP_RULE_RES_BADREQ;
2979 }
2980
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002981 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002982 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2983 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002984
2985 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002986 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2987 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002988
2989 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002990 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002991 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002992 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002993 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002994 else
2995 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002996 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002997 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002998 free_trash_chunk(key);
2999 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003000 break;
3001 }
William Lallemand73025dd2014-04-24 14:38:37 +02003002
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003003 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003004 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3005 return HTTP_RULE_RES_BADREQ;
3006 return HTTP_RULE_RES_DONE;
3007
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003008 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3009 /* Note: only the first valid tracking parameter of each
3010 * applies.
3011 */
3012
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003013 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003014 struct stktable *t;
3015 struct stksess *ts;
3016 struct stktable_key *key;
3017 void *ptr;
3018
3019 t = rule->arg.trk_ctr.table.t;
3020 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3021
3022 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003023 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003024
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003025 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003026
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003027 /* let's count a new HTTP request as it's the first time we do it */
3028 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3029 if (ptr)
3030 stktable_data_cast(ptr, http_req_cnt)++;
3031
3032 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3033 if (ptr)
3034 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3035 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3036
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003037 /* When the client triggers a 4xx from the server, it's most often due
3038 * to a missing object or permission. These events should be tracked
3039 * because if they happen often, it may indicate a brute force or a
3040 * vulnerability scan. Normally this is done when receiving the response
3041 * but here we're tracking after this ought to have been done so we have
3042 * to do it on purpose.
3043 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003044 if ((unsigned)(txn->status - 400) < 100) {
3045 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3046 if (ptr)
3047 stktable_data_cast(ptr, http_err_cnt)++;
3048
3049 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3050 if (ptr)
3051 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3052 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3053 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003054
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003055 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003056
3057 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3058 if (sess->fe != s->be)
3059 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3060
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003061 }
3062 }
3063 break;
3064
Thierry FOURNIER42148732015-09-02 17:17:33 +02003065 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003066 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3067 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003068
Willy Tarreauacc98002015-09-27 23:34:39 +02003069 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003070 case ACT_RET_ERR:
3071 case ACT_RET_CONT:
3072 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003073 case ACT_RET_STOP:
3074 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003075 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003076 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003077 return HTTP_RULE_RES_YIELD;
3078 }
William Lallemand73025dd2014-04-24 14:38:37 +02003079 break;
3080
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003081 /* other flags exists, but normaly, they never be matched. */
3082 default:
3083 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003084 }
3085 }
3086
3087 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003088 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003089}
3090
3091
Willy Tarreau71241ab2012-12-27 11:30:54 +01003092/* Perform an HTTP redirect based on the information in <rule>. The function
3093 * returns non-zero on success, or zero in case of a, irrecoverable error such
3094 * as too large a request to build a valid response.
3095 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003096static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003097{
Willy Tarreaub329a312015-05-22 16:27:37 +02003098 struct http_msg *req = &txn->req;
3099 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003100 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003101 struct chunk *chunk;
3102 int ret = 0;
3103
3104 chunk = alloc_trash_chunk();
3105 if (!chunk)
3106 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003107
3108 /* build redirect message */
3109 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003110 case 308:
3111 msg_fmt = HTTP_308;
3112 break;
3113 case 307:
3114 msg_fmt = HTTP_307;
3115 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003116 case 303:
3117 msg_fmt = HTTP_303;
3118 break;
3119 case 301:
3120 msg_fmt = HTTP_301;
3121 break;
3122 case 302:
3123 default:
3124 msg_fmt = HTTP_302;
3125 break;
3126 }
3127
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003128 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3129 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003130
3131 switch(rule->type) {
3132 case REDIRECT_TYPE_SCHEME: {
3133 const char *path;
3134 const char *host;
3135 struct hdr_ctx ctx;
3136 int pathlen;
3137 int hostlen;
3138
3139 host = "";
3140 hostlen = 0;
3141 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003142 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003143 host = ctx.line + ctx.val;
3144 hostlen = ctx.vlen;
3145 }
3146
3147 path = http_get_path(txn);
3148 /* build message using path */
3149 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003150 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003151 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3152 int qs = 0;
3153 while (qs < pathlen) {
3154 if (path[qs] == '?') {
3155 pathlen = qs;
3156 break;
3157 }
3158 qs++;
3159 }
3160 }
3161 } else {
3162 path = "/";
3163 pathlen = 1;
3164 }
3165
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003166 if (rule->rdr_str) { /* this is an old "redirect" rule */
3167 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003168 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3169 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003170
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003171 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003172 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3173 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003174 }
3175 else {
3176 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003177 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003178
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003179 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003180 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3181 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003182 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003183 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003184 memcpy(chunk->str + chunk->len, "://", 3);
3185 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003186
3187 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003188 memcpy(chunk->str + chunk->len, host, hostlen);
3189 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003190
3191 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003192 memcpy(chunk->str + chunk->len, path, pathlen);
3193 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003194
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003195 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003196 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003197 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003198 if (chunk->len > chunk->size - 5)
3199 goto leave;
3200 chunk->str[chunk->len] = '/';
3201 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003202 }
3203
3204 break;
3205 }
3206 case REDIRECT_TYPE_PREFIX: {
3207 const char *path;
3208 int pathlen;
3209
3210 path = http_get_path(txn);
3211 /* build message using path */
3212 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003213 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003214 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3215 int qs = 0;
3216 while (qs < pathlen) {
3217 if (path[qs] == '?') {
3218 pathlen = qs;
3219 break;
3220 }
3221 qs++;
3222 }
3223 }
3224 } else {
3225 path = "/";
3226 pathlen = 1;
3227 }
3228
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003229 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003230 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3231 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003232
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003233 /* add prefix. Note that if prefix == "/", we don't want to
3234 * add anything, otherwise it makes it hard for the user to
3235 * configure a self-redirection.
3236 */
3237 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003238 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3239 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003240 }
3241 }
3242 else {
3243 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003244 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003245
3246 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003247 if (chunk->len + pathlen > chunk->size - 4)
3248 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003249 }
3250
3251 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003252 memcpy(chunk->str + chunk->len, path, pathlen);
3253 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003254
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003255 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003256 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003257 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003258 if (chunk->len > chunk->size - 5)
3259 goto leave;
3260 chunk->str[chunk->len] = '/';
3261 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003262 }
3263
3264 break;
3265 }
3266 case REDIRECT_TYPE_LOCATION:
3267 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003268 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003269 if (chunk->len + rule->rdr_len > chunk->size - 4)
3270 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003271
3272 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003273 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3274 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003275 }
3276 else {
3277 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003278 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003279
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003280 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003281 if (chunk->len > chunk->size - 4)
3282 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003283 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003284 break;
3285 }
3286
3287 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003288 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3289 chunk->len += 14;
3290 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3291 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003292 }
3293
Willy Tarreau19b14122017-02-28 09:48:11 +01003294 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003295 txn->status = rule->code;
3296 /* let's log the request time */
3297 s->logs.tv_request = now;
3298
Christopher Fauletbe821b92017-03-30 11:21:53 +02003299 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003300 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3301 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3302 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003303 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003304 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003305 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3306 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003307 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003308 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3309 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003310 }
3311 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003312 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3313 chunk->len += 4;
3314 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003315 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003316 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003317 bi_fast_delete(req->chn->buf, req->sov);
3318 req->next -= req->sov;
3319 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003320 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003321 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003322 req->msg_state = HTTP_MSG_CLOSED;
3323 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003324 /* Trim any possible response */
3325 res->chn->buf->i = 0;
3326 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003327 /* let the server side turn to SI_ST_CLO */
3328 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003329 } else {
3330 /* keep-alive not possible */
3331 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003332 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3333 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003334 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003335 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3336 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003337 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003338 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003339 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003340 }
3341
Willy Tarreaue7dff022015-04-03 01:14:29 +02003342 if (!(s->flags & SF_ERR_MASK))
3343 s->flags |= SF_ERR_LOCAL;
3344 if (!(s->flags & SF_FINST_MASK))
3345 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003346
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003347 ret = 1;
3348 leave:
3349 free_trash_chunk(chunk);
3350 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003351}
3352
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003353/* This stream analyser runs all HTTP request processing which is common to
3354 * frontends and backends, which means blocking ACLs, filters, connection-close,
3355 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003356 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003357 * either needs more data or wants to immediately abort the request (eg: deny,
3358 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003359 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003360int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003361{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003362 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003363 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003364 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003365 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003366 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003367 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003368 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003369 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003370
Willy Tarreau655dce92009-11-08 13:10:58 +01003371 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003372 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003373 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003374 }
3375
Willy Tarreau87b09662015-04-03 00:22:06 +02003376 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 +02003377 now_ms, __FUNCTION__,
3378 s,
3379 req,
3380 req->rex, req->wex,
3381 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003382 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003383 req->analysers);
3384
Willy Tarreau65410832014-04-28 21:25:43 +02003385 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003386 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003387
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003388 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003389 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003390 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003391
Willy Tarreau0b748332014-04-29 00:13:29 +02003392 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003393 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3394 goto return_prx_yield;
3395
Willy Tarreau0b748332014-04-29 00:13:29 +02003396 case HTTP_RULE_RES_CONT:
3397 case HTTP_RULE_RES_STOP: /* nothing to do */
3398 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003399
Willy Tarreau0b748332014-04-29 00:13:29 +02003400 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3401 if (txn->flags & TX_CLTARPIT)
3402 goto tarpit;
3403 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003404
Willy Tarreau0b748332014-04-29 00:13:29 +02003405 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3406 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003407
Willy Tarreau0b748332014-04-29 00:13:29 +02003408 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003409 goto done;
3410
Willy Tarreau0b748332014-04-29 00:13:29 +02003411 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3412 goto return_bad_req;
3413 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003414 }
3415
Olivier Houchardc2aae742017-09-22 18:26:28 +02003416 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3417 struct hdr_ctx ctx;
3418
3419 ctx.idx = 0;
3420 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3421 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3422 if (unlikely(http_header_add_tail2(&txn->req,
3423 &txn->hdr_idx, "Early-Data: 1",
3424 strlen("Early-Data: 1"))) < 0) {
3425 goto return_bad_req;
3426 }
3427 }
3428
3429 }
3430
Willy Tarreau52542592014-04-28 18:33:26 +02003431 /* OK at this stage, we know that the request was accepted according to
3432 * the http-request rules, we can check for the stats. Note that the
3433 * URI is detected *before* the req* rules in order not to be affected
3434 * by a possible reqrep, while they are processed *after* so that a
3435 * reqdeny can still block them. This clearly needs to change in 1.6!
3436 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003437 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003438 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003439 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003440 txn->status = 500;
3441 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003442 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003443
Willy Tarreaue7dff022015-04-03 01:14:29 +02003444 if (!(s->flags & SF_ERR_MASK))
3445 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003446 goto return_prx_cond;
3447 }
3448
3449 /* parse the whole stats request and extract the relevant information */
3450 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003451 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003452 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003453
Willy Tarreau0b748332014-04-29 00:13:29 +02003454 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3455 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003456
Willy Tarreau0b748332014-04-29 00:13:29 +02003457 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3458 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003459 }
3460
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003461 /* evaluate the req* rules except reqadd */
3462 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003463 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003464 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003465
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003466 if (txn->flags & TX_CLDENY)
3467 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003468
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003469 if (txn->flags & TX_CLTARPIT) {
3470 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003471 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003472 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003473 }
Willy Tarreau06619262006-12-17 08:37:22 +01003474
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003475 /* add request headers from the rule sets in the same order */
3476 list_for_each_entry(wl, &px->req_add, list) {
3477 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003478 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003479 ret = acl_pass(ret);
3480 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3481 ret = !ret;
3482 if (!ret)
3483 continue;
3484 }
3485
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003486 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003487 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003488 }
3489
Willy Tarreau52542592014-04-28 18:33:26 +02003490
3491 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003492 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3493 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003494 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003495 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003496 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003497
Willy Tarreaue7dff022015-04-03 01:14:29 +02003498 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3499 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3500 if (!(s->flags & SF_FINST_MASK))
3501 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003502
Willy Tarreau70730dd2014-04-24 18:06:27 +02003503 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003504 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3505 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003506 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003507 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003508 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003509
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003510 /* check whether we have some ACLs set to redirect this request */
3511 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003512 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003513 int ret;
3514
Willy Tarreau192252e2015-04-04 01:47:55 +02003515 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003516 ret = acl_pass(ret);
3517 if (rule->cond->pol == ACL_COND_UNLESS)
3518 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003519 if (!ret)
3520 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003521 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003522 if (!http_apply_redirect_rule(rule, s, txn))
3523 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003524 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003525 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003526
Willy Tarreau2be39392010-01-03 17:24:51 +01003527 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3528 * If this happens, then the data will not come immediately, so we must
3529 * send all what we have without waiting. Note that due to the small gain
3530 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003531 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003532 * itself once used.
3533 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003534 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003535
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003536 done: /* done with this analyser, continue with next ones that the calling
3537 * points will have set, if any.
3538 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003539 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003540 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3541 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003542 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003543
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003544 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003545 /* Allow cookie logging
3546 */
3547 if (s->be->cookie_name || sess->fe->capture_name)
3548 manage_client_side_cookies(s, req);
3549
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003550 /* When a connection is tarpitted, we use the tarpit timeout,
3551 * which may be the same as the connect timeout if unspecified.
3552 * If unset, then set it to zero because we really want it to
3553 * eventually expire. We build the tarpit as an analyser.
3554 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003555 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003556
3557 /* wipe the request out so that we can drop the connection early
3558 * if the client closes first.
3559 */
3560 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003561
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003562 txn->status = http_err_codes[deny_status];
3563
Christopher Faulet0184ea72017-01-05 14:06:34 +01003564 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003565 req->analysers |= AN_REQ_HTTP_TARPIT;
3566 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3567 if (!req->analyse_exp)
3568 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003569 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003570 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003571 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003572 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003573 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003574 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003575 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003576
3577 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003578
3579 /* Allow cookie logging
3580 */
3581 if (s->be->cookie_name || sess->fe->capture_name)
3582 manage_client_side_cookies(s, req);
3583
Willy Tarreau0b748332014-04-29 00:13:29 +02003584 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003585 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003586 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003587 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003588 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003589 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003590 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003591 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003592 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003593 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003594 goto return_prx_cond;
3595
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003596 return_bad_req:
3597 /* We centralize bad requests processing here */
3598 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3599 /* we detected a parsing error. We want to archive this request
3600 * in the dedicated proxy area for later troubleshooting.
3601 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003602 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003603 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003604
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003605 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003606 txn->req.msg_state = HTTP_MSG_ERROR;
3607 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003608 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003609
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003610 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003611 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003612 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003613
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003614 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003615 if (!(s->flags & SF_ERR_MASK))
3616 s->flags |= SF_ERR_PRXCOND;
3617 if (!(s->flags & SF_FINST_MASK))
3618 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003619
Christopher Faulet0184ea72017-01-05 14:06:34 +01003620 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003621 req->analyse_exp = TICK_ETERNITY;
3622 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003623
3624 return_prx_yield:
3625 channel_dont_connect(req);
3626 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003627}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003628
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003629/* This function performs all the processing enabled for the current request.
3630 * It returns 1 if the processing can continue on next analysers, or zero if it
3631 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003632 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003633 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003634int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003635{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003636 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003637 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003638 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003639 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003640
Willy Tarreau655dce92009-11-08 13:10:58 +01003641 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003642 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003643 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003644 return 0;
3645 }
3646
Willy Tarreau87b09662015-04-03 00:22:06 +02003647 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 +02003648 now_ms, __FUNCTION__,
3649 s,
3650 req,
3651 req->rex, req->wex,
3652 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003653 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003654 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003655
Willy Tarreau59234e92008-11-30 23:51:27 +01003656 /*
3657 * Right now, we know that we have processed the entire headers
3658 * and that unwanted requests have been filtered out. We can do
3659 * whatever we want with the remaining request. Also, now we
3660 * may have separate values for ->fe, ->be.
3661 */
Willy Tarreau06619262006-12-17 08:37:22 +01003662
Willy Tarreau59234e92008-11-30 23:51:27 +01003663 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003664 * If HTTP PROXY is set we simply get remote server address parsing
3665 * incoming request. Note that this requires that a connection is
3666 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003667 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003668 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003669 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003670 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003671
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003672 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003673 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003674 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003675 txn->req.msg_state = HTTP_MSG_ERROR;
3676 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003677 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003678 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003679
Willy Tarreaue7dff022015-04-03 01:14:29 +02003680 if (!(s->flags & SF_ERR_MASK))
3681 s->flags |= SF_ERR_RESOURCE;
3682 if (!(s->flags & SF_FINST_MASK))
3683 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003684
3685 return 0;
3686 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003687
3688 path = http_get_path(txn);
3689 url2sa(req->buf->p + msg->sl.rq.u,
3690 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003691 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003692 /* if the path was found, we have to remove everything between
3693 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3694 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3695 * u_l characters by a single "/".
3696 */
3697 if (path) {
3698 char *cur_ptr = req->buf->p;
3699 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3700 int delta;
3701
3702 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3703 http_msg_move_end(&txn->req, delta);
3704 cur_end += delta;
3705 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3706 goto return_bad_req;
3707 }
3708 else {
3709 char *cur_ptr = req->buf->p;
3710 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3711 int delta;
3712
3713 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3714 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3715 http_msg_move_end(&txn->req, delta);
3716 cur_end += delta;
3717 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3718 goto return_bad_req;
3719 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003720 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003721
Willy Tarreau59234e92008-11-30 23:51:27 +01003722 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003723 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003724 * Note that doing so might move headers in the request, but
3725 * the fields will stay coherent and the URI will not move.
3726 * This should only be performed in the backend.
3727 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003728 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003729 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003730
William Lallemanda73203e2012-03-12 12:48:57 +01003731 /* add unique-id if "header-unique-id" is specified */
3732
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003733 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003734 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003735 goto return_bad_req;
3736 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003737 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003738 }
William Lallemanda73203e2012-03-12 12:48:57 +01003739
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003740 if (sess->fe->header_unique_id && s->unique_id) {
3741 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003742 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003743 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003744 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003745 goto return_bad_req;
3746 }
3747
Cyril Bontéb21570a2009-11-29 20:04:48 +01003748 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003749 * 9: add X-Forwarded-For if either the frontend or the backend
3750 * asks for it.
3751 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003752 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003753 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003754 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3755 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3756 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003757 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003758 /* The header is set to be added only if none is present
3759 * and we found it, so don't do anything.
3760 */
3761 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003762 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003763 /* Add an X-Forwarded-For header unless the source IP is
3764 * in the 'except' network range.
3765 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003766 if ((!sess->fe->except_mask.s_addr ||
3767 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3768 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003770 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003771 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003772 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003773 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003774 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003775
3776 /* Note: we rely on the backend to get the header name to be used for
3777 * x-forwarded-for, because the header is really meant for the backends.
3778 * However, if the backend did not specify any option, we have to rely
3779 * on the frontend's header name.
3780 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003781 if (s->be->fwdfor_hdr_len) {
3782 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003783 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003784 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003785 len = sess->fe->fwdfor_hdr_len;
3786 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003787 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003788 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 +01003789
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003790 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003791 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003792 }
3793 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003794 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003795 /* FIXME: for the sake of completeness, we should also support
3796 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003797 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003798 int len;
3799 char pn[INET6_ADDRSTRLEN];
3800 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003801 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003802 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003803
Willy Tarreau59234e92008-11-30 23:51:27 +01003804 /* Note: we rely on the backend to get the header name to be used for
3805 * x-forwarded-for, because the header is really meant for the backends.
3806 * However, if the backend did not specify any option, we have to rely
3807 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003808 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003809 if (s->be->fwdfor_hdr_len) {
3810 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003811 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003812 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003813 len = sess->fe->fwdfor_hdr_len;
3814 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003815 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003816 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003817
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003818 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003819 goto return_bad_req;
3820 }
3821 }
3822
3823 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003824 * 10: add X-Original-To if either the frontend or the backend
3825 * asks for it.
3826 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003827 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003828
3829 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003830 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003831 /* Add an X-Original-To header unless the destination IP is
3832 * in the 'except' network range.
3833 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003834 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003835
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003836 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003837 ((!sess->fe->except_mask_to.s_addr ||
3838 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3839 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003840 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003841 (((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 +02003842 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003843 int len;
3844 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003845 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003846
3847 /* Note: we rely on the backend to get the header name to be used for
3848 * x-original-to, because the header is really meant for the backends.
3849 * However, if the backend did not specify any option, we have to rely
3850 * on the frontend's header name.
3851 */
3852 if (s->be->orgto_hdr_len) {
3853 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003854 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003855 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003856 len = sess->fe->orgto_hdr_len;
3857 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003858 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003859 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 +02003860
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003861 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003862 goto return_bad_req;
3863 }
3864 }
3865 }
3866
Willy Tarreau50fc7772012-11-11 22:19:57 +01003867 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3868 * If an "Upgrade" token is found, the header is left untouched in order not to have
3869 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3870 * "Upgrade" is present in the Connection header.
3871 */
3872 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3873 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003874 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003875 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003876 unsigned int want_flags = 0;
3877
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003878 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003879 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003880 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003881 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003882 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003883 want_flags |= TX_CON_CLO_SET;
3884 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003885 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003886 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003887 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003888 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003889 want_flags |= TX_CON_KAL_SET;
3890 }
3891
3892 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003893 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003894 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003895
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003896
Willy Tarreau522d6c02009-12-06 18:49:18 +01003897 /* If we have no server assigned yet and we're balancing on url_param
3898 * with a POST request, we may be interested in checking the body for
3899 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003900 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003901 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003902 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003903 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003904 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003905 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003906 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003907
Christopher Fauletbe821b92017-03-30 11:21:53 +02003908 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3909 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003910#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003911 /* We expect some data from the client. Unless we know for sure
3912 * we already have a full request, we have to re-enable quick-ack
3913 * in case we previously disabled it, otherwise we might cause
3914 * the client to delay further data.
3915 */
3916 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3917 cli_conn && conn_ctrl_ready(cli_conn) &&
3918 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3919 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003920 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003921#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003922
Willy Tarreau59234e92008-11-30 23:51:27 +01003923 /*************************************************************
3924 * OK, that's finished for the headers. We have done what we *
3925 * could. Let's switch to the DATA state. *
3926 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003927 req->analyse_exp = TICK_ETERNITY;
3928 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003929
Willy Tarreau59234e92008-11-30 23:51:27 +01003930 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003931 /* OK let's go on with the BODY now */
3932 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003933
Willy Tarreau59234e92008-11-30 23:51:27 +01003934 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003935 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003936 /* we detected a parsing error. We want to archive this request
3937 * in the dedicated proxy area for later troubleshooting.
3938 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003939 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003940 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003941
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003942 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003943 txn->req.msg_state = HTTP_MSG_ERROR;
3944 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003945 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003946 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003947
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003948 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003949 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003950 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003951
Willy Tarreaue7dff022015-04-03 01:14:29 +02003952 if (!(s->flags & SF_ERR_MASK))
3953 s->flags |= SF_ERR_PRXCOND;
3954 if (!(s->flags & SF_FINST_MASK))
3955 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003956 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003957}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003958
Willy Tarreau60b85b02008-11-30 23:28:40 +01003959/* This function is an analyser which processes the HTTP tarpit. It always
3960 * returns zero, at the beginning because it prevents any other processing
3961 * from occurring, and at the end because it terminates the request.
3962 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003963int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003964{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003965 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003966
3967 /* This connection is being tarpitted. The CLIENT side has
3968 * already set the connect expiration date to the right
3969 * timeout. We just have to check that the client is still
3970 * there and that the timeout has not expired.
3971 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003972 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003973 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003974 !tick_is_expired(req->analyse_exp, now_ms))
3975 return 0;
3976
3977 /* We will set the queue timer to the time spent, just for
3978 * logging purposes. We fake a 500 server error, so that the
3979 * attacker will not suspect his connection has been tarpitted.
3980 * It will not cause trouble to the logs because we can exclude
3981 * the tarpitted connections by filtering on the 'PT' status flags.
3982 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003983 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3984
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003985 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003986 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003987
Christopher Faulet0184ea72017-01-05 14:06:34 +01003988 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003989 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003990
Willy Tarreaue7dff022015-04-03 01:14:29 +02003991 if (!(s->flags & SF_ERR_MASK))
3992 s->flags |= SF_ERR_PRXCOND;
3993 if (!(s->flags & SF_FINST_MASK))
3994 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003995 return 0;
3996}
3997
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003998/* This function is an analyser which waits for the HTTP request body. It waits
3999 * for either the buffer to be full, or the full advertised contents to have
4000 * reached the buffer. It must only be called after the standard HTTP request
4001 * processing has occurred, because it expects the request to be parsed and will
4002 * look for the Expect header. It may send a 100-Continue interim response. It
4003 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4004 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4005 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004006 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004007int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004008{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004009 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004010 struct http_txn *txn = s->txn;
4011 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004012
4013 /* We have to parse the HTTP request body to find any required data.
4014 * "balance url_param check_post" should have been the only way to get
4015 * into this. We were brought here after HTTP header analysis, so all
4016 * related structures are ready.
4017 */
4018
Willy Tarreau890988f2014-04-10 11:59:33 +02004019 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4020 /* This is the first call */
4021 if (msg->msg_state < HTTP_MSG_BODY)
4022 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004023
Willy Tarreau890988f2014-04-10 11:59:33 +02004024 if (msg->msg_state < HTTP_MSG_100_SENT) {
4025 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4026 * send an HTTP/1.1 100 Continue intermediate response.
4027 */
4028 if (msg->flags & HTTP_MSGF_VER_11) {
4029 struct hdr_ctx ctx;
4030 ctx.idx = 0;
4031 /* Expect is allowed in 1.1, look for it */
4032 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4033 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004034 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004035 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004036 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004037 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004038 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004039 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004040
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004041 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004042 * req->buf->p still points to the beginning of the message. We
4043 * must save the body in msg->next because it survives buffer
4044 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004045 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004046 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004047
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004048 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004049 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4050 else
4051 msg->msg_state = HTTP_MSG_DATA;
4052 }
4053
Willy Tarreau890988f2014-04-10 11:59:33 +02004054 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4055 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004056 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004057 goto missing_data;
4058
4059 /* OK we have everything we need now */
4060 goto http_end;
4061 }
4062
4063 /* OK here we're parsing a chunked-encoded message */
4064
Willy Tarreau522d6c02009-12-06 18:49:18 +01004065 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004066 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004067 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004068 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004069 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004070 unsigned int chunk;
4071 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004072
Willy Tarreau115acb92009-12-26 13:56:06 +01004073 if (!ret)
4074 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004075 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004076 msg->err_pos = req->buf->i + ret;
4077 if (msg->err_pos < 0)
4078 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004079 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004080 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004081 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004082
4083 msg->chunk_len = chunk;
4084 msg->body_len += chunk;
4085
4086 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004087 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004088 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004089 }
4090
Willy Tarreaud98cf932009-12-27 22:54:55 +01004091 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004092 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4093 * for at least a whole chunk or the whole content length bytes after
4094 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004095 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004096 if (msg->msg_state == HTTP_MSG_TRAILERS)
4097 goto http_end;
4098
Willy Tarreaue115b492015-05-01 23:05:14 +02004099 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004100 goto http_end;
4101
4102 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004103 /* we get here if we need to wait for more data. If the buffer is full,
4104 * we have the maximum we can expect.
4105 */
4106 if (buffer_full(req->buf, global.tune.maxrewrite))
4107 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004108
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004109 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004110 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004111 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004112
Willy Tarreaue7dff022015-04-03 01:14:29 +02004113 if (!(s->flags & SF_ERR_MASK))
4114 s->flags |= SF_ERR_CLITO;
4115 if (!(s->flags & SF_FINST_MASK))
4116 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004117 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004118 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004119
4120 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004121 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004122 /* Not enough data. We'll re-use the http-request
4123 * timeout here. Ideally, we should set the timeout
4124 * relative to the accept() date. We just set the
4125 * request timeout once at the beginning of the
4126 * request.
4127 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004128 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004129 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004130 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004131 return 0;
4132 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004133
4134 http_end:
4135 /* The situation will not evolve, so let's give up on the analysis. */
4136 s->logs.tv_request = now; /* update the request timer to reflect full request */
4137 req->analysers &= ~an_bit;
4138 req->analyse_exp = TICK_ETERNITY;
4139 return 1;
4140
4141 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004142 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004143 txn->req.msg_state = HTTP_MSG_ERROR;
4144 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004145 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004146
Willy Tarreaue7dff022015-04-03 01:14:29 +02004147 if (!(s->flags & SF_ERR_MASK))
4148 s->flags |= SF_ERR_PRXCOND;
4149 if (!(s->flags & SF_FINST_MASK))
4150 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004151
Willy Tarreau522d6c02009-12-06 18:49:18 +01004152 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004153 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004154 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004155 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004156 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004157 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004158}
4159
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004160/* send a server's name with an outgoing request over an established connection.
4161 * Note: this function is designed to be called once the request has been scheduled
4162 * for being forwarded. This is the reason why it rewinds the buffer before
4163 * proceeding.
4164 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004165int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004166
4167 struct hdr_ctx ctx;
4168
Mark Lamourinec2247f02012-01-04 13:02:01 -05004169 char *hdr_name = be->server_id_hdr_name;
4170 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004171 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004172 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004173 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004174
William Lallemandd9e90662012-01-30 17:27:17 +01004175 ctx.idx = 0;
4176
Willy Tarreau211cdec2014-04-17 20:18:08 +02004177 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004178 if (old_o) {
4179 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004180 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004181 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004182 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004183 }
4184
Willy Tarreau9b28e032012-10-12 23:49:43 +02004185 old_i = chn->buf->i;
4186 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 -05004187 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004188 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004189 }
4190
4191 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004192 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004193 memcpy(hdr_val, hdr_name, hdr_name_len);
4194 hdr_val += hdr_name_len;
4195 *hdr_val++ = ':';
4196 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004197 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4198 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004199
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004200 if (old_o) {
4201 /* If this was a forwarded request, we must readjust the amount of
4202 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004203 * variations. Note that the current state is >= HTTP_MSG_BODY,
4204 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004205 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004206 old_o += chn->buf->i - old_i;
4207 b_adv(chn->buf, old_o);
4208 txn->req.next -= old_o;
4209 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004210 }
4211
Mark Lamourinec2247f02012-01-04 13:02:01 -05004212 return 0;
4213}
4214
Willy Tarreau610ecce2010-01-04 21:15:02 +01004215/* Terminate current transaction and prepare a new one. This is very tricky
4216 * right now but it works.
4217 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004218void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004220 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004221 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004222 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004223 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004224 struct connection *srv_conn;
4225 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004226 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004227
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228 /* FIXME: We need a more portable way of releasing a backend's and a
4229 * server's connections. We need a safer way to reinitialize buffer
4230 * flags. We also need a more accurate method for computing per-request
4231 * data.
4232 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004233 /*
4234 * XXX cognet: This is probably wrong, this is killing a whole
4235 * connection, in the new world order, we probably want to just kill
4236 * the stream, this is to be revisited the day we handle multiple
4237 * streams in one server connection.
4238 */
4239 cs = objt_cs(s->si[1].end);
4240 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004241
Willy Tarreau4213a112013-12-15 10:25:42 +01004242 /* unless we're doing keep-alive, we want to quickly close the connection
4243 * to the server.
4244 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004245 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004246 !si_conn_ready(&s->si[1])) {
4247 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4248 si_shutr(&s->si[1]);
4249 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004250 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251
Willy Tarreaue7dff022015-04-03 01:14:29 +02004252 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004253 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004254 if (unlikely(s->srv_conn))
4255 sess_change_server(s, NULL);
4256 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257
4258 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004259 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004260
Willy Tarreaueee5b512015-04-03 23:46:31 +02004261 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 int n;
4263
Willy Tarreaueee5b512015-04-03 23:46:31 +02004264 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004265 if (n < 1 || n > 5)
4266 n = 0;
4267
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004268 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004269 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004270 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004271 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004272 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004273 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4274 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004275 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004276 }
4277
4278 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004279 s->logs.bytes_in -= s->req.buf->i;
4280 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281
4282 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004283 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004284 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004285 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 s->do_log(s);
4287 }
4288
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004289 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004290 stream_stop_content_counters(s);
4291 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004292
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 s->logs.accept_date = date; /* user-visible date for logging */
4294 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004295 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4296 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 tv_zero(&s->logs.tv_request);
4298 s->logs.t_queue = -1;
4299 s->logs.t_connect = -1;
4300 s->logs.t_data = -1;
4301 s->logs.t_close = 0;
4302 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4303 s->logs.srv_queue_size = 0; /* we will get this number soon */
4304
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004305 s->logs.bytes_in = s->req.total = s->req.buf->i;
4306 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004307
4308 if (s->pend_pos)
4309 pendconn_free(s->pend_pos);
4310
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004311 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004312 if (s->flags & SF_CURR_SESS) {
4313 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004314 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004316 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004317 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004318 }
4319
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004320 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004321
Willy Tarreau4213a112013-12-15 10:25:42 +01004322 /* only release our endpoint if we don't intend to reuse the
4323 * connection.
4324 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004325 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004326 !si_conn_ready(&s->si[1])) {
4327 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004328 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004329 }
4330
Willy Tarreau350f4872014-11-28 14:42:25 +01004331 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4332 s->si[1].err_type = SI_ET_NONE;
4333 s->si[1].conn_retries = 0; /* used for logging too */
4334 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004335 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 +01004336 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 +01004337 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 +02004338 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4339 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4340 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004341
Willy Tarreaueee5b512015-04-03 23:46:31 +02004342 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004344 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004345
4346 if (prev_status == 401 || prev_status == 407) {
4347 /* In HTTP keep-alive mode, if we receive a 401, we still have
4348 * a chance of being able to send the visitor again to the same
4349 * server over the same connection. This is required by some
4350 * broken protocols such as NTLM, and anyway whenever there is
4351 * an opportunity for sending the challenge to the proper place,
4352 * it's better to do it (at least it helps with debugging).
4353 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004354 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004355 if (srv_conn)
4356 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004357 }
4358
Willy Tarreau53f96852016-02-02 18:50:47 +01004359 /* Never ever allow to reuse a connection from a non-reuse backend */
4360 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4361 srv_conn->flags |= CO_FL_PRIVATE;
4362
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004363 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004364 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004365
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004366 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004367 s->req.flags |= CF_NEVER_WAIT;
4368 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004369 }
4370
Willy Tarreau714ea782015-11-25 20:11:11 +01004371 /* we're removing the analysers, we MUST re-enable events detection.
4372 * We don't enable close on the response channel since it's either
4373 * already closed, or in keep-alive with an idle connection handler.
4374 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004375 channel_auto_read(&s->req);
4376 channel_auto_close(&s->req);
4377 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004378
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004379 /* we're in keep-alive with an idle connection, monitor it if not already done */
4380 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004381 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004382 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004383 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004384 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004385 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004386 else if (prev_flags & TX_NOT_FIRST)
4387 /* note: we check the request, not the connection, but
4388 * this is valid for strategies SAFE and AGGR, and in
4389 * case of ALWS, we don't care anyway.
4390 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004391 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004392 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004393 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004394 }
Christopher Faulete6006242017-03-10 11:52:44 +01004395 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4396 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397}
4398
4399
4400/* This function updates the request state machine according to the response
4401 * state machine and buffer flags. It returns 1 if it changes anything (flag
4402 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4403 * it is only used to find when a request/response couple is complete. Both
4404 * this function and its equivalent should loop until both return zero. It
4405 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4406 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004407int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004408{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004409 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004410 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004411 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412 unsigned int old_state = txn->req.msg_state;
4413
Christopher Faulet4be98032017-07-18 10:48:24 +02004414 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004415 return 0;
4416
4417 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004418 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004419 * We can shut the read side unless we want to abort_on_close,
4420 * or we have a POST request. The issue with POST requests is
4421 * that some browsers still send a CRLF after the request, and
4422 * this CRLF must be read so that it does not remain in the kernel
4423 * buffers, otherwise a close could cause an RST on some systems
4424 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004425 * Note that if we're using keep-alive on the client side, we'd
4426 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004427 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004428 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004429 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004430 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4431 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4432 !(s->be->options & PR_O_ABRT_CLOSE) &&
4433 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004434 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435
Willy Tarreau40f151a2012-12-20 12:10:09 +01004436 /* if the server closes the connection, we want to immediately react
4437 * and close the socket to save packets and syscalls.
4438 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004439 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004440
Willy Tarreau7f876a12015-11-18 11:59:55 +01004441 /* In any case we've finished parsing the request so we must
4442 * disable Nagle when sending data because 1) we're not going
4443 * to shut this side, and 2) the server is waiting for us to
4444 * send pending data.
4445 */
4446 chn->flags |= CF_NEVER_WAIT;
4447
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4449 goto wait_other_side;
4450
4451 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4452 /* The server has not finished to respond, so we
4453 * don't want to move in order not to upset it.
4454 */
4455 goto wait_other_side;
4456 }
4457
Willy Tarreau610ecce2010-01-04 21:15:02 +01004458 /* When we get here, it means that both the request and the
4459 * response have finished receiving. Depending on the connection
4460 * mode, we'll have to wait for the last bytes to leave in either
4461 * direction, and sometimes for a close to be effective.
4462 */
4463
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004464 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4465 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004466 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4467 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004468 }
4469 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4470 /* Option forceclose is set, or either side wants to close,
4471 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004472 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004473 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004474 *
4475 * However, there is an exception if the response
4476 * length is undefined. In this case, we need to wait
4477 * the close from the server. The response will be
4478 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004479 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004480 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4481 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4482 goto check_channel_flags;
4483
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004484 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4485 channel_shutr_now(chn);
4486 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004487 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004488 }
4489 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004490 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4491 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004492 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004493 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4494 channel_auto_read(chn);
4495 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004496 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004497 }
4498
Christopher Faulet4be98032017-07-18 10:48:24 +02004499 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 }
4501
4502 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4503 http_msg_closing:
4504 /* nothing else to forward, just waiting for the output buffer
4505 * to be empty and for the shutw_now to take effect.
4506 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004507 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 txn->req.msg_state = HTTP_MSG_CLOSED;
4509 goto http_msg_closed;
4510 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004511 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004512 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004515 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004516 }
4517
4518 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4519 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004520 /* see above in MSG_DONE why we only do this in these states */
4521 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4522 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4523 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004524 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004525 goto wait_other_side;
4526 }
4527
Christopher Faulet4be98032017-07-18 10:48:24 +02004528 check_channel_flags:
4529 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4530 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4531 /* if we've just closed an output, let's switch */
4532 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4533 txn->req.msg_state = HTTP_MSG_CLOSING;
4534 goto http_msg_closing;
4535 }
4536
4537
Willy Tarreau610ecce2010-01-04 21:15:02 +01004538 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004539 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540}
4541
4542
4543/* This function updates the response state machine according to the request
4544 * state machine and buffer flags. It returns 1 if it changes anything (flag
4545 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4546 * it is only used to find when a request/response couple is complete. Both
4547 * this function and its equivalent should loop until both return zero. It
4548 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4549 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004550int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004552 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004553 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004554 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004555 unsigned int old_state = txn->rsp.msg_state;
4556
Christopher Faulet4be98032017-07-18 10:48:24 +02004557 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004558 return 0;
4559
4560 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4561 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004562 * still monitor the server connection for a possible close
4563 * while the request is being uploaded, so we don't disable
4564 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004566 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567
4568 if (txn->req.msg_state == HTTP_MSG_ERROR)
4569 goto wait_other_side;
4570
4571 if (txn->req.msg_state < HTTP_MSG_DONE) {
4572 /* The client seems to still be sending data, probably
4573 * because we got an error response during an upload.
4574 * We have the choice of either breaking the connection
4575 * or letting it pass through. Let's do the later.
4576 */
4577 goto wait_other_side;
4578 }
4579
Willy Tarreau610ecce2010-01-04 21:15:02 +01004580 /* When we get here, it means that both the request and the
4581 * response have finished receiving. Depending on the connection
4582 * mode, we'll have to wait for the last bytes to leave in either
4583 * direction, and sometimes for a close to be effective.
4584 */
4585
4586 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4587 /* Server-close mode : shut read and wait for the request
4588 * side to close its output buffer. The caller will detect
4589 * when we're in DONE and the other is in CLOSED and will
4590 * catch that for the final cleanup.
4591 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004592 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4593 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004594 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004595 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4596 /* Option forceclose is set, or either side wants to close,
4597 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004598 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004599 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004600 *
4601 * However, there is an exception if the response length
4602 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004603 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004604 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4605 channel_auto_read(chn);
4606 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4607 chn->flags |= CF_NEVER_WAIT;
4608 }
4609 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004610 channel_shutr_now(chn);
4611 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004612 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004613 }
4614 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004615 /* The last possible modes are keep-alive and tunnel. Tunnel will
4616 * need to forward remaining data. Keep-alive will need to monitor
4617 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004619 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004620 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004621 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4622 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004623 }
4624
Christopher Faulet4be98032017-07-18 10:48:24 +02004625 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626 }
4627
4628 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4629 http_msg_closing:
4630 /* nothing else to forward, just waiting for the output buffer
4631 * to be empty and for the shutw_now to take effect.
4632 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004633 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004634 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4635 goto http_msg_closed;
4636 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004637 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004638 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004640 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004641 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004642 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004643 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004644 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645 }
4646
4647 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4648 http_msg_closed:
4649 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004650 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004651 channel_auto_close(chn);
4652 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004653 goto wait_other_side;
4654 }
4655
Christopher Faulet4be98032017-07-18 10:48:24 +02004656 check_channel_flags:
4657 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4658 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4659 /* if we've just closed an output, let's switch */
4660 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4661 goto http_msg_closing;
4662 }
4663
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004665 /* We force the response to leave immediately if we're waiting for the
4666 * other side, since there is no pending shutdown to push it out.
4667 */
4668 if (!channel_is_empty(chn))
4669 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004670 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671}
4672
4673
Christopher Faulet894da4c2017-07-18 11:29:07 +02004674/* Resync the request and response state machines. */
4675void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004677 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004678#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004679 int old_req_state = txn->req.msg_state;
4680 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004681#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004682
Willy Tarreau610ecce2010-01-04 21:15:02 +01004683 http_sync_req_state(s);
4684 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 if (!http_sync_res_state(s))
4686 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687 if (!http_sync_req_state(s))
4688 break;
4689 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004690
Christopher Faulet894da4c2017-07-18 11:29:07 +02004691 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4692 "req->analysers=0x%08x res->analysers=0x%08x\n",
4693 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004694 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4695 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004696 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004697
4698
Willy Tarreau610ecce2010-01-04 21:15:02 +01004699 /* OK, both state machines agree on a compatible state.
4700 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004701 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4702 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004703 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4704 * means we must abort the request.
4705 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4706 * corresponding channel.
4707 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4708 * on the response with server-close mode means we've completed one
4709 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004711 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4712 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004713 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004714 channel_auto_close(&s->req);
4715 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004716 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004717 channel_auto_close(&s->res);
4718 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004719 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004720 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4721 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004722 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004723 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004724 channel_auto_close(&s->res);
4725 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004726 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004727 channel_abort(&s->req);
4728 channel_auto_close(&s->req);
4729 channel_auto_read(&s->req);
4730 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004731 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004732 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4733 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4734 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4735 s->req.analysers &= AN_REQ_FLT_END;
4736 if (HAS_REQ_DATA_FILTERS(s))
4737 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4738 }
4739 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4740 s->res.analysers &= AN_RES_FLT_END;
4741 if (HAS_RSP_DATA_FILTERS(s))
4742 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4743 }
4744 channel_auto_close(&s->req);
4745 channel_auto_read(&s->req);
4746 channel_auto_close(&s->res);
4747 channel_auto_read(&s->res);
4748 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004749 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4750 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004752 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4753 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4754 /* server-close/keep-alive: terminate this transaction,
4755 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004756 * a fresh-new transaction, but only once we're sure there's
4757 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004758 * another request. They must not hold any pending output data
4759 * and the response buffer must realigned
4760 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004761 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004762 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004763 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004764 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004765 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004766 else {
4767 s->req.analysers = AN_REQ_FLT_END;
4768 s->res.analysers = AN_RES_FLT_END;
4769 txn->flags |= TX_WAIT_CLEANUP;
4770 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004771 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004772}
4773
Willy Tarreaud98cf932009-12-27 22:54:55 +01004774/* This function is an analyser which forwards request body (including chunk
4775 * sizes if any). It is called as soon as we must forward, even if we forward
4776 * zero byte. The only situation where it must not be called is when we're in
4777 * tunnel mode and we want to forward till the close. It's used both to forward
4778 * remaining data and to resync after end of body. It expects the msg_state to
4779 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004780 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004781 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004782 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004783 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004784int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004785{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004786 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004787 struct http_txn *txn = s->txn;
4788 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004789 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004790
Christopher Faulet814d2702017-03-30 11:33:44 +02004791 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4792 now_ms, __FUNCTION__,
4793 s,
4794 req,
4795 req->rex, req->wex,
4796 req->flags,
4797 req->buf->i,
4798 req->analysers);
4799
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004800 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4801 return 0;
4802
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004803 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004804 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004805 /* Output closed while we were sending data. We must abort and
4806 * wake the other side up.
4807 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004808 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004809 msg->msg_state = HTTP_MSG_ERROR;
4810 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004811 return 1;
4812 }
4813
Willy Tarreaud98cf932009-12-27 22:54:55 +01004814 /* Note that we don't have to send 100-continue back because we don't
4815 * need the data to complete our job, and it's up to the server to
4816 * decide whether to return 100, 417 or anything else in return of
4817 * an "Expect: 100-continue" header.
4818 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004819 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004820 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4821 ? HTTP_MSG_CHUNK_SIZE
4822 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004823
4824 /* TODO/filters: when http-buffer-request option is set or if a
4825 * rule on url_param exists, the first chunk size could be
4826 * already parsed. In that case, msg->next is after the chunk
4827 * size (including the CRLF after the size). So this case should
4828 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004829 }
4830
Willy Tarreau7ba23542014-04-17 21:50:00 +02004831 /* Some post-connect processing might want us to refrain from starting to
4832 * forward data. Currently, the only reason for this is "balance url_param"
4833 * whichs need to parse/process the request after we've enabled forwarding.
4834 */
4835 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004836 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004837 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004838 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004839 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004840 }
4841 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4842 }
4843
Willy Tarreau80a92c02014-03-12 10:41:13 +01004844 /* in most states, we should abort in case of early close */
4845 channel_auto_close(req);
4846
Willy Tarreauefdf0942014-04-24 20:08:57 +02004847 if (req->to_forward) {
4848 /* We can't process the buffer's contents yet */
4849 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004850 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004851 }
4852
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004853 if (msg->msg_state < HTTP_MSG_DONE) {
4854 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4855 ? http_msg_forward_chunked_body(s, msg)
4856 : http_msg_forward_body(s, msg));
4857 if (!ret)
4858 goto missing_data_or_waiting;
4859 if (ret < 0)
4860 goto return_bad_req;
4861 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004862
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004863 /* other states, DONE...TUNNEL */
4864 /* we don't want to forward closes on DONE except in tunnel mode. */
4865 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4866 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004867
Christopher Faulet894da4c2017-07-18 11:29:07 +02004868 http_resync_states(s);
4869 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004870 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4871 if (req->flags & CF_SHUTW) {
4872 /* request errors are most likely due to the
4873 * server aborting the transfer. */
4874 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004875 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004876 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004877 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004878 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004879 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004880 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004881 }
4882
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004883 /* If "option abortonclose" is set on the backend, we want to monitor
4884 * the client's connection and forward any shutdown notification to the
4885 * server, which will decide whether to close or to go on processing the
4886 * request. We only do that in tunnel mode, and not in other modes since
4887 * it can be abused to exhaust source ports. */
4888 if (s->be->options & PR_O_ABRT_CLOSE) {
4889 channel_auto_read(req);
4890 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4891 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4892 s->si[1].flags |= SI_FL_NOLINGER;
4893 channel_auto_close(req);
4894 }
4895 else if (s->txn->meth == HTTP_METH_POST) {
4896 /* POST requests may require to read extra CRLF sent by broken
4897 * browsers and which could cause an RST to be sent upon close
4898 * on some systems (eg: Linux). */
4899 channel_auto_read(req);
4900 }
4901 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004902
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004903 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004904 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004905 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004906 if (!(s->flags & SF_ERR_MASK))
4907 s->flags |= SF_ERR_CLICL;
4908 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004909 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004910 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004911 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004912 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004913 }
4914
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004915 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4916 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004917 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004918 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004919
4920 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004921 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004922
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004923 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004924 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004925 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004926
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004927 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004928 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004929 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004930 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004931 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004932
Willy Tarreau5c620922011-05-11 19:56:11 +02004933 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004934 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004935 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004936 * modes are already handled by the stream sock layer. We must not do
4937 * this in content-length mode because it could present the MSG_MORE
4938 * flag with the last block of forwarded data, which would cause an
4939 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004940 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004941 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004942 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004943
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004944 return 0;
4945
Willy Tarreaud98cf932009-12-27 22:54:55 +01004946 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004947 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004948 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004949 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004950
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004951 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004952 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004953 txn->req.msg_state = HTTP_MSG_ERROR;
4954 if (txn->status) {
4955 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004956 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004957 } else {
4958 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004959 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004960 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004961 req->analysers &= AN_REQ_FLT_END;
4962 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 +01004963
Willy Tarreaue7dff022015-04-03 01:14:29 +02004964 if (!(s->flags & SF_ERR_MASK))
4965 s->flags |= SF_ERR_PRXCOND;
4966 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004967 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004968 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004969 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004970 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004971 }
4972 return 0;
4973
4974 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004975 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004976 txn->req.msg_state = HTTP_MSG_ERROR;
4977 if (txn->status) {
4978 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004979 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004980 } else {
4981 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004982 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004983 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004984 req->analysers &= AN_REQ_FLT_END;
4985 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 +01004986
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004987 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4988 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004989 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004990 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004991
Willy Tarreaue7dff022015-04-03 01:14:29 +02004992 if (!(s->flags & SF_ERR_MASK))
4993 s->flags |= SF_ERR_SRVCL;
4994 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004995 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004996 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004997 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004998 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004999 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005000 return 0;
5001}
5002
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005003/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5004 * processing can continue on next analysers, or zero if it either needs more
5005 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005006 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005007 * when it has nothing left to do, and may remove any analyser when it wants to
5008 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005009 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005010int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005011{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005012 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005013 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005014 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005015 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005016 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005017 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005018 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005019
Willy Tarreau87b09662015-04-03 00:22:06 +02005020 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 +02005021 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005022 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005023 rep,
5024 rep->rex, rep->wex,
5025 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005026 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005027 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005028
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005029 /*
5030 * Now parse the partial (or complete) lines.
5031 * We will check the response syntax, and also join multi-line
5032 * headers. An index of all the lines will be elaborated while
5033 * parsing.
5034 *
5035 * For the parsing, we use a 28 states FSM.
5036 *
5037 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005038 * rep->buf->p = beginning of response
5039 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5040 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005041 * msg->eol = end of current header or line (LF or CRLF)
5042 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005043 */
5044
Willy Tarreau628c40c2014-04-24 19:11:26 +02005045 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005046 /* There's a protected area at the end of the buffer for rewriting
5047 * purposes. We don't want to start to parse the request if the
5048 * protected area is affected, because we may have to move processed
5049 * data later, which is much more complicated.
5050 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005051 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005052 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005053 /* some data has still not left the buffer, wake us once that's done */
5054 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5055 goto abort_response;
5056 channel_dont_close(rep);
5057 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005058 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005059 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005060 }
5061
Willy Tarreau379357a2013-06-08 12:55:46 +02005062 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5063 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5064 buffer_slow_realign(rep->buf);
5065
Willy Tarreau9b28e032012-10-12 23:49:43 +02005066 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005067 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005068 }
5069
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005070 /* 1: we might have to print this header in debug mode */
5071 if (unlikely((global.mode & MODE_DEBUG) &&
5072 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005073 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005074 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005075
Willy Tarreau9b28e032012-10-12 23:49:43 +02005076 sol = rep->buf->p;
5077 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005078 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005080 sol += hdr_idx_first_pos(&txn->hdr_idx);
5081 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005082
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005083 while (cur_idx) {
5084 eol = sol + txn->hdr_idx.v[cur_idx].len;
5085 debug_hdr("srvhdr", s, sol, eol);
5086 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5087 cur_idx = txn->hdr_idx.v[cur_idx].next;
5088 }
5089 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005090
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005091 /*
5092 * Now we quickly check if we have found a full valid response.
5093 * If not so, we check the FD and buffer states before leaving.
5094 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005095 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005096 * responses are checked first.
5097 *
5098 * Depending on whether the client is still there or not, we
5099 * may send an error response back or not. Note that normally
5100 * we should only check for HTTP status there, and check I/O
5101 * errors somewhere else.
5102 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103
Willy Tarreau655dce92009-11-08 13:10:58 +01005104 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005105 /* Invalid response */
5106 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5107 /* we detected a parsing error. We want to archive this response
5108 * in the dedicated proxy area for later troubleshooting.
5109 */
5110 hdr_response_bad:
5111 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005112 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005113
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005114 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005115 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005116 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005117 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005118 }
Willy Tarreau64648412010-03-05 10:41:54 +01005119 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005120 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005121 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005122 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005123 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005124 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005125 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005126
Willy Tarreaue7dff022015-04-03 01:14:29 +02005127 if (!(s->flags & SF_ERR_MASK))
5128 s->flags |= SF_ERR_PRXCOND;
5129 if (!(s->flags & SF_FINST_MASK))
5130 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005131
5132 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005133 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005134
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005135 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005136 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005137 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005138 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005139 goto hdr_response_bad;
5140 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005141
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005143 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005144 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005145 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005146 else if (txn->flags & TX_NOT_FIRST)
5147 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005148
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005149 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005150 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005151 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005152 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005153 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005154
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005155 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005156 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005157 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005158
5159 /* Check to see if the server refused the early data.
5160 * If so, just send a 425
5161 */
5162 if (objt_cs(s->si[1].end)) {
5163 struct connection *conn = objt_cs(s->si[1].end)->conn;
5164
5165 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5166 txn->status = 425;
5167 }
5168
Willy Tarreau350f4872014-11-28 14:42:25 +01005169 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005170 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005171 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005172
Willy Tarreaue7dff022015-04-03 01:14:29 +02005173 if (!(s->flags & SF_ERR_MASK))
5174 s->flags |= SF_ERR_SRVCL;
5175 if (!(s->flags & SF_FINST_MASK))
5176 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005177 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005178 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005179
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005180 /* read timeout : return a 504 to the client. */
5181 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005183 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005184
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005185 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005186 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005187 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005188 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005189 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005190
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005191 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005192 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005194 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005195 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005196 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005197
Willy Tarreaue7dff022015-04-03 01:14:29 +02005198 if (!(s->flags & SF_ERR_MASK))
5199 s->flags |= SF_ERR_SRVTO;
5200 if (!(s->flags & SF_FINST_MASK))
5201 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 return 0;
5203 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005204
Willy Tarreauf003d372012-11-26 13:35:37 +01005205 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005206 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005207 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5208 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005209 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005210 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005211
Christopher Faulet0184ea72017-01-05 14:06:34 +01005212 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005213 channel_auto_close(rep);
5214
5215 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005216 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005217 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005218
Willy Tarreaue7dff022015-04-03 01:14:29 +02005219 if (!(s->flags & SF_ERR_MASK))
5220 s->flags |= SF_ERR_CLICL;
5221 if (!(s->flags & SF_FINST_MASK))
5222 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005223
Willy Tarreau87b09662015-04-03 00:22:06 +02005224 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005225 return 0;
5226 }
5227
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005228 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005229 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005230 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005231 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005232 else if (txn->flags & TX_NOT_FIRST)
5233 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005234
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005235 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005236 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005237 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005238 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005239 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005240
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005241 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005242 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005243 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005244 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005245 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005246 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005247
Willy Tarreaue7dff022015-04-03 01:14:29 +02005248 if (!(s->flags & SF_ERR_MASK))
5249 s->flags |= SF_ERR_SRVCL;
5250 if (!(s->flags & SF_FINST_MASK))
5251 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005252 return 0;
5253 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005254
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005255 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005256 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005257 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005258 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005259 else if (txn->flags & TX_NOT_FIRST)
5260 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005261
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005262 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005263 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005264 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005265
Willy Tarreaue7dff022015-04-03 01:14:29 +02005266 if (!(s->flags & SF_ERR_MASK))
5267 s->flags |= SF_ERR_CLICL;
5268 if (!(s->flags & SF_FINST_MASK))
5269 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005270
Willy Tarreau87b09662015-04-03 00:22:06 +02005271 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005272 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005273 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005274
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005275 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005276 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005277 return 0;
5278 }
5279
5280 /* More interesting part now : we know that we have a complete
5281 * response which at least looks like HTTP. We have an indicator
5282 * of each header's length, so we can parse them quickly.
5283 */
5284
5285 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005286 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005287
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005288 /*
5289 * 1: get the status code
5290 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005291 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292 if (n < 1 || n > 5)
5293 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005294 /* when the client triggers a 4xx from the server, it's most often due
5295 * to a missing object or permission. These events should be tracked
5296 * because if they happen often, it may indicate a brute force or a
5297 * vulnerability scan.
5298 */
5299 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005300 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005301
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005302 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005303 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005304
Willy Tarreau91852eb2015-05-01 13:26:00 +02005305 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5306 * exactly one digit "." one digit. This check may be disabled using
5307 * option accept-invalid-http-response.
5308 */
5309 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5310 if (msg->sl.st.v_l != 8) {
5311 msg->err_pos = 0;
5312 goto hdr_response_bad;
5313 }
5314
5315 if (rep->buf->p[4] != '/' ||
5316 !isdigit((unsigned char)rep->buf->p[5]) ||
5317 rep->buf->p[6] != '.' ||
5318 !isdigit((unsigned char)rep->buf->p[7])) {
5319 msg->err_pos = 4;
5320 goto hdr_response_bad;
5321 }
5322 }
5323
Willy Tarreau5b154472009-12-21 20:11:07 +01005324 /* check if the response is HTTP/1.1 or above */
5325 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005326 ((rep->buf->p[5] > '1') ||
5327 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005328 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005329
5330 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005331 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 +01005332
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005333 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005334 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005335
Willy Tarreau9b28e032012-10-12 23:49:43 +02005336 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005337
Willy Tarreau39650402010-03-15 19:44:39 +01005338 /* Adjust server's health based on status code. Note: status codes 501
5339 * and 505 are triggered on demand by client request, so we must not
5340 * count them as server failures.
5341 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005342 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005343 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005344 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005345 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005346 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005347 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005348
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005349 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005350 * We may be facing a 100-continue response, or any other informational
5351 * 1xx response which is non-final, in which case this is not the right
5352 * response, and we're waiting for the next one. Let's allow this response
5353 * to go to the client and wait for the next one. There's an exception for
5354 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005355 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005356 if (txn->status < 200 &&
5357 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005358 hdr_idx_init(&txn->hdr_idx);
5359 msg->next -= channel_forward(rep, msg->next);
5360 msg->msg_state = HTTP_MSG_RPBEFORE;
5361 txn->status = 0;
5362 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005363 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005364 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005365 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005366
Willy Tarreaua14ad722017-07-07 11:36:32 +02005367 /*
5368 * 2: check for cacheability.
5369 */
5370
5371 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005372 case 200:
5373 case 203:
5374 case 206:
5375 case 300:
5376 case 301:
5377 case 410:
5378 /* RFC2616 @13.4:
5379 * "A response received with a status code of
5380 * 200, 203, 206, 300, 301 or 410 MAY be stored
5381 * by a cache (...) unless a cache-control
5382 * directive prohibits caching."
5383 *
5384 * RFC2616 @9.5: POST method :
5385 * "Responses to this method are not cacheable,
5386 * unless the response includes appropriate
5387 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005388 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005390 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005391 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5392 break;
5393 default:
5394 break;
5395 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005396
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005397 /*
5398 * 3: we may need to capture headers
5399 */
5400 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005401 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005402 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005403 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005404
Willy Tarreau557f1992015-05-01 10:05:17 +02005405 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5406 * by RFC7230#3.3.3 :
5407 *
5408 * The length of a message body is determined by one of the following
5409 * (in order of precedence):
5410 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005411 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5412 * the connection will become a tunnel immediately after the empty
5413 * line that concludes the header fields. A client MUST ignore
5414 * any Content-Length or Transfer-Encoding header fields received
5415 * in such a message. Any 101 response (Switching Protocols) is
5416 * managed in the same manner.
5417 *
5418 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005419 * (Informational), 204 (No Content), or 304 (Not Modified) status
5420 * code is always terminated by the first empty line after the
5421 * header fields, regardless of the header fields present in the
5422 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005423 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005424 * 3. If a Transfer-Encoding header field is present and the chunked
5425 * transfer coding (Section 4.1) is the final encoding, the message
5426 * body length is determined by reading and decoding the chunked
5427 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005428 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005429 * If a Transfer-Encoding header field is present in a response and
5430 * the chunked transfer coding is not the final encoding, the
5431 * message body length is determined by reading the connection until
5432 * it is closed by the server. If a Transfer-Encoding header field
5433 * is present in a request and the chunked transfer coding is not
5434 * the final encoding, the message body length cannot be determined
5435 * reliably; the server MUST respond with the 400 (Bad Request)
5436 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005437 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005438 * If a message is received with both a Transfer-Encoding and a
5439 * Content-Length header field, the Transfer-Encoding overrides the
5440 * Content-Length. Such a message might indicate an attempt to
5441 * perform request smuggling (Section 9.5) or response splitting
5442 * (Section 9.4) and ought to be handled as an error. A sender MUST
5443 * remove the received Content-Length field prior to forwarding such
5444 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005445 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005446 * 4. If a message is received without Transfer-Encoding and with
5447 * either multiple Content-Length header fields having differing
5448 * field-values or a single Content-Length header field having an
5449 * invalid value, then the message framing is invalid and the
5450 * recipient MUST treat it as an unrecoverable error. If this is a
5451 * request message, the server MUST respond with a 400 (Bad Request)
5452 * status code and then close the connection. If this is a response
5453 * message received by a proxy, the proxy MUST close the connection
5454 * to the server, discard the received response, and send a 502 (Bad
5455 * Gateway) response to the client. If this is a response message
5456 * received by a user agent, the user agent MUST close the
5457 * connection to the server and discard the received response.
5458 *
5459 * 5. If a valid Content-Length header field is present without
5460 * Transfer-Encoding, its decimal value defines the expected message
5461 * body length in octets. If the sender closes the connection or
5462 * the recipient times out before the indicated number of octets are
5463 * received, the recipient MUST consider the message to be
5464 * incomplete and close the connection.
5465 *
5466 * 6. If this is a request message and none of the above are true, then
5467 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005468 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005469 * 7. Otherwise, this is a response message without a declared message
5470 * body length, so the message body length is determined by the
5471 * number of octets received prior to the server closing the
5472 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005473 */
5474
5475 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005476 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005477 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005478 * FIXME: should we parse anyway and return an error on chunked encoding ?
5479 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005480 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5481 txn->status == 101)) {
5482 /* Either we've established an explicit tunnel, or we're
5483 * switching the protocol. In both cases, we're very unlikely
5484 * to understand the next protocols. We have to switch to tunnel
5485 * mode, so that we transfer the request and responses then let
5486 * this protocol pass unmodified. When we later implement specific
5487 * parsers for such protocols, we'll want to check the Upgrade
5488 * header which contains information about that protocol for
5489 * responses with status 101 (eg: see RFC2817 about TLS).
5490 */
5491 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5492 msg->flags |= HTTP_MSGF_XFER_LEN;
5493 goto end;
5494 }
5495
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005496 if (txn->meth == HTTP_METH_HEAD ||
5497 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005498 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005499 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005500 goto skip_content_length;
5501 }
5502
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005503 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005504 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005505 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005506 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005507 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5508 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005509 /* bad transfer-encoding (chunked followed by something else) */
5510 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005511 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005512 break;
5513 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005514 }
5515
Willy Tarreau1c913912015-04-30 10:57:51 +02005516 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005517 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005518 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005519 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5520 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5521 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005522 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005523 signed long long cl;
5524
Willy Tarreauad14f752011-09-02 20:33:27 +02005525 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005526 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005527 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005528 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005529
Willy Tarreauad14f752011-09-02 20:33:27 +02005530 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005531 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005532 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005533 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005534
Willy Tarreauad14f752011-09-02 20:33:27 +02005535 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005536 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005537 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005538 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005539
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005540 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005541 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005542 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005543 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005544
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005545 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005546 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005547 }
5548
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005549 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005550 /* Now we have to check if we need to modify the Connection header.
5551 * This is more difficult on the response than it is on the request,
5552 * because we can have two different HTTP versions and we don't know
5553 * how the client will interprete a response. For instance, let's say
5554 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5555 * HTTP/1.1 response without any header. Maybe it will bound itself to
5556 * HTTP/1.0 because it only knows about it, and will consider the lack
5557 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5558 * the lack of header as a keep-alive. Thus we will use two flags
5559 * indicating how a request MAY be understood by the client. In case
5560 * of multiple possibilities, we'll fix the header to be explicit. If
5561 * ambiguous cases such as both close and keepalive are seen, then we
5562 * will fall back to explicit close. Note that we won't take risks with
5563 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005564 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005565 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005566 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5567 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5568 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5569 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005570 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005571
Willy Tarreau70dffda2014-01-30 03:07:23 +01005572 /* this situation happens when combining pretend-keepalive with httpclose. */
5573 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005574 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005575 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005576 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5577
Willy Tarreau60466522010-01-18 19:08:45 +01005578 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005579 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005580 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5581 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005582
Willy Tarreau60466522010-01-18 19:08:45 +01005583 /* now adjust header transformations depending on current state */
5584 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5585 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5586 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005587 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005588 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005589 }
Willy Tarreau60466522010-01-18 19:08:45 +01005590 else { /* SCL / KAL */
5591 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005592 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005593 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005594 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005595
Willy Tarreau60466522010-01-18 19:08:45 +01005596 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005597 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005598
Willy Tarreau60466522010-01-18 19:08:45 +01005599 /* Some keep-alive responses are converted to Server-close if
5600 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005601 */
Willy Tarreau60466522010-01-18 19:08:45 +01005602 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5603 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005604 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005605 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005606 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005607 }
5608
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005609 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005610 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005611 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005612
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005613 /* end of job, return OK */
5614 rep->analysers &= ~an_bit;
5615 rep->analyse_exp = TICK_ETERNITY;
5616 channel_auto_close(rep);
5617 return 1;
5618
5619 abort_keep_alive:
5620 /* A keep-alive request to the server failed on a network error.
5621 * The client is required to retry. We need to close without returning
5622 * any other information so that the client retries.
5623 */
5624 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005625 rep->analysers &= AN_RES_FLT_END;
5626 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005627 channel_auto_close(rep);
5628 s->logs.logwait = 0;
5629 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005630 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005631 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005632 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005633 return 0;
5634}
5635
5636/* This function performs all the processing enabled for the current response.
5637 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005638 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005639 * other functions. It works like process_request (see indications above).
5640 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005641int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005642{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005643 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005644 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005645 struct http_msg *msg = &txn->rsp;
5646 struct proxy *cur_proxy;
5647 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005648 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005649
Willy Tarreau87b09662015-04-03 00:22:06 +02005650 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 +02005651 now_ms, __FUNCTION__,
5652 s,
5653 rep,
5654 rep->rex, rep->wex,
5655 rep->flags,
5656 rep->buf->i,
5657 rep->analysers);
5658
5659 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5660 return 0;
5661
Willy Tarreau70730dd2014-04-24 18:06:27 +02005662 /* The stats applet needs to adjust the Connection header but we don't
5663 * apply any filter there.
5664 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005665 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5666 rep->analysers &= ~an_bit;
5667 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005668 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005669 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005670
Willy Tarreau58975672014-04-24 21:13:57 +02005671 /*
5672 * We will have to evaluate the filters.
5673 * As opposed to version 1.2, now they will be evaluated in the
5674 * filters order and not in the header order. This means that
5675 * each filter has to be validated among all headers.
5676 *
5677 * Filters are tried with ->be first, then with ->fe if it is
5678 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005679 *
5680 * Maybe we are in resume condiion. In this case I choose the
5681 * "struct proxy" which contains the rule list matching the resume
5682 * pointer. If none of theses "struct proxy" match, I initialise
5683 * the process with the first one.
5684 *
5685 * In fact, I check only correspondance betwwen the current list
5686 * pointer and the ->fe rule list. If it doesn't match, I initialize
5687 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005688 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005689 if (s->current_rule_list == &sess->fe->http_res_rules)
5690 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005691 else
5692 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005693 while (1) {
5694 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005695
Willy Tarreau58975672014-04-24 21:13:57 +02005696 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005697 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005698 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005699
Willy Tarreau51d861a2015-05-22 17:30:48 +02005700 if (ret == HTTP_RULE_RES_BADREQ)
5701 goto return_srv_prx_502;
5702
5703 if (ret == HTTP_RULE_RES_DONE) {
5704 rep->analysers &= ~an_bit;
5705 rep->analyse_exp = TICK_ETERNITY;
5706 return 1;
5707 }
5708 }
5709
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005710 /* we need to be called again. */
5711 if (ret == HTTP_RULE_RES_YIELD) {
5712 channel_dont_close(rep);
5713 return 0;
5714 }
5715
Willy Tarreau58975672014-04-24 21:13:57 +02005716 /* try headers filters */
5717 if (rule_set->rsp_exp != NULL) {
5718 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5719 return_bad_resp:
5720 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005721 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005722 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005723 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005724 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005725 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005726 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005727 txn->status = 502;
5728 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005729 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005730 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005731 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005732 if (!(s->flags & SF_ERR_MASK))
5733 s->flags |= SF_ERR_PRXCOND;
5734 if (!(s->flags & SF_FINST_MASK))
5735 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005736 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005737 }
Willy Tarreau58975672014-04-24 21:13:57 +02005738 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005739
Willy Tarreau58975672014-04-24 21:13:57 +02005740 /* has the response been denied ? */
5741 if (txn->flags & TX_SVDENY) {
5742 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005743 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005744
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005745 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5746 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005747 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005748 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005749
Willy Tarreau58975672014-04-24 21:13:57 +02005750 goto return_srv_prx_502;
5751 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005752
Willy Tarreau58975672014-04-24 21:13:57 +02005753 /* add response headers from the rule sets in the same order */
5754 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005755 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005756 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005757 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005758 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005759 ret = acl_pass(ret);
5760 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5761 ret = !ret;
5762 if (!ret)
5763 continue;
5764 }
5765 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5766 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005767 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005768
Willy Tarreau58975672014-04-24 21:13:57 +02005769 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005770 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005771 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005772 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005773 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005774
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005775 /* After this point, this anayzer can't return yield, so we can
5776 * remove the bit corresponding to this analyzer from the list.
5777 *
5778 * Note that the intermediate returns and goto found previously
5779 * reset the analyzers.
5780 */
5781 rep->analysers &= ~an_bit;
5782 rep->analyse_exp = TICK_ETERNITY;
5783
Willy Tarreau58975672014-04-24 21:13:57 +02005784 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005785 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005786 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005787
Willy Tarreau58975672014-04-24 21:13:57 +02005788 /*
5789 * Now check for a server cookie.
5790 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005791 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005792 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005793
Willy Tarreau58975672014-04-24 21:13:57 +02005794 /*
5795 * Check for cache-control or pragma headers if required.
5796 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005797 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 +02005798 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005799
Willy Tarreau58975672014-04-24 21:13:57 +02005800 /*
5801 * Add server cookie in the response if needed
5802 */
5803 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5804 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005805 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005806 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5807 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5808 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5809 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5810 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005811 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005812 /* the server is known, it's not the one the client requested, or the
5813 * cookie's last seen date needs to be refreshed. We have to
5814 * insert a set-cookie here, except if we want to insert only on POST
5815 * requests and this one isn't. Note that servers which don't have cookies
5816 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005817 */
Willy Tarreau58975672014-04-24 21:13:57 +02005818 if (!objt_server(s->target)->cookie) {
5819 chunk_printf(&trash,
5820 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5821 s->be->cookie_name);
5822 }
5823 else {
5824 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005825
Willy Tarreau58975672014-04-24 21:13:57 +02005826 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5827 /* emit last_date, which is mandatory */
5828 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5829 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5830 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005831
Willy Tarreau58975672014-04-24 21:13:57 +02005832 if (s->be->cookie_maxlife) {
5833 /* emit first_date, which is either the original one or
5834 * the current date.
5835 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005836 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005837 s30tob64(txn->cookie_first_date ?
5838 txn->cookie_first_date >> 2 :
5839 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005840 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005841 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005842 }
Willy Tarreau58975672014-04-24 21:13:57 +02005843 chunk_appendf(&trash, "; path=/");
5844 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005845
Willy Tarreau58975672014-04-24 21:13:57 +02005846 if (s->be->cookie_domain)
5847 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005848
Willy Tarreau58975672014-04-24 21:13:57 +02005849 if (s->be->ck_opts & PR_CK_HTTPONLY)
5850 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005851
Willy Tarreau58975672014-04-24 21:13:57 +02005852 if (s->be->ck_opts & PR_CK_SECURE)
5853 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005854
Willy Tarreau58975672014-04-24 21:13:57 +02005855 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5856 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005857
Willy Tarreau58975672014-04-24 21:13:57 +02005858 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005859 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005860 /* the server did not change, only the date was updated */
5861 txn->flags |= TX_SCK_UPDATED;
5862 else
5863 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005864
Willy Tarreau58975672014-04-24 21:13:57 +02005865 /* Here, we will tell an eventual cache on the client side that we don't
5866 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5867 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5868 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869 */
Willy Tarreau58975672014-04-24 21:13:57 +02005870 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005871
Willy Tarreau58975672014-04-24 21:13:57 +02005872 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005873
Willy Tarreau58975672014-04-24 21:13:57 +02005874 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5875 "Cache-control: private", 22) < 0))
5876 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005877 }
Willy Tarreau58975672014-04-24 21:13:57 +02005878 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005879
Willy Tarreau58975672014-04-24 21:13:57 +02005880 /*
5881 * Check if result will be cacheable with a cookie.
5882 * We'll block the response if security checks have caught
5883 * nasty things such as a cacheable cookie.
5884 */
5885 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5886 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5887 (s->be->options & PR_O_CHK_CACHE)) {
5888 /* we're in presence of a cacheable response containing
5889 * a set-cookie header. We'll block it as requested by
5890 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005891 */
Willy Tarreau58975672014-04-24 21:13:57 +02005892 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005893 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005894
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005895 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5896 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005897 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005898 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005899
Christopher Faulet767a84b2017-11-24 16:50:31 +01005900 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5901 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005902 send_log(s->be, LOG_ALERT,
5903 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5904 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5905 goto return_srv_prx_502;
5906 }
Willy Tarreau03945942009-12-22 16:50:27 +01005907
Willy Tarreau70730dd2014-04-24 18:06:27 +02005908 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005909 /*
5910 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5911 * If an "Upgrade" token is found, the header is left untouched in order
5912 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005913 * if anything but "Upgrade" is present in the Connection header. We don't
5914 * want to touch any 101 response either since it's switching to another
5915 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005916 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005917 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005918 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005919 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005920 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5921 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005922
Willy Tarreau58975672014-04-24 21:13:57 +02005923 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5924 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5925 /* we want a keep-alive response here. Keep-alive header
5926 * required if either side is not 1.1.
5927 */
5928 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5929 want_flags |= TX_CON_KAL_SET;
5930 }
5931 else {
5932 /* we want a close response here. Close header required if
5933 * the server is 1.1, regardless of the client.
5934 */
5935 if (msg->flags & HTTP_MSGF_VER_11)
5936 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005938
Willy Tarreau58975672014-04-24 21:13:57 +02005939 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5940 http_change_connection_header(txn, msg, want_flags);
5941 }
5942
5943 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005944 /* Always enter in the body analyzer */
5945 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5946 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005947
Willy Tarreau58975672014-04-24 21:13:57 +02005948 /* if the user wants to log as soon as possible, without counting
5949 * bytes from the server, then this is the right moment. We have
5950 * to temporarily assign bytes_out to log what we currently have.
5951 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005952 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005953 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5954 s->logs.bytes_out = txn->rsp.eoh;
5955 s->do_log(s);
5956 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005957 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005958 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005959}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005960
Willy Tarreaud98cf932009-12-27 22:54:55 +01005961/* This function is an analyser which forwards response body (including chunk
5962 * sizes if any). It is called as soon as we must forward, even if we forward
5963 * zero byte. The only situation where it must not be called is when we're in
5964 * tunnel mode and we want to forward till the close. It's used both to forward
5965 * remaining data and to resync after end of body. It expects the msg_state to
5966 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005967 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005968 *
5969 * It is capable of compressing response data both in content-length mode and
5970 * in chunked mode. The state machines follows different flows depending on
5971 * whether content-length and chunked modes are used, since there are no
5972 * trailers in content-length :
5973 *
5974 * chk-mode cl-mode
5975 * ,----- BODY -----.
5976 * / \
5977 * V size > 0 V chk-mode
5978 * .--> SIZE -------------> DATA -------------> CRLF
5979 * | | size == 0 | last byte |
5980 * | v final crlf v inspected |
5981 * | TRAILERS -----------> DONE |
5982 * | |
5983 * `----------------------------------------------'
5984 *
5985 * Compression only happens in the DATA state, and must be flushed in final
5986 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5987 * is performed at once on final states for all bytes parsed, or when leaving
5988 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005989 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005990int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005991{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005992 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005993 struct http_txn *txn = s->txn;
5994 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005995 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005996
Christopher Faulet814d2702017-03-30 11:33:44 +02005997 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5998 now_ms, __FUNCTION__,
5999 s,
6000 res,
6001 res->rex, res->wex,
6002 res->flags,
6003 res->buf->i,
6004 res->analysers);
6005
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006006 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6007 return 0;
6008
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006009 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006010 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006011 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006012 /* Output closed while we were sending data. We must abort and
6013 * wake the other side up.
6014 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006015 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006016 msg->msg_state = HTTP_MSG_ERROR;
6017 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006018 return 1;
6019 }
6020
Willy Tarreau4fe41902010-06-07 22:27:41 +02006021 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006022 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006023
Christopher Fauletd7c91962015-04-30 11:48:27 +02006024 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006025 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6026 ? HTTP_MSG_CHUNK_SIZE
6027 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006028 }
6029
Willy Tarreauefdf0942014-04-24 20:08:57 +02006030 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006031 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006032 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006033 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006034 }
6035
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006036 if (msg->msg_state < HTTP_MSG_DONE) {
6037 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6038 ? http_msg_forward_chunked_body(s, msg)
6039 : http_msg_forward_body(s, msg));
6040 if (!ret)
6041 goto missing_data_or_waiting;
6042 if (ret < 0)
6043 goto return_bad_res;
6044 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006045
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006046 /* other states, DONE...TUNNEL */
6047 /* for keep-alive we don't want to forward closes on DONE */
6048 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6049 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6050 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006051
Christopher Faulet894da4c2017-07-18 11:29:07 +02006052 http_resync_states(s);
6053 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006054 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6055 if (res->flags & CF_SHUTW) {
6056 /* response errors are most likely due to the
6057 * client aborting the transfer. */
6058 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006059 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006060 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006061 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 +01006062 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006063 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006064 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006065 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006066 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006067
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006068 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006069 if (res->flags & CF_SHUTW)
6070 goto aborted_xfer;
6071
6072 /* stop waiting for data if the input is closed before the end. If the
6073 * client side was already closed, it means that the client has aborted,
6074 * so we don't want to count this as a server abort. Otherwise it's a
6075 * server abort.
6076 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006077 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006078 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006079 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006080 /* If we have some pending data, we continue the processing */
6081 if (!buffer_pending(res->buf)) {
6082 if (!(s->flags & SF_ERR_MASK))
6083 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006084 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006085 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006086 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006087 goto return_bad_res_stats_ok;
6088 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006089 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006090
Willy Tarreau40dba092010-03-04 18:14:51 +01006091 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006092 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006093 goto return_bad_res;
6094
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006095 /* When TE: chunked is used, we need to get there again to parse
6096 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006097 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6098 * or if there are filters registered on the stream, we don't want to
6099 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006100 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006101 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006102 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006103 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6104 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006105 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006106
Willy Tarreau5c620922011-05-11 19:56:11 +02006107 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006108 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006109 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006110 * modes are already handled by the stream sock layer. We must not do
6111 * this in content-length mode because it could present the MSG_MORE
6112 * flag with the last block of forwarded data, which would cause an
6113 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006114 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006115 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006116 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006117
Willy Tarreau87b09662015-04-03 00:22:06 +02006118 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006119 return 0;
6120
Willy Tarreau40dba092010-03-04 18:14:51 +01006121 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006122 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006123 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006124 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006125
6126 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006127 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006128 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006129 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006130 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006131 res->analysers &= AN_RES_FLT_END;
6132 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 +01006133 if (objt_server(s->target))
6134 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006135
Willy Tarreaue7dff022015-04-03 01:14:29 +02006136 if (!(s->flags & SF_ERR_MASK))
6137 s->flags |= SF_ERR_PRXCOND;
6138 if (!(s->flags & SF_FINST_MASK))
6139 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006140 return 0;
6141
6142 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006143 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006144 txn->rsp.msg_state = HTTP_MSG_ERROR;
6145 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006146 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006147 res->analysers &= AN_RES_FLT_END;
6148 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 +01006149
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006150 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6151 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006152 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006153 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006154
Willy Tarreaue7dff022015-04-03 01:14:29 +02006155 if (!(s->flags & SF_ERR_MASK))
6156 s->flags |= SF_ERR_CLICL;
6157 if (!(s->flags & SF_FINST_MASK))
6158 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006159 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006160}
6161
6162
6163static inline int
6164http_msg_forward_body(struct stream *s, struct http_msg *msg)
6165{
6166 struct channel *chn = msg->chn;
6167 int ret;
6168
6169 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6170
6171 if (msg->msg_state == HTTP_MSG_ENDING)
6172 goto ending;
6173
6174 /* Neither content-length, nor transfer-encoding was found, so we must
6175 * read the body until the server connection is closed. In that case, we
6176 * eat data as they come. Of course, this happens for response only. */
6177 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6178 unsigned long long len = (chn->buf->i - msg->next);
6179 msg->chunk_len += len;
6180 msg->body_len += len;
6181 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006182 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6183 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6184 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006185 msg->next += ret;
6186 msg->chunk_len -= ret;
6187 if (msg->chunk_len) {
6188 /* input empty or output full */
6189 if (chn->buf->i > msg->next)
6190 chn->flags |= CF_WAKE_WRITE;
6191 goto missing_data_or_waiting;
6192 }
6193
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006194 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6195 * always set for a request. */
6196 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6197 /* The server still sending data that should be filtered */
6198 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6199 goto missing_data_or_waiting;
6200 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006201
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006202 msg->msg_state = HTTP_MSG_ENDING;
6203
6204 ending:
6205 /* we may have some pending data starting at res->buf->p such as a last
6206 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006207 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6208 /* default_ret */ msg->next,
6209 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006210 b_adv(chn->buf, ret);
6211 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006212 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6213 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006214 if (msg->next)
6215 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006216
Christopher Fauletda02e172015-12-04 09:25:05 +01006217 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6218 /* default_ret */ 1,
6219 /* on_error */ goto error,
6220 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006221 msg->msg_state = HTTP_MSG_DONE;
6222 return 1;
6223
6224 missing_data_or_waiting:
6225 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006226 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6227 /* default_ret */ msg->next,
6228 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006229 b_adv(chn->buf, ret);
6230 msg->next -= ret;
6231 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6232 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006233 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006234 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006235 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006236 return 0;
6237 error:
6238 return -1;
6239}
6240
6241static inline int
6242http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6243{
6244 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006245 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006246 int ret;
6247
6248 /* Here we have the guarantee to be in one of the following state:
6249 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6250 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6251
6252 switch_states:
6253 switch (msg->msg_state) {
6254 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006255 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6256 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6257 /* on_error */ goto error);
6258 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006259 msg->chunk_len -= ret;
6260 if (msg->chunk_len) {
6261 /* input empty or output full */
6262 if (chn->buf->i > msg->next)
6263 chn->flags |= CF_WAKE_WRITE;
6264 goto missing_data_or_waiting;
6265 }
6266
6267 /* nothing left to forward for this chunk*/
6268 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6269 /* fall through for HTTP_MSG_CHUNK_CRLF */
6270
6271 case HTTP_MSG_CHUNK_CRLF:
6272 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006273 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006274 if (ret == 0)
6275 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006276 if (ret < 0) {
6277 msg->err_pos = chn->buf->i + ret;
6278 if (msg->err_pos < 0)
6279 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006280 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006281 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006282 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006283 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6284 /* fall through for HTTP_MSG_CHUNK_SIZE */
6285
6286 case HTTP_MSG_CHUNK_SIZE:
6287 /* read the chunk size and assign it to ->chunk_len,
6288 * then set ->next to point to the body and switch to
6289 * DATA or TRAILERS state.
6290 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006291 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006292 if (ret == 0)
6293 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006294 if (ret < 0) {
6295 msg->err_pos = chn->buf->i + ret;
6296 if (msg->err_pos < 0)
6297 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006298 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006299 }
6300
6301 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006302 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006303 msg->chunk_len = chunk;
6304 msg->body_len += chunk;
6305
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006306 if (msg->chunk_len) {
6307 msg->msg_state = HTTP_MSG_DATA;
6308 goto switch_states;
6309 }
6310 msg->msg_state = HTTP_MSG_TRAILERS;
6311 /* fall through for HTTP_MSG_TRAILERS */
6312
6313 case HTTP_MSG_TRAILERS:
6314 ret = http_forward_trailers(msg);
6315 if (ret < 0)
6316 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006317 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6318 /* default_ret */ 1,
6319 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006320 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006321 if (!ret)
6322 goto missing_data_or_waiting;
6323 break;
6324
6325 case HTTP_MSG_ENDING:
6326 goto ending;
6327
6328 default:
6329 /* This should no happen in this function */
6330 goto error;
6331 }
6332
6333 msg->msg_state = HTTP_MSG_ENDING;
6334 ending:
6335 /* we may have some pending data starting at res->buf->p such as a last
6336 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006337 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006338 /* default_ret */ msg->next,
6339 /* on_error */ goto error);
6340 b_adv(chn->buf, ret);
6341 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006342 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6343 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006344 if (msg->next)
6345 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006346
Christopher Fauletda02e172015-12-04 09:25:05 +01006347 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006348 /* default_ret */ 1,
6349 /* on_error */ goto error,
6350 /* on_wait */ goto waiting);
6351 msg->msg_state = HTTP_MSG_DONE;
6352 return 1;
6353
6354 missing_data_or_waiting:
6355 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006356 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006357 /* default_ret */ msg->next,
6358 /* on_error */ goto error);
6359 b_adv(chn->buf, ret);
6360 msg->next -= ret;
6361 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6362 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006363 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006364 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006365 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006366 return 0;
6367
6368 chunk_parsing_error:
6369 if (msg->err_pos >= 0) {
6370 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006371 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006372 msg->msg_state, strm_fe(s));
6373 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006374 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006375 msg, msg->msg_state, s->be);
6376 }
6377 error:
6378 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006379}
6380
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382/* Iterate the same filter through all request headers.
6383 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006384 * Since it can manage the switch to another backend, it updates the per-proxy
6385 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006387int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006388{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 char *cur_ptr, *cur_end, *cur_next;
6390 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006391 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006392 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006393 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006395 last_hdr = 0;
6396
Willy Tarreau9b28e032012-10-12 23:49:43 +02006397 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006398 old_idx = 0;
6399
6400 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006401 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006403 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006404 (exp->action == ACT_ALLOW ||
6405 exp->action == ACT_DENY ||
6406 exp->action == ACT_TARPIT))
6407 return 0;
6408
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006409 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006410 if (!cur_idx)
6411 break;
6412
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006413 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006414 cur_ptr = cur_next;
6415 cur_end = cur_ptr + cur_hdr->len;
6416 cur_next = cur_end + cur_hdr->cr + 1;
6417
6418 /* Now we have one header between cur_ptr and cur_end,
6419 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006420 */
6421
Willy Tarreau15a53a42015-01-21 13:39:42 +01006422 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006423 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006425 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426 last_hdr = 1;
6427 break;
6428
6429 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006430 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006431 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 break;
6433
6434 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006435 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006436 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006437 break;
6438
6439 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006440 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6441 if (trash.len < 0)
6442 return -1;
6443
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006444 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006445 /* FIXME: if the user adds a newline in the replacement, the
6446 * index will not be recalculated for now, and the new line
6447 * will not be counted as a new header.
6448 */
6449
6450 cur_end += delta;
6451 cur_next += delta;
6452 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006453 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 break;
6455
6456 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006457 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006458 cur_next += delta;
6459
Willy Tarreaufa355d42009-11-29 18:12:29 +01006460 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006461 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6462 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 cur_hdr->len = 0;
6464 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006465 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 break;
6467
6468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006469 }
6470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006471 /* keep the link from this header to next one in case of later
6472 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006474 old_idx = cur_idx;
6475 }
6476 return 0;
6477}
6478
6479
6480/* Apply the filter to the request line.
6481 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6482 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006483 * Since it can manage the switch to another backend, it updates the per-proxy
6484 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006486int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006487{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 char *cur_ptr, *cur_end;
6489 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006490 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006491 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006492
Willy Tarreau3d300592007-03-18 18:34:41 +01006493 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006494 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006495 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496 (exp->action == ACT_ALLOW ||
6497 exp->action == ACT_DENY ||
6498 exp->action == ACT_TARPIT))
6499 return 0;
6500 else if (exp->action == ACT_REMOVE)
6501 return 0;
6502
6503 done = 0;
6504
Willy Tarreau9b28e032012-10-12 23:49:43 +02006505 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006506 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507
6508 /* Now we have the request line between cur_ptr and cur_end */
6509
Willy Tarreau15a53a42015-01-21 13:39:42 +01006510 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006512 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006513 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 done = 1;
6515 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006517 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006518 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 done = 1;
6520 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006523 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 done = 1;
6525 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006527 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006528 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6529 if (trash.len < 0)
6530 return -1;
6531
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006532 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 /* FIXME: if the user adds a newline in the replacement, the
6534 * index will not be recalculated for now, and the new line
6535 * will not be counted as a new header.
6536 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006537
Willy Tarreaufa355d42009-11-29 18:12:29 +01006538 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006540 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 HTTP_MSG_RQMETH,
6542 cur_ptr, cur_end + 1,
6543 NULL, NULL);
6544 if (unlikely(!cur_end))
6545 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006547 /* we have a full request and we know that we have either a CR
6548 * or an LF at <ptr>.
6549 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006550 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6551 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552 /* there is no point trying this regex on headers */
6553 return 1;
6554 }
6555 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 return done;
6557}
Willy Tarreau97de6242006-12-27 17:18:38 +01006558
Willy Tarreau58f10d72006-12-04 02:26:12 +01006559
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006562 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564 * unparsable request. Since it can manage the switch to another backend, it
6565 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006567int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568{
Willy Tarreau192252e2015-04-04 01:47:55 +02006569 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006570 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006571 struct hdr_exp *exp;
6572
6573 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006574 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006576 /*
6577 * The interleaving of transformations and verdicts
6578 * makes it difficult to decide to continue or stop
6579 * the evaluation.
6580 */
6581
Willy Tarreau6c123b12010-01-28 20:22:06 +01006582 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6583 break;
6584
Willy Tarreau3d300592007-03-18 18:34:41 +01006585 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006586 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006587 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006588 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006589
6590 /* if this filter had a condition, evaluate it now and skip to
6591 * next filter if the condition does not match.
6592 */
6593 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006594 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006595 ret = acl_pass(ret);
6596 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6597 ret = !ret;
6598
6599 if (!ret)
6600 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006601 }
6602
6603 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006604 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006605 if (unlikely(ret < 0))
6606 return -1;
6607
6608 if (likely(ret == 0)) {
6609 /* The filter did not match the request, it can be
6610 * iterated through all headers.
6611 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006612 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6613 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006615 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006616 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006617}
6618
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006619
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006620/* Find the end of a cookie value contained between <s> and <e>. It works the
6621 * same way as with headers above except that the semi-colon also ends a token.
6622 * See RFC2965 for more information. Note that it requires a valid header to
6623 * return a valid result.
6624 */
6625char *find_cookie_value_end(char *s, const char *e)
6626{
6627 int quoted, qdpair;
6628
6629 quoted = qdpair = 0;
6630 for (; s < e; s++) {
6631 if (qdpair) qdpair = 0;
6632 else if (quoted) {
6633 if (*s == '\\') qdpair = 1;
6634 else if (*s == '"') quoted = 0;
6635 }
6636 else if (*s == '"') quoted = 1;
6637 else if (*s == ',' || *s == ';') return s;
6638 }
6639 return s;
6640}
6641
6642/* Delete a value in a header between delimiters <from> and <next> in buffer
6643 * <buf>. The number of characters displaced is returned, and the pointer to
6644 * the first delimiter is updated if required. The function tries as much as
6645 * possible to respect the following principles :
6646 * - replace <from> delimiter by the <next> one unless <from> points to a
6647 * colon, in which case <next> is simply removed
6648 * - set exactly one space character after the new first delimiter, unless
6649 * there are not enough characters in the block being moved to do so.
6650 * - remove unneeded spaces before the previous delimiter and after the new
6651 * one.
6652 *
6653 * It is the caller's responsibility to ensure that :
6654 * - <from> points to a valid delimiter or the colon ;
6655 * - <next> points to a valid delimiter or the final CR/LF ;
6656 * - there are non-space chars before <from> ;
6657 * - there is a CR/LF at or after <next>.
6658 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006659int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660{
6661 char *prev = *from;
6662
6663 if (*prev == ':') {
6664 /* We're removing the first value, preserve the colon and add a
6665 * space if possible.
6666 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006667 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006668 next++;
6669 prev++;
6670 if (prev < next)
6671 *prev++ = ' ';
6672
Willy Tarreau2235b262016-11-05 15:50:20 +01006673 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006674 next++;
6675 } else {
6676 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006677 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006678 prev--;
6679 *from = prev;
6680
6681 /* copy the delimiter and if possible a space if we're
6682 * not at the end of the line.
6683 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006684 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006685 *prev++ = *next++;
6686 if (prev + 1 < next)
6687 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006688 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 next++;
6690 }
6691 }
6692 return buffer_replace2(buf, prev, next, NULL, 0);
6693}
6694
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006695/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006696 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006697 * desirable to call it only when needed. This code is quite complex because
6698 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6699 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006700 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006701void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006702{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006703 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006704 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006705 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006706 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006707 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6708 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006709
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006710 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006711 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006712 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006713
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006714 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006715 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006716 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006717
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006718 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 hdr_beg = hdr_next;
6720 hdr_end = hdr_beg + cur_hdr->len;
6721 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006722
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006723 /* We have one full header between hdr_beg and hdr_end, and the
6724 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006725 * "Cookie:" headers.
6726 */
6727
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006729 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006730 old_idx = cur_idx;
6731 continue;
6732 }
6733
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006734 del_from = NULL; /* nothing to be deleted */
6735 preserve_hdr = 0; /* assume we may kill the whole header */
6736
Willy Tarreau58f10d72006-12-04 02:26:12 +01006737 /* Now look for cookies. Conforming to RFC2109, we have to support
6738 * attributes whose name begin with a '$', and associate them with
6739 * the right cookie, if we want to delete this cookie.
6740 * So there are 3 cases for each cookie read :
6741 * 1) it's a special attribute, beginning with a '$' : ignore it.
6742 * 2) it's a server id cookie that we *MAY* want to delete : save
6743 * some pointers on it (last semi-colon, beginning of cookie...)
6744 * 3) it's an application cookie : we *MAY* have to delete a previous
6745 * "special" cookie.
6746 * At the end of loop, if a "special" cookie remains, we may have to
6747 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006748 * *MUST* delete it.
6749 *
6750 * Note: RFC2965 is unclear about the processing of spaces around
6751 * the equal sign in the ATTR=VALUE form. A careful inspection of
6752 * the RFC explicitly allows spaces before it, and not within the
6753 * tokens (attrs or values). An inspection of RFC2109 allows that
6754 * too but section 10.1.3 lets one think that spaces may be allowed
6755 * after the equal sign too, resulting in some (rare) buggy
6756 * implementations trying to do that. So let's do what servers do.
6757 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6758 * allowed quoted strings in values, with any possible character
6759 * after a backslash, including control chars and delimitors, which
6760 * causes parsing to become ambiguous. Browsers also allow spaces
6761 * within values even without quotes.
6762 *
6763 * We have to keep multiple pointers in order to support cookie
6764 * removal at the beginning, middle or end of header without
6765 * corrupting the header. All of these headers are valid :
6766 *
6767 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6768 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6769 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6770 * | | | | | | | | |
6771 * | | | | | | | | hdr_end <--+
6772 * | | | | | | | +--> next
6773 * | | | | | | +----> val_end
6774 * | | | | | +-----------> val_beg
6775 * | | | | +--------------> equal
6776 * | | | +----------------> att_end
6777 * | | +---------------------> att_beg
6778 * | +--------------------------> prev
6779 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006780 */
6781
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006782 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6783 /* Iterate through all cookies on this line */
6784
6785 /* find att_beg */
6786 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006787 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006788 att_beg++;
6789
6790 /* find att_end : this is the first character after the last non
6791 * space before the equal. It may be equal to hdr_end.
6792 */
6793 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006794
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006795 while (equal < hdr_end) {
6796 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006798 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006799 continue;
6800 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006801 }
6802
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006803 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6804 * is between <att_beg> and <equal>, both may be identical.
6805 */
6806
6807 /* look for end of cookie if there is an equal sign */
6808 if (equal < hdr_end && *equal == '=') {
6809 /* look for the beginning of the value */
6810 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006811 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006812 val_beg++;
6813
6814 /* find the end of the value, respecting quotes */
6815 next = find_cookie_value_end(val_beg, hdr_end);
6816
6817 /* make val_end point to the first white space or delimitor after the value */
6818 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006819 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006820 val_end--;
6821 } else {
6822 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006823 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006824
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006825 /* We have nothing to do with attributes beginning with '$'. However,
6826 * they will automatically be removed if a header before them is removed,
6827 * since they're supposed to be linked together.
6828 */
6829 if (*att_beg == '$')
6830 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006831
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006832 /* Ignore cookies with no equal sign */
6833 if (equal == next) {
6834 /* This is not our cookie, so we must preserve it. But if we already
6835 * scheduled another cookie for removal, we cannot remove the
6836 * complete header, but we can remove the previous block itself.
6837 */
6838 preserve_hdr = 1;
6839 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006840 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006841 val_end += delta;
6842 next += delta;
6843 hdr_end += delta;
6844 hdr_next += delta;
6845 cur_hdr->len += delta;
6846 http_msg_move_end(&txn->req, delta);
6847 prev = del_from;
6848 del_from = NULL;
6849 }
6850 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006851 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006852
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006853 /* if there are spaces around the equal sign, we need to
6854 * strip them otherwise we'll get trouble for cookie captures,
6855 * or even for rewrites. Since this happens extremely rarely,
6856 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006857 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006858 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6859 int stripped_before = 0;
6860 int stripped_after = 0;
6861
6862 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006863 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006864 equal += stripped_before;
6865 val_beg += stripped_before;
6866 }
6867
6868 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006869 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 val_beg += stripped_after;
6871 stripped_before += stripped_after;
6872 }
6873
6874 val_end += stripped_before;
6875 next += stripped_before;
6876 hdr_end += stripped_before;
6877 hdr_next += stripped_before;
6878 cur_hdr->len += stripped_before;
6879 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006881 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 /* First, let's see if we want to capture this cookie. We check
6884 * that we don't already have a client side cookie, because we
6885 * can only capture one. Also as an optimisation, we ignore
6886 * cookies shorter than the declared name.
6887 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006888 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6889 (val_end - att_beg >= sess->fe->capture_namelen) &&
6890 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 int log_len = val_end - att_beg;
6892
Willy Tarreaubafbe012017-11-24 17:34:44 +01006893 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006894 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006896 if (log_len > sess->fe->capture_len)
6897 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 memcpy(txn->cli_cookie, att_beg, log_len);
6899 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006902
Willy Tarreaubca99692010-10-06 19:25:55 +02006903 /* Persistence cookies in passive, rewrite or insert mode have the
6904 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006906 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006908 * For cookies in prefix mode, the form is :
6909 *
6910 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006911 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006912 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6913 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6914 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006916
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006917 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6918 * have the server ID between val_beg and delim, and the original cookie between
6919 * delim+1 and val_end. Otherwise, delim==val_end :
6920 *
6921 * Cookie: NAME=SRV; # in all but prefix modes
6922 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6923 * | || || | |+-> next
6924 * | || || | +--> val_end
6925 * | || || +---------> delim
6926 * | || |+------------> val_beg
6927 * | || +-------------> att_end = equal
6928 * | |+-----------------> att_beg
6929 * | +------------------> prev
6930 * +-------------------------> hdr_beg
6931 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006932
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006933 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006934 for (delim = val_beg; delim < val_end; delim++)
6935 if (*delim == COOKIE_DELIM)
6936 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006937 } else {
6938 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006939 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006940 /* Now check if the cookie contains a date field, which would
6941 * appear after a vertical bar ('|') just after the server name
6942 * and before the delimiter.
6943 */
6944 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6945 if (vbar1) {
6946 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006947 * right is the last seen date. It is a base64 encoded
6948 * 30-bit value representing the UNIX date since the
6949 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006950 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006951 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006952 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006953 if (val_end - vbar1 >= 5) {
6954 val = b64tos30(vbar1);
6955 if (val > 0)
6956 txn->cookie_last_date = val << 2;
6957 }
6958 /* look for a second vertical bar */
6959 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6960 if (vbar1 && (val_end - vbar1 > 5)) {
6961 val = b64tos30(vbar1 + 1);
6962 if (val > 0)
6963 txn->cookie_first_date = val << 2;
6964 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006965 }
6966 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006967
Willy Tarreauf64d1412010-10-07 20:06:11 +02006968 /* if the cookie has an expiration date and the proxy wants to check
6969 * it, then we do that now. We first check if the cookie is too old,
6970 * then only if it has expired. We detect strict overflow because the
6971 * time resolution here is not great (4 seconds). Cookies with dates
6972 * in the future are ignored if their offset is beyond one day. This
6973 * allows an admin to fix timezone issues without expiring everyone
6974 * and at the same time avoids keeping unwanted side effects for too
6975 * long.
6976 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006977 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6978 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006979 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006980 txn->flags &= ~TX_CK_MASK;
6981 txn->flags |= TX_CK_OLD;
6982 delim = val_beg; // let's pretend we have not found the cookie
6983 txn->cookie_first_date = 0;
6984 txn->cookie_last_date = 0;
6985 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006986 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6987 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006988 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006989 txn->flags &= ~TX_CK_MASK;
6990 txn->flags |= TX_CK_EXPIRED;
6991 delim = val_beg; // let's pretend we have not found the cookie
6992 txn->cookie_first_date = 0;
6993 txn->cookie_last_date = 0;
6994 }
6995
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006996 /* Here, we'll look for the first running server which supports the cookie.
6997 * This allows to share a same cookie between several servers, for example
6998 * to dedicate backup servers to specific servers only.
6999 * However, to prevent clients from sticking to cookie-less backup server
7000 * when they have incidentely learned an empty cookie, we simply ignore
7001 * empty cookies and mark them as invalid.
7002 * The same behaviour is applied when persistence must be ignored.
7003 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007004 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007006
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007007 while (srv) {
7008 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7009 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007010 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007011 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007012 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007013 /* we found the server and we can use it */
7014 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007015 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007016 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007017 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007018 break;
7019 } else {
7020 /* we found a server, but it's down,
7021 * mark it as such and go on in case
7022 * another one is available.
7023 */
7024 txn->flags &= ~TX_CK_MASK;
7025 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007026 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007027 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007028 srv = srv->next;
7029 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007030
Willy Tarreauf64d1412010-10-07 20:06:11 +02007031 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007032 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007033 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007034 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007035 txn->flags |= TX_CK_UNUSED;
7036 else
7037 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007040 /* depending on the cookie mode, we may have to either :
7041 * - delete the complete cookie if we're in insert+indirect mode, so that
7042 * the server never sees it ;
7043 * - remove the server id from the cookie value, and tag the cookie as an
7044 * application cookie so that it does not get accidentely removed later,
7045 * if we're in cookie prefix mode
7046 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007047 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049
Willy Tarreau9b28e032012-10-12 23:49:43 +02007050 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007051 val_end += delta;
7052 next += delta;
7053 hdr_end += delta;
7054 hdr_next += delta;
7055 cur_hdr->len += delta;
7056 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007057
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007058 del_from = NULL;
7059 preserve_hdr = 1; /* we want to keep this cookie */
7060 }
7061 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007062 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007063 del_from = prev;
7064 }
7065 } else {
7066 /* This is not our cookie, so we must preserve it. But if we already
7067 * scheduled another cookie for removal, we cannot remove the
7068 * complete header, but we can remove the previous block itself.
7069 */
7070 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007073 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007074 if (att_beg >= del_from)
7075 att_beg += delta;
7076 if (att_end >= del_from)
7077 att_end += delta;
7078 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007079 val_end += delta;
7080 next += delta;
7081 hdr_end += delta;
7082 hdr_next += delta;
7083 cur_hdr->len += delta;
7084 http_msg_move_end(&txn->req, delta);
7085 prev = del_from;
7086 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007089
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007090 /* continue with next cookie on this header line */
7091 att_beg = next;
7092 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007094 /* There are no more cookies on this line.
7095 * We may still have one (or several) marked for deletion at the
7096 * end of the line. We must do this now in two ways :
7097 * - if some cookies must be preserved, we only delete from the
7098 * mark to the end of line ;
7099 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007104 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007105 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007106 cur_hdr->len += delta;
7107 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007108 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109
7110 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007111 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7112 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007114 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007117 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118 }
7119
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007120 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007122 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123}
7124
7125
Willy Tarreaua15645d2007-03-18 16:22:39 +01007126/* Iterate the same filter through all response headers contained in <rtr>.
7127 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007129int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 char *cur_ptr, *cur_end, *cur_next;
7132 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007133 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007135 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136
7137 last_hdr = 0;
7138
Willy Tarreau9b28e032012-10-12 23:49:43 +02007139 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140 old_idx = 0;
7141
7142 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007143 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007145 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146 (exp->action == ACT_ALLOW ||
7147 exp->action == ACT_DENY))
7148 return 0;
7149
7150 cur_idx = txn->hdr_idx.v[old_idx].next;
7151 if (!cur_idx)
7152 break;
7153
7154 cur_hdr = &txn->hdr_idx.v[cur_idx];
7155 cur_ptr = cur_next;
7156 cur_end = cur_ptr + cur_hdr->len;
7157 cur_next = cur_end + cur_hdr->cr + 1;
7158
7159 /* Now we have one header between cur_ptr and cur_end,
7160 * and the next header starts at cur_next.
7161 */
7162
Willy Tarreau15a53a42015-01-21 13:39:42 +01007163 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 switch (exp->action) {
7165 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007166 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167 last_hdr = 1;
7168 break;
7169
7170 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007171 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172 last_hdr = 1;
7173 break;
7174
7175 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007176 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7177 if (trash.len < 0)
7178 return -1;
7179
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007180 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181 /* FIXME: if the user adds a newline in the replacement, the
7182 * index will not be recalculated for now, and the new line
7183 * will not be counted as a new header.
7184 */
7185
7186 cur_end += delta;
7187 cur_next += delta;
7188 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007189 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 break;
7191
7192 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007193 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 cur_next += delta;
7195
Willy Tarreaufa355d42009-11-29 18:12:29 +01007196 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7198 txn->hdr_idx.used--;
7199 cur_hdr->len = 0;
7200 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007201 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007202 break;
7203
7204 }
7205 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206
7207 /* keep the link from this header to next one in case of later
7208 * removal of next header.
7209 */
7210 old_idx = cur_idx;
7211 }
7212 return 0;
7213}
7214
7215
7216/* Apply the filter to the status line in the response buffer <rtr>.
7217 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7218 * or -1 if a replacement resulted in an invalid status line.
7219 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007220int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222 char *cur_ptr, *cur_end;
7223 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007224 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007225 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226
7227
Willy Tarreau3d300592007-03-18 18:34:41 +01007228 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007230 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 (exp->action == ACT_ALLOW ||
7232 exp->action == ACT_DENY))
7233 return 0;
7234 else if (exp->action == ACT_REMOVE)
7235 return 0;
7236
7237 done = 0;
7238
Willy Tarreau9b28e032012-10-12 23:49:43 +02007239 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007240 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241
7242 /* Now we have the status line between cur_ptr and cur_end */
7243
Willy Tarreau15a53a42015-01-21 13:39:42 +01007244 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 switch (exp->action) {
7246 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007247 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248 done = 1;
7249 break;
7250
7251 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007252 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007253 done = 1;
7254 break;
7255
7256 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007257 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7258 if (trash.len < 0)
7259 return -1;
7260
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007261 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262 /* FIXME: if the user adds a newline in the replacement, the
7263 * index will not be recalculated for now, and the new line
7264 * will not be counted as a new header.
7265 */
7266
Willy Tarreaufa355d42009-11-29 18:12:29 +01007267 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007269 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007270 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 cur_ptr, cur_end + 1,
7272 NULL, NULL);
7273 if (unlikely(!cur_end))
7274 return -1;
7275
7276 /* we have a full respnse and we know that we have either a CR
7277 * or an LF at <ptr>.
7278 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007279 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007280 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 /* there is no point trying this regex on headers */
7282 return 1;
7283 }
7284 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 return done;
7286}
7287
7288
7289
7290/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007291 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007292 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7293 * unparsable response.
7294 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007295int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296{
Willy Tarreau192252e2015-04-04 01:47:55 +02007297 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007298 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007299 struct hdr_exp *exp;
7300
7301 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302 int ret;
7303
7304 /*
7305 * The interleaving of transformations and verdicts
7306 * makes it difficult to decide to continue or stop
7307 * the evaluation.
7308 */
7309
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007310 if (txn->flags & TX_SVDENY)
7311 break;
7312
Willy Tarreau3d300592007-03-18 18:34:41 +01007313 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7315 exp->action == ACT_PASS)) {
7316 exp = exp->next;
7317 continue;
7318 }
7319
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007320 /* if this filter had a condition, evaluate it now and skip to
7321 * next filter if the condition does not match.
7322 */
7323 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007324 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007325 ret = acl_pass(ret);
7326 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7327 ret = !ret;
7328 if (!ret)
7329 continue;
7330 }
7331
Willy Tarreaua15645d2007-03-18 16:22:39 +01007332 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007333 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 if (unlikely(ret < 0))
7335 return -1;
7336
7337 if (likely(ret == 0)) {
7338 /* The filter did not match the response, it can be
7339 * iterated through all headers.
7340 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007341 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7342 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344 }
7345 return 0;
7346}
7347
7348
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007350 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007351 * desirable to call it only when needed. This function is also used when we
7352 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007354void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007356 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007357 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007358 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007359 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007361 char *hdr_beg, *hdr_end, *hdr_next;
7362 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007363
Willy Tarreaua15645d2007-03-18 16:22:39 +01007364 /* Iterate through the headers.
7365 * we start with the start line.
7366 */
7367 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007368 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007369
7370 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7371 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007372 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007373
7374 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007375 hdr_beg = hdr_next;
7376 hdr_end = hdr_beg + cur_hdr->len;
7377 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007378
Willy Tarreau24581ba2010-08-31 22:39:35 +02007379 /* We have one full header between hdr_beg and hdr_end, and the
7380 * next header starts at hdr_next. We're only interested in
7381 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007382 */
7383
Willy Tarreau24581ba2010-08-31 22:39:35 +02007384 is_cookie2 = 0;
7385 prev = hdr_beg + 10;
7386 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007387 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007388 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7389 if (!val) {
7390 old_idx = cur_idx;
7391 continue;
7392 }
7393 is_cookie2 = 1;
7394 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007395 }
7396
Willy Tarreau24581ba2010-08-31 22:39:35 +02007397 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7398 * <prev> points to the colon.
7399 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007400 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007401
Willy Tarreau24581ba2010-08-31 22:39:35 +02007402 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7403 * check-cache is enabled) and we are not interested in checking
7404 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007405 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007406 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 return;
7408
Willy Tarreau24581ba2010-08-31 22:39:35 +02007409 /* OK so now we know we have to process this response cookie.
7410 * The format of the Set-Cookie header is slightly different
7411 * from the format of the Cookie header in that it does not
7412 * support the comma as a cookie delimiter (thus the header
7413 * cannot be folded) because the Expires attribute described in
7414 * the original Netscape's spec may contain an unquoted date
7415 * with a comma inside. We have to live with this because
7416 * many browsers don't support Max-Age and some browsers don't
7417 * support quoted strings. However the Set-Cookie2 header is
7418 * clean.
7419 *
7420 * We have to keep multiple pointers in order to support cookie
7421 * removal at the beginning, middle or end of header without
7422 * corrupting the header (in case of set-cookie2). A special
7423 * pointer, <scav> points to the beginning of the set-cookie-av
7424 * fields after the first semi-colon. The <next> pointer points
7425 * either to the end of line (set-cookie) or next unquoted comma
7426 * (set-cookie2). All of these headers are valid :
7427 *
7428 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7429 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7430 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7431 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7432 * | | | | | | | | | |
7433 * | | | | | | | | +-> next hdr_end <--+
7434 * | | | | | | | +------------> scav
7435 * | | | | | | +--------------> val_end
7436 * | | | | | +--------------------> val_beg
7437 * | | | | +----------------------> equal
7438 * | | | +------------------------> att_end
7439 * | | +----------------------------> att_beg
7440 * | +------------------------------> prev
7441 * +-----------------------------------------> hdr_beg
7442 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443
Willy Tarreau24581ba2010-08-31 22:39:35 +02007444 for (; prev < hdr_end; prev = next) {
7445 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007446
Willy Tarreau24581ba2010-08-31 22:39:35 +02007447 /* find att_beg */
7448 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007449 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007450 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007451
Willy Tarreau24581ba2010-08-31 22:39:35 +02007452 /* find att_end : this is the first character after the last non
7453 * space before the equal. It may be equal to hdr_end.
7454 */
7455 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456
Willy Tarreau24581ba2010-08-31 22:39:35 +02007457 while (equal < hdr_end) {
7458 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7459 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007460 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007461 continue;
7462 att_end = equal;
7463 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007464
Willy Tarreau24581ba2010-08-31 22:39:35 +02007465 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7466 * is between <att_beg> and <equal>, both may be identical.
7467 */
7468
7469 /* look for end of cookie if there is an equal sign */
7470 if (equal < hdr_end && *equal == '=') {
7471 /* look for the beginning of the value */
7472 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007473 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007475
Willy Tarreau24581ba2010-08-31 22:39:35 +02007476 /* find the end of the value, respecting quotes */
7477 next = find_cookie_value_end(val_beg, hdr_end);
7478
7479 /* make val_end point to the first white space or delimitor after the value */
7480 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007481 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007482 val_end--;
7483 } else {
7484 /* <equal> points to next comma, semi-colon or EOL */
7485 val_beg = val_end = next = equal;
7486 }
7487
7488 if (next < hdr_end) {
7489 /* Set-Cookie2 supports multiple cookies, and <next> points to
7490 * a colon or semi-colon before the end. So skip all attr-value
7491 * pairs and look for the next comma. For Set-Cookie, since
7492 * commas are permitted in values, skip to the end.
7493 */
7494 if (is_cookie2)
7495 next = find_hdr_value_end(next, hdr_end);
7496 else
7497 next = hdr_end;
7498 }
7499
7500 /* Now everything is as on the diagram above */
7501
7502 /* Ignore cookies with no equal sign */
7503 if (equal == val_end)
7504 continue;
7505
7506 /* If there are spaces around the equal sign, we need to
7507 * strip them otherwise we'll get trouble for cookie captures,
7508 * or even for rewrites. Since this happens extremely rarely,
7509 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007511 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7512 int stripped_before = 0;
7513 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007514
Willy Tarreau24581ba2010-08-31 22:39:35 +02007515 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007516 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007517 equal += stripped_before;
7518 val_beg += stripped_before;
7519 }
7520
7521 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007522 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007523 val_beg += stripped_after;
7524 stripped_before += stripped_after;
7525 }
7526
7527 val_end += stripped_before;
7528 next += stripped_before;
7529 hdr_end += stripped_before;
7530 hdr_next += stripped_before;
7531 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007532 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007533 }
7534
7535 /* First, let's see if we want to capture this cookie. We check
7536 * that we don't already have a server side cookie, because we
7537 * can only capture one. Also as an optimisation, we ignore
7538 * cookies shorter than the declared name.
7539 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007540 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007541 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007542 (val_end - att_beg >= sess->fe->capture_namelen) &&
7543 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007544 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007545 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007546 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007548 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007549 if (log_len > sess->fe->capture_len)
7550 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007551 memcpy(txn->srv_cookie, att_beg, log_len);
7552 txn->srv_cookie[log_len] = 0;
7553 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007554 }
7555
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007556 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007558 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007559 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7560 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007561 /* assume passive cookie by default */
7562 txn->flags &= ~TX_SCK_MASK;
7563 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564
7565 /* If the cookie is in insert mode on a known server, we'll delete
7566 * this occurrence because we'll insert another one later.
7567 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007568 * a direct access.
7569 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007570 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007571 /* The "preserve" flag was set, we don't want to touch the
7572 * server's cookie.
7573 */
7574 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007575 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007576 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 /* this cookie must be deleted */
7578 if (*prev == ':' && next == hdr_end) {
7579 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007580 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007581 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7582 txn->hdr_idx.used--;
7583 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007584 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 hdr_next += delta;
7586 http_msg_move_end(&txn->rsp, delta);
7587 /* note: while both invalid now, <next> and <hdr_end>
7588 * are still equal, so the for() will stop as expected.
7589 */
7590 } else {
7591 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007592 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007593 next = prev;
7594 hdr_end += delta;
7595 hdr_next += delta;
7596 cur_hdr->len += delta;
7597 http_msg_move_end(&txn->rsp, delta);
7598 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007599 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007600 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007601 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007603 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007604 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605 * with this server since we know it.
7606 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007607 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007608 next += delta;
7609 hdr_end += delta;
7610 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007612 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007613
Willy Tarreauf1348312010-10-07 15:54:11 +02007614 txn->flags &= ~TX_SCK_MASK;
7615 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007616 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007617 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007618 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007619 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007621 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007622 next += delta;
7623 hdr_end += delta;
7624 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007625 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007626 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627
Willy Tarreau827aee92011-03-10 16:55:02 +01007628 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007629 txn->flags &= ~TX_SCK_MASK;
7630 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631 }
7632 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007633 /* that's done for this cookie, check the next one on the same
7634 * line when next != hdr_end (only if is_cookie2).
7635 */
7636 }
7637 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007639 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640}
7641
7642
Willy Tarreaua15645d2007-03-18 16:22:39 +01007643/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007644 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007645 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007646void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007648 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 char *p1, *p2;
7650
7651 char *cur_ptr, *cur_end, *cur_next;
7652 int cur_idx;
7653
Willy Tarreau5df51872007-11-25 16:20:08 +01007654 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655 return;
7656
7657 /* Iterate through the headers.
7658 * we start with the start line.
7659 */
7660 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007661 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007662
7663 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7664 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007665 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007666
7667 cur_hdr = &txn->hdr_idx.v[cur_idx];
7668 cur_ptr = cur_next;
7669 cur_end = cur_ptr + cur_hdr->len;
7670 cur_next = cur_end + cur_hdr->cr + 1;
7671
7672 /* We have one full header between cur_ptr and cur_end, and the
7673 * next header starts at cur_next. We're only interested in
7674 * "Cookie:" headers.
7675 */
7676
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007677 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7678 if (val) {
7679 if ((cur_end - (cur_ptr + val) >= 8) &&
7680 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7681 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7682 return;
7683 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007684 }
7685
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007686 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7687 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688 continue;
7689
7690 /* OK, right now we know we have a cache-control header at cur_ptr */
7691
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007692 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007693
7694 if (p1 >= cur_end) /* no more info */
7695 continue;
7696
7697 /* p1 is at the beginning of the value */
7698 p2 = p1;
7699
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007700 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007701 p2++;
7702
7703 /* we have a complete value between p1 and p2 */
7704 if (p2 < cur_end && *p2 == '=') {
7705 /* we have something of the form no-cache="set-cookie" */
7706 if ((cur_end - p1 >= 21) &&
7707 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7708 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007709 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007710 continue;
7711 }
7712
7713 /* OK, so we know that either p2 points to the end of string or to a comma */
7714 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007715 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007716 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7717 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7718 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007719 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007720 return;
7721 }
7722
7723 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007724 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007725 continue;
7726 }
7727 }
7728}
7729
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730
Willy Tarreaub2513902006-12-17 14:52:38 +01007731/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007732 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007733 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007734 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007735 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007736 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007737 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007738 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007739 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007740int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007741{
7742 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007743 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007744 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007745
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007746 if (!uri_auth)
7747 return 0;
7748
Cyril Bonté70be45d2010-10-12 00:14:35 +02007749 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007750 return 0;
7751
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007752 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007753 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007754 return 0;
7755
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007756 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007757 return 0;
7758
Willy Tarreaub2513902006-12-17 14:52:38 +01007759 return 1;
7760}
7761
Willy Tarreau4076a152009-04-02 15:18:36 +02007762/*
7763 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007764 * By default it tries to report the error position as msg->err_pos. However if
7765 * this one is not set, it will then report msg->next, which is the last known
7766 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007767 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007768 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007769void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007770 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007771 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007772{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007773 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007774 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007775 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007776
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007777 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007778 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007779 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007780 len1 = MIN(len1, es->len);
7781 len2 = es->len - len1; /* remaining data if buffer wraps */
7782
Willy Tarreauf3764b72016-03-31 13:45:10 +02007783 if (!es->buf)
7784 es->buf = malloc(global.tune.bufsize);
7785
7786 if (es->buf) {
7787 memcpy(es->buf, chn->buf->p, len1);
7788 if (len2)
7789 memcpy(es->buf + len1, chn->buf->data, len2);
7790 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007791
Willy Tarreau4076a152009-04-02 15:18:36 +02007792 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007793 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007794 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007795 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007796
Willy Tarreau4076a152009-04-02 15:18:36 +02007797 es->when = date; // user-visible date
7798 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007799 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007800 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007801 if (objt_conn(sess->origin))
7802 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007803 else
7804 memset(&es->src, 0, sizeof(es->src));
7805
Willy Tarreau078272e2010-12-12 12:46:33 +01007806 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007807 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007808 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007809 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007810 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007811 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007812 es->b_out = chn->buf->o;
7813 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007814 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007815 es->m_clen = msg->chunk_len;
7816 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007817 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007818}
Willy Tarreaub2513902006-12-17 14:52:38 +01007819
Willy Tarreau294c4732011-12-16 21:35:50 +01007820/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7821 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7822 * performed over the whole headers. Otherwise it must contain a valid header
7823 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7824 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7825 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7826 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007827 * -1. The value fetch stops at commas, so this function is suited for use with
7828 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007829 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007830 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007831unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007832 struct hdr_idx *idx, int occ,
7833 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007834{
Willy Tarreau294c4732011-12-16 21:35:50 +01007835 struct hdr_ctx local_ctx;
7836 char *ptr_hist[MAX_HDR_HISTORY];
7837 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007838 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007839 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007840
Willy Tarreau294c4732011-12-16 21:35:50 +01007841 if (!ctx) {
7842 local_ctx.idx = 0;
7843 ctx = &local_ctx;
7844 }
7845
Willy Tarreaubce70882009-09-07 11:51:47 +02007846 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007847 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007848 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007849 occ--;
7850 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007851 *vptr = ctx->line + ctx->val;
7852 *vlen = ctx->vlen;
7853 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007854 }
7855 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007856 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007857 }
7858
7859 /* negative occurrence, we scan all the list then walk back */
7860 if (-occ > MAX_HDR_HISTORY)
7861 return 0;
7862
Willy Tarreau294c4732011-12-16 21:35:50 +01007863 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007864 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007865 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7866 len_hist[hist_ptr] = ctx->vlen;
7867 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007868 hist_ptr = 0;
7869 found++;
7870 }
7871 if (-occ > found)
7872 return 0;
7873 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007874 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7875 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7876 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007877 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007878 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007879 if (hist_ptr >= MAX_HDR_HISTORY)
7880 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007881 *vptr = ptr_hist[hist_ptr];
7882 *vlen = len_hist[hist_ptr];
7883 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007884}
7885
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007886/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7887 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7888 * performed over the whole headers. Otherwise it must contain a valid header
7889 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7890 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7891 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7892 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7893 * -1. This function differs from http_get_hdr() in that it only returns full
7894 * line header values and does not stop at commas.
7895 * The return value is 0 if nothing was found, or non-zero otherwise.
7896 */
7897unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7898 struct hdr_idx *idx, int occ,
7899 struct hdr_ctx *ctx, char **vptr, int *vlen)
7900{
7901 struct hdr_ctx local_ctx;
7902 char *ptr_hist[MAX_HDR_HISTORY];
7903 int len_hist[MAX_HDR_HISTORY];
7904 unsigned int hist_ptr;
7905 int found;
7906
7907 if (!ctx) {
7908 local_ctx.idx = 0;
7909 ctx = &local_ctx;
7910 }
7911
7912 if (occ >= 0) {
7913 /* search from the beginning */
7914 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7915 occ--;
7916 if (occ <= 0) {
7917 *vptr = ctx->line + ctx->val;
7918 *vlen = ctx->vlen;
7919 return 1;
7920 }
7921 }
7922 return 0;
7923 }
7924
7925 /* negative occurrence, we scan all the list then walk back */
7926 if (-occ > MAX_HDR_HISTORY)
7927 return 0;
7928
7929 found = hist_ptr = 0;
7930 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7931 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7932 len_hist[hist_ptr] = ctx->vlen;
7933 if (++hist_ptr >= MAX_HDR_HISTORY)
7934 hist_ptr = 0;
7935 found++;
7936 }
7937 if (-occ > found)
7938 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007939
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007940 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007941 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7942 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7943 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007944 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007945 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007946 if (hist_ptr >= MAX_HDR_HISTORY)
7947 hist_ptr -= MAX_HDR_HISTORY;
7948 *vptr = ptr_hist[hist_ptr];
7949 *vlen = len_hist[hist_ptr];
7950 return 1;
7951}
7952
Willy Tarreaubaaee002006-06-26 02:48:02 +02007953/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007954 * Print a debug line with a header. Always stop at the first CR or LF char,
7955 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7956 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007957 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007958void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007959{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007960 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007961 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007962
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007963 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007964 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007965 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007966 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 +02007967
7968 for (max = 0; start + max < end; max++)
7969 if (start[max] == '\r' || start[max] == '\n')
7970 break;
7971
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007972 UBOUND(max, trash.size - trash.len - 3);
7973 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7974 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007975 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007976}
7977
Willy Tarreaueee5b512015-04-03 23:46:31 +02007978
7979/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7980 * The hdr_idx is allocated as well. In case of allocation failure, everything
7981 * allocated is freed and NULL is returned. Otherwise the new transaction is
7982 * assigned to the stream and returned.
7983 */
7984struct http_txn *http_alloc_txn(struct stream *s)
7985{
7986 struct http_txn *txn = s->txn;
7987
7988 if (txn)
7989 return txn;
7990
Willy Tarreaubafbe012017-11-24 17:34:44 +01007991 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007992 if (!txn)
7993 return txn;
7994
7995 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007996 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007997 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007998 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007999 return NULL;
8000 }
8001
8002 s->txn = txn;
8003 return txn;
8004}
8005
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008006void http_txn_reset_req(struct http_txn *txn)
8007{
8008 txn->req.flags = 0;
8009 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8010 txn->req.next = 0;
8011 txn->req.chunk_len = 0LL;
8012 txn->req.body_len = 0LL;
8013 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8014}
8015
8016void http_txn_reset_res(struct http_txn *txn)
8017{
8018 txn->rsp.flags = 0;
8019 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8020 txn->rsp.next = 0;
8021 txn->rsp.chunk_len = 0LL;
8022 txn->rsp.body_len = 0LL;
8023 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8024}
8025
Willy Tarreau0937bc42009-12-22 15:03:09 +01008026/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008027 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008028 * the required fields are properly allocated and that we only need to (re)init
8029 * them. This should be used before processing any new request.
8030 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008031void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008032{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008033 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008034 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008035
8036 txn->flags = 0;
8037 txn->status = -1;
8038
Willy Tarreauf64d1412010-10-07 20:06:11 +02008039 txn->cookie_first_date = 0;
8040 txn->cookie_last_date = 0;
8041
Willy Tarreaueee5b512015-04-03 23:46:31 +02008042 txn->srv_cookie = NULL;
8043 txn->cli_cookie = NULL;
8044 txn->uri = NULL;
8045
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008046 http_txn_reset_req(txn);
8047 http_txn_reset_res(txn);
8048
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008049 txn->req.chn = &s->req;
8050 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008051
8052 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008053
8054 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8055 if (fe->options2 & PR_O2_REQBUG_OK)
8056 txn->req.err_pos = -1; /* let buggy requests pass */
8057
Willy Tarreau0937bc42009-12-22 15:03:09 +01008058 if (txn->hdr_idx.v)
8059 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008060
8061 vars_init(&s->vars_txn, SCOPE_TXN);
8062 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008063}
8064
8065/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008066void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008067{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008068 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008069 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008070
8071 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008072 pool_free(pool_head_requri, txn->uri);
8073 pool_free(pool_head_capture, txn->cli_cookie);
8074 pool_free(pool_head_capture, txn->srv_cookie);
8075 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008076
William Lallemanda73203e2012-03-12 12:48:57 +01008077 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008078 txn->uri = NULL;
8079 txn->srv_cookie = NULL;
8080 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008081
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008082 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008083 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008084 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008085 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008086 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008087 }
8088
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008089 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008090 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008091 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008092 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008093 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008094 }
8095
Willy Tarreau6204cd92016-03-10 16:33:04 +01008096 vars_prune(&s->vars_txn, s->sess, s);
8097 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008098}
8099
8100/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008101void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008102{
8103 http_end_txn(s);
8104 http_init_txn(s);
8105
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008106 /* reinitialise the current rule list pointer to NULL. We are sure that
8107 * any rulelist match the NULL pointer.
8108 */
8109 s->current_rule_list = NULL;
8110
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008111 s->be = strm_fe(s);
8112 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008113 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008114 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008115 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008116 /* re-init store persistence */
8117 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008118 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008119
Willy Tarreau0937bc42009-12-22 15:03:09 +01008120 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008121
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008122 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008123
Willy Tarreau739cfba2010-01-25 23:11:14 +01008124 /* We must trim any excess data from the response buffer, because we
8125 * may have blocked an invalid response from a server that we don't
8126 * want to accidentely forward once we disable the analysers, nor do
8127 * we want those data to come along with next response. A typical
8128 * example of such data would be from a buggy server responding to
8129 * a HEAD with some data, or sending more than the advertised
8130 * content-length.
8131 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008132 if (unlikely(s->res.buf->i))
8133 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008134
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008135 /* Now we can realign the response buffer */
8136 buffer_realign(s->res.buf);
8137
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008138 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008139 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008140
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008141 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008142 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008143
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008144 s->req.rex = TICK_ETERNITY;
8145 s->req.wex = TICK_ETERNITY;
8146 s->req.analyse_exp = TICK_ETERNITY;
8147 s->res.rex = TICK_ETERNITY;
8148 s->res.wex = TICK_ETERNITY;
8149 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008150 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008151}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008152
Sasha Pachev218f0642014-06-16 12:05:59 -06008153void free_http_res_rules(struct list *r)
8154{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008155 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008156
8157 list_for_each_entry_safe(pr, tr, r, list) {
8158 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008159 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008160 free(pr);
8161 }
8162}
8163
8164void free_http_req_rules(struct list *r)
8165{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008166 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008167
8168 list_for_each_entry_safe(pr, tr, r, list) {
8169 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008170 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008171 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008172
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008173 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008174 free(pr);
8175 }
8176}
8177
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008178/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008179struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008180{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008181 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008182 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008183 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008184 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008185
Vincent Bernat02779b62016-04-03 13:48:43 +02008186 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008187 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008188 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008189 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008190 }
8191
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008192 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008193 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008194 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008195 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008196 int code;
8197 int hc;
8198
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008199 if (!strcmp(args[0], "tarpit")) {
8200 rule->action = ACT_HTTP_REQ_TARPIT;
8201 rule->deny_status = HTTP_ERR_500;
8202 }
8203 else {
8204 rule->action = ACT_ACTION_DENY;
8205 rule->deny_status = HTTP_ERR_403;
8206 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008207 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008208 if (strcmp(args[cur_arg], "deny_status") == 0) {
8209 cur_arg++;
8210 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008211 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8212 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008213 goto out_err;
8214 }
8215
8216 code = atol(args[cur_arg]);
8217 cur_arg++;
8218 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8219 if (http_err_codes[hc] == code) {
8220 rule->deny_status = hc;
8221 break;
8222 }
8223 }
8224
8225 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008226 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8227 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008228 }
8229 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008230 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008231 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008232 cur_arg = 1;
8233
8234 while(*args[cur_arg]) {
8235 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008236 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008237 cur_arg+=2;
8238 continue;
8239 } else
8240 break;
8241 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008242 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008243 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008244 cur_arg = 1;
8245
8246 if (!*args[cur_arg] ||
8247 (*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 +01008248 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8249 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008250 goto out_err;
8251 }
8252 rule->arg.nice = atoi(args[cur_arg]);
8253 if (rule->arg.nice < -1024)
8254 rule->arg.nice = -1024;
8255 else if (rule->arg.nice > 1024)
8256 rule->arg.nice = 1024;
8257 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008258 } else if (!strcmp(args[0], "set-tos")) {
8259#ifdef IP_TOS
8260 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008261 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008262 cur_arg = 1;
8263
8264 if (!*args[cur_arg] ||
8265 (*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 +01008266 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8267 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008268 goto out_err;
8269 }
8270
8271 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8272 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008273 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8274 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008275 goto out_err;
8276 }
8277 cur_arg++;
8278#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008279 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 +02008280 goto out_err;
8281#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008282 } else if (!strcmp(args[0], "set-mark")) {
8283#ifdef SO_MARK
8284 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008285 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008286 cur_arg = 1;
8287
8288 if (!*args[cur_arg] ||
8289 (*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 +01008290 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8291 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008292 goto out_err;
8293 }
8294
8295 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8296 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008297 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8298 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008299 goto out_err;
8300 }
8301 cur_arg++;
8302 global.last_checks |= LSTCHK_NETADM;
8303#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008304 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 +02008305 goto out_err;
8306#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008307 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008308 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008309 cur_arg = 1;
8310
8311 if (!*args[cur_arg] ||
8312 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8313 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008314 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8315 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008316 goto out_err;
8317 }
8318 if (strcmp(args[cur_arg], "silent") == 0)
8319 rule->arg.loglevel = -1;
8320 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8321 goto bad_log_level;
8322 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008323 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008324 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008325 cur_arg = 1;
8326
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008327 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8328 (*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 +01008329 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8330 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008331 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008332 }
8333
8334 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8335 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8336 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008337
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008338 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008339 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008340 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 +01008341 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008342 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8343 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008344 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008345 goto out_err;
8346 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008347 free(proxy->conf.lfs_file);
8348 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8349 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008350 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008351 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008352 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008353 cur_arg = 1;
8354
8355 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008356 (*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 +01008357 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8358 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008359 goto out_err;
8360 }
8361
8362 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8363 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8364 LIST_INIT(&rule->arg.hdr_add.fmt);
8365
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008366 error = NULL;
8367 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008368 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8369 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008370 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008371 goto out_err;
8372 }
8373
8374 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008375 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008376 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 +01008377 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008378 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8379 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008380 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008381 goto out_err;
8382 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008383
8384 free(proxy->conf.lfs_file);
8385 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8386 proxy->conf.lfs_line = proxy->conf.args.line;
8387 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008388 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008389 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008390 cur_arg = 1;
8391
8392 if (!*args[cur_arg] ||
8393 (*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 +01008394 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8395 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008396 goto out_err;
8397 }
8398
8399 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8400 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8401
8402 proxy->conf.args.ctx = ARGC_HRQ;
8403 free(proxy->conf.lfs_file);
8404 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8405 proxy->conf.lfs_line = proxy->conf.args.line;
8406 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008407 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8408 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008409 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008410 struct sample_expr *expr;
8411 unsigned int where;
8412 char *err = NULL;
8413
8414 cur_arg = 1;
8415 proxy->conf.args.ctx = ARGC_TRK;
8416
8417 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8418 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008419 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8420 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008421 free(err);
8422 goto out_err;
8423 }
8424
8425 where = 0;
8426 if (proxy->cap & PR_CAP_FE)
8427 where |= SMP_VAL_FE_HRQ_HDR;
8428 if (proxy->cap & PR_CAP_BE)
8429 where |= SMP_VAL_BE_HRQ_HDR;
8430
8431 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008432 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8433 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8434 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8435 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008436 free(expr);
8437 goto out_err;
8438 }
8439
8440 if (strcmp(args[cur_arg], "table") == 0) {
8441 cur_arg++;
8442 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008443 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8444 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008445 free(expr);
8446 goto out_err;
8447 }
8448 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008449 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008450 cur_arg++;
8451 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008452 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008453 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02008454 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008455 } else if (strcmp(args[0], "redirect") == 0) {
8456 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008457 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008458
Willy Tarreaube4653b2015-05-28 15:26:58 +02008459 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008460 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8461 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008462 goto out_err;
8463 }
8464
8465 /* this redirect rule might already contain a parsed condition which
8466 * we'll pass to the http-request rule.
8467 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008468 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008469 rule->arg.redir = redir;
8470 rule->cond = redir->cond;
8471 redir->cond = NULL;
8472 cur_arg = 2;
8473 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008474 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8475 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008476 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008477 /*
8478 * '+ 8' for 'add-acl('
8479 * '- 9' for 'add-acl(' + trailing ')'
8480 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008481 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008482
8483 cur_arg = 1;
8484
8485 if (!*args[cur_arg] ||
8486 (*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 +01008487 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8488 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008489 goto out_err;
8490 }
8491
8492 LIST_INIT(&rule->arg.map.key);
8493 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008494 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008495 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008496 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008497 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8498 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008499 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008500 goto out_err;
8501 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008502 free(proxy->conf.lfs_file);
8503 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8504 proxy->conf.lfs_line = proxy->conf.args.line;
8505 cur_arg += 1;
8506 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8507 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008508 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008509 /*
8510 * '+ 8' for 'del-acl('
8511 * '- 9' for 'del-acl(' + trailing ')'
8512 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008513 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008514
8515 cur_arg = 1;
8516
8517 if (!*args[cur_arg] ||
8518 (*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 +01008519 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8520 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008521 goto out_err;
8522 }
8523
8524 LIST_INIT(&rule->arg.map.key);
8525 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008526 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008527 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008528 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008529 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8530 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008531 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008532 goto out_err;
8533 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008534 free(proxy->conf.lfs_file);
8535 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8536 proxy->conf.lfs_line = proxy->conf.args.line;
8537 cur_arg += 1;
8538 } else if (strncmp(args[0], "del-map", 7) == 0) {
8539 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008540 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008541 /*
8542 * '+ 8' for 'del-map('
8543 * '- 9' for 'del-map(' + trailing ')'
8544 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008545 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008546
8547 cur_arg = 1;
8548
8549 if (!*args[cur_arg] ||
8550 (*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 +01008551 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8552 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008553 goto out_err;
8554 }
8555
8556 LIST_INIT(&rule->arg.map.key);
8557 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008558 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008559 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008560 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008561 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8562 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008563 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008564 goto out_err;
8565 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008566 free(proxy->conf.lfs_file);
8567 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8568 proxy->conf.lfs_line = proxy->conf.args.line;
8569 cur_arg += 1;
8570 } else if (strncmp(args[0], "set-map", 7) == 0) {
8571 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008572 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008573 /*
8574 * '+ 8' for 'set-map('
8575 * '- 9' for 'set-map(' + trailing ')'
8576 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008577 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008578
8579 cur_arg = 1;
8580
8581 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8582 (*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 +01008583 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8584 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008585 goto out_err;
8586 }
8587
8588 LIST_INIT(&rule->arg.map.key);
8589 LIST_INIT(&rule->arg.map.value);
8590 proxy->conf.args.ctx = ARGC_HRQ;
8591
8592 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008593 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008594 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008595 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008596 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8597 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008598 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008599 goto out_err;
8600 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008601
8602 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008603 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008604 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 +01008605 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008606 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8607 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008608 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008609 goto out_err;
8610 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008611 free(proxy->conf.lfs_file);
8612 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8613 proxy->conf.lfs_line = proxy->conf.args.line;
8614
8615 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008616 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8617 char *errmsg = NULL;
8618 cur_arg = 1;
8619 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008620 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008621 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008622 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008623 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8624 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008625 free(errmsg);
8626 goto out_err;
8627 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008628 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008629 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008630 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8631 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8632 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8633 "%s%s, but got '%s'%s.\n",
8634 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008635 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008636 }
8637
8638 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8639 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008640 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008641
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008642 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008643 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8644 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008645 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008646 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008647 }
8648 rule->cond = cond;
8649 }
8650 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008651 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8652 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8653 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008654 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008655 }
8656
8657 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008658 out_err:
8659 free(rule);
8660 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008661}
8662
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008663/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008664struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008665{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008666 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008667 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008668 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008669 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008670
8671 rule = calloc(1, sizeof(*rule));
8672 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008673 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008674 goto out_err;
8675 }
8676
8677 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008678 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008679 cur_arg = 1;
8680 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008681 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008682 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008683 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008684 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008685 cur_arg = 1;
8686
8687 if (!*args[cur_arg] ||
8688 (*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 +01008689 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8690 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008691 goto out_err;
8692 }
8693 rule->arg.nice = atoi(args[cur_arg]);
8694 if (rule->arg.nice < -1024)
8695 rule->arg.nice = -1024;
8696 else if (rule->arg.nice > 1024)
8697 rule->arg.nice = 1024;
8698 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008699 } else if (!strcmp(args[0], "set-tos")) {
8700#ifdef IP_TOS
8701 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008702 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008703 cur_arg = 1;
8704
8705 if (!*args[cur_arg] ||
8706 (*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 +01008707 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8708 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008709 goto out_err;
8710 }
8711
8712 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8713 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008714 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8715 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008716 goto out_err;
8717 }
8718 cur_arg++;
8719#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008720 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 +02008721 goto out_err;
8722#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008723 } else if (!strcmp(args[0], "set-mark")) {
8724#ifdef SO_MARK
8725 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008726 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008727 cur_arg = 1;
8728
8729 if (!*args[cur_arg] ||
8730 (*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 +01008731 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8732 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008733 goto out_err;
8734 }
8735
8736 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8737 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008738 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8739 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008740 goto out_err;
8741 }
8742 cur_arg++;
8743 global.last_checks |= LSTCHK_NETADM;
8744#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008745 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 +02008746 goto out_err;
8747#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008748 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008749 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008750 cur_arg = 1;
8751
8752 if (!*args[cur_arg] ||
8753 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8754 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008755 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8756 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008757 goto out_err;
8758 }
8759 if (strcmp(args[cur_arg], "silent") == 0)
8760 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008761 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008762 goto bad_log_level;
8763 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008764 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008765 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008766 cur_arg = 1;
8767
8768 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8769 (*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 +01008770 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8771 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008772 goto out_err;
8773 }
8774
8775 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8776 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8777 LIST_INIT(&rule->arg.hdr_add.fmt);
8778
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008779 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008780 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008781 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 +01008782 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008783 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8784 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008785 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008786 goto out_err;
8787 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008788 free(proxy->conf.lfs_file);
8789 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8790 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008791 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008792 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008793 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008794 cur_arg = 1;
8795
8796 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008797 (*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 +01008798 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8799 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008800 goto out_err;
8801 }
8802
8803 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8804 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8805 LIST_INIT(&rule->arg.hdr_add.fmt);
8806
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008807 error = NULL;
8808 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008809 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8810 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008811 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008812 goto out_err;
8813 }
8814
8815 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008816 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008817 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 +01008818 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008819 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8820 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008821 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008822 goto out_err;
8823 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008824
8825 free(proxy->conf.lfs_file);
8826 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8827 proxy->conf.lfs_line = proxy->conf.args.line;
8828 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008829 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008830 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008831 cur_arg = 1;
8832
8833 if (!*args[cur_arg] ||
8834 (*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 +01008835 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8836 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008837 goto out_err;
8838 }
8839
8840 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8841 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8842
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008843 proxy->conf.args.ctx = ARGC_HRS;
8844 free(proxy->conf.lfs_file);
8845 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8846 proxy->conf.lfs_line = proxy->conf.args.line;
8847 cur_arg += 1;
8848 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8849 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008850 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008851 /*
8852 * '+ 8' for 'add-acl('
8853 * '- 9' for 'add-acl(' + trailing ')'
8854 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008855 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008856
8857 cur_arg = 1;
8858
8859 if (!*args[cur_arg] ||
8860 (*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 +01008861 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8862 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008863 goto out_err;
8864 }
8865
8866 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008867 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008868 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008869 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008870 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008871 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8872 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008873 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008874 goto out_err;
8875 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008876 free(proxy->conf.lfs_file);
8877 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8878 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008879
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008880 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008881 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8882 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008883 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008884 /*
8885 * '+ 8' for 'del-acl('
8886 * '- 9' for 'del-acl(' + trailing ')'
8887 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008888 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008889
8890 cur_arg = 1;
8891
8892 if (!*args[cur_arg] ||
8893 (*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 +01008894 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8895 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008896 goto out_err;
8897 }
8898
8899 LIST_INIT(&rule->arg.map.key);
8900 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008901 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008902 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008903 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008904 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8905 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008906 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008907 goto out_err;
8908 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008909 free(proxy->conf.lfs_file);
8910 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8911 proxy->conf.lfs_line = proxy->conf.args.line;
8912 cur_arg += 1;
8913 } else if (strncmp(args[0], "del-map", 7) == 0) {
8914 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008915 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008916 /*
8917 * '+ 8' for 'del-map('
8918 * '- 9' for 'del-map(' + trailing ')'
8919 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008920 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008921
8922 cur_arg = 1;
8923
8924 if (!*args[cur_arg] ||
8925 (*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 +01008926 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8927 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008928 goto out_err;
8929 }
8930
8931 LIST_INIT(&rule->arg.map.key);
8932 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008933 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008934 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008935 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008936 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8937 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008938 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008939 goto out_err;
8940 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008941 free(proxy->conf.lfs_file);
8942 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8943 proxy->conf.lfs_line = proxy->conf.args.line;
8944 cur_arg += 1;
8945 } else if (strncmp(args[0], "set-map", 7) == 0) {
8946 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008947 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008948 /*
8949 * '+ 8' for 'set-map('
8950 * '- 9' for 'set-map(' + trailing ')'
8951 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008952 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008953
8954 cur_arg = 1;
8955
8956 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8957 (*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 +01008958 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8959 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008960 goto out_err;
8961 }
8962
8963 LIST_INIT(&rule->arg.map.key);
8964 LIST_INIT(&rule->arg.map.value);
8965
8966 proxy->conf.args.ctx = ARGC_HRS;
8967
8968 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008969 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008970 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008971 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008972 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8973 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008974 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008975 goto out_err;
8976 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008977
8978 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008979 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008980 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 +01008981 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008982 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8983 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008984 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008985 goto out_err;
8986 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008987
8988 free(proxy->conf.lfs_file);
8989 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8990 proxy->conf.lfs_line = proxy->conf.args.line;
8991
8992 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008993 } else if (strcmp(args[0], "redirect") == 0) {
8994 struct redirect_rule *redir;
8995 char *errmsg = NULL;
8996
8997 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008998 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8999 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009000 goto out_err;
9001 }
9002
9003 /* this redirect rule might already contain a parsed condition which
9004 * we'll pass to the http-request rule.
9005 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009006 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009007 rule->arg.redir = redir;
9008 rule->cond = redir->cond;
9009 redir->cond = NULL;
9010 cur_arg = 2;
9011 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009012 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9013 args[0][9] == '\0' && args[0][8] >= '0' &&
9014 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9015 struct sample_expr *expr;
9016 unsigned int where;
9017 char *err = NULL;
9018
9019 cur_arg = 1;
9020 proxy->conf.args.ctx = ARGC_TRK;
9021
9022 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9023 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009024 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9025 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009026 free(err);
9027 goto out_err;
9028 }
9029
9030 where = 0;
9031 if (proxy->cap & PR_CAP_FE)
9032 where |= SMP_VAL_FE_HRS_HDR;
9033 if (proxy->cap & PR_CAP_BE)
9034 where |= SMP_VAL_BE_HRS_HDR;
9035
9036 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009037 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9038 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9039 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9040 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009041 free(expr);
9042 goto out_err;
9043 }
9044
9045 if (strcmp(args[cur_arg], "table") == 0) {
9046 cur_arg++;
9047 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009048 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9049 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009050 free(expr);
9051 goto out_err;
9052 }
9053 /* we copy the table name for now, it will be resolved later */
9054 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9055 cur_arg++;
9056 }
9057 rule->arg.trk_ctr.expr = expr;
9058 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02009059 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009060 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9061 char *errmsg = NULL;
9062 cur_arg = 1;
9063 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009064 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009065 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009066 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009067 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9068 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009069 free(errmsg);
9070 goto out_err;
9071 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009072 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009073 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009074 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9075 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9076 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9077 "%s%s, but got '%s'%s.\n",
9078 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009079 goto out_err;
9080 }
9081
9082 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9083 struct acl_cond *cond;
9084 char *errmsg = NULL;
9085
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009086 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009087 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9088 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009089 free(errmsg);
9090 goto out_err;
9091 }
9092 rule->cond = cond;
9093 }
9094 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009095 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9096 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9097 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009098 goto out_err;
9099 }
9100
9101 return rule;
9102 out_err:
9103 free(rule);
9104 return NULL;
9105}
9106
Willy Tarreau4baae242012-12-27 12:00:31 +01009107/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009108 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009109 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9110 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009111 */
9112struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009113 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009114{
9115 struct redirect_rule *rule;
9116 int cur_arg;
9117 int type = REDIRECT_TYPE_NONE;
9118 int code = 302;
9119 const char *destination = NULL;
9120 const char *cookie = NULL;
9121 int cookie_set = 0;
9122 unsigned int flags = REDIRECT_FLAG_NONE;
9123 struct acl_cond *cond = NULL;
9124
9125 cur_arg = 0;
9126 while (*(args[cur_arg])) {
9127 if (strcmp(args[cur_arg], "location") == 0) {
9128 if (!*args[cur_arg + 1])
9129 goto missing_arg;
9130
9131 type = REDIRECT_TYPE_LOCATION;
9132 cur_arg++;
9133 destination = args[cur_arg];
9134 }
9135 else if (strcmp(args[cur_arg], "prefix") == 0) {
9136 if (!*args[cur_arg + 1])
9137 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009138 type = REDIRECT_TYPE_PREFIX;
9139 cur_arg++;
9140 destination = args[cur_arg];
9141 }
9142 else if (strcmp(args[cur_arg], "scheme") == 0) {
9143 if (!*args[cur_arg + 1])
9144 goto missing_arg;
9145
9146 type = REDIRECT_TYPE_SCHEME;
9147 cur_arg++;
9148 destination = args[cur_arg];
9149 }
9150 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9151 if (!*args[cur_arg + 1])
9152 goto missing_arg;
9153
9154 cur_arg++;
9155 cookie = args[cur_arg];
9156 cookie_set = 1;
9157 }
9158 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9159 if (!*args[cur_arg + 1])
9160 goto missing_arg;
9161
9162 cur_arg++;
9163 cookie = args[cur_arg];
9164 cookie_set = 0;
9165 }
9166 else if (strcmp(args[cur_arg], "code") == 0) {
9167 if (!*args[cur_arg + 1])
9168 goto missing_arg;
9169
9170 cur_arg++;
9171 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009172 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009173 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009174 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009175 args[cur_arg - 1], args[cur_arg]);
9176 return NULL;
9177 }
9178 }
9179 else if (!strcmp(args[cur_arg],"drop-query")) {
9180 flags |= REDIRECT_FLAG_DROP_QS;
9181 }
9182 else if (!strcmp(args[cur_arg],"append-slash")) {
9183 flags |= REDIRECT_FLAG_APPEND_SLASH;
9184 }
9185 else if (strcmp(args[cur_arg], "if") == 0 ||
9186 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009187 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009188 if (!cond) {
9189 memprintf(errmsg, "error in condition: %s", *errmsg);
9190 return NULL;
9191 }
9192 break;
9193 }
9194 else {
9195 memprintf(errmsg,
9196 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9197 args[cur_arg]);
9198 return NULL;
9199 }
9200 cur_arg++;
9201 }
9202
9203 if (type == REDIRECT_TYPE_NONE) {
9204 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9205 return NULL;
9206 }
9207
Willy Tarreaube4653b2015-05-28 15:26:58 +02009208 if (dir && type != REDIRECT_TYPE_LOCATION) {
9209 memprintf(errmsg, "response only supports redirect type 'location'");
9210 return NULL;
9211 }
9212
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009213 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009214 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009215 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009216
9217 if (!use_fmt) {
9218 /* old-style static redirect rule */
9219 rule->rdr_str = strdup(destination);
9220 rule->rdr_len = strlen(destination);
9221 }
9222 else {
9223 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009224
9225 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9226 * if prefix == "/", we don't want to add anything, otherwise it
9227 * makes it hard for the user to configure a self-redirection.
9228 */
Godbachd9722032014-12-18 15:44:58 +08009229 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009230 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009231 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9232 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 +01009233 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9234 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009235 return NULL;
9236 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009237 free(curproxy->conf.lfs_file);
9238 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9239 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009240 }
9241 }
9242
Willy Tarreau4baae242012-12-27 12:00:31 +01009243 if (cookie) {
9244 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9245 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9246 */
9247 rule->cookie_len = strlen(cookie);
9248 if (cookie_set) {
9249 rule->cookie_str = malloc(rule->cookie_len + 10);
9250 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9251 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9252 rule->cookie_len += 9;
9253 } else {
9254 rule->cookie_str = malloc(rule->cookie_len + 21);
9255 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9256 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9257 rule->cookie_len += 20;
9258 }
9259 }
9260 rule->type = type;
9261 rule->code = code;
9262 rule->flags = flags;
9263 LIST_INIT(&rule->list);
9264 return rule;
9265
9266 missing_arg:
9267 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9268 return NULL;
9269}
9270
Willy Tarreau8797c062007-05-07 00:55:35 +02009271/************************************************************************/
9272/* The code below is dedicated to ACL parsing and matching */
9273/************************************************************************/
9274
9275
Willy Tarreau14174bc2012-04-16 14:34:04 +02009276/* This function ensures that the prerequisites for an L7 fetch are ready,
9277 * which means that a request or response is ready. If some data is missing,
9278 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009279 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9280 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009281 *
9282 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009283 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9284 * decide whether or not an HTTP message is present ;
9285 * 0 if the requested data cannot be fetched or if it is certain that
9286 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009287 * 1 if an HTTP message is ready
9288 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009289int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009290 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009291{
Willy Tarreau192252e2015-04-04 01:47:55 +02009292 struct http_txn *txn;
9293 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009294
Willy Tarreaube508f12016-03-10 11:47:01 +01009295 /* Note: it is possible that <s> is NULL when called before stream
9296 * initialization (eg: tcp-request connection), so this function is the
9297 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009298 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009299 if (!s)
9300 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009301
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009302 if (!s->txn) {
9303 if (unlikely(!http_alloc_txn(s)))
9304 return 0; /* not enough memory */
9305 http_init_txn(s);
9306 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009307 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009308 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009309
9310 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009311 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009312
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009313 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009314 /* If the buffer does not leave enough free space at the end,
9315 * we must first realign it.
9316 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009317 if (s->req.buf->p > s->req.buf->data &&
9318 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9319 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009320
Willy Tarreau14174bc2012-04-16 14:34:04 +02009321 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009322 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009323 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009324
9325 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009326 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009327 http_msg_analyzer(msg, &txn->hdr_idx);
9328
9329 /* Still no valid request ? */
9330 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009331 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009332 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009333 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009334 }
9335 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009336 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009337 return 0;
9338 }
9339
9340 /* OK we just got a valid HTTP request. We have some minor
9341 * preparation to perform so that further checks can rely
9342 * on HTTP tests.
9343 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009344
9345 /* If the request was parsed but was too large, we must absolutely
9346 * return an error so that it is not processed. At the moment this
9347 * cannot happen, but if the parsers are to change in the future,
9348 * we want this check to be maintained.
9349 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009350 if (unlikely(s->req.buf->i + s->req.buf->p >
9351 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009352 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009353 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009354 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009355 return 1;
9356 }
9357
Willy Tarreau9b28e032012-10-12 23:49:43 +02009358 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009359 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009360 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009361
Willy Tarreau506d0502013-07-06 13:29:24 +02009362 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9363 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009364 }
9365
Willy Tarreau506d0502013-07-06 13:29:24 +02009366 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009367 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009368 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009369
9370 /* otherwise everything's ready for the request */
9371 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009372 else {
9373 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009374 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9375 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009376 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009377 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009378 }
9379
9380 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009381 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009382 return 1;
9383}
Willy Tarreau8797c062007-05-07 00:55:35 +02009384
Willy Tarreau8797c062007-05-07 00:55:35 +02009385/* 1. Check on METHOD
9386 * We use the pre-parsed method if it is known, and store its number as an
9387 * integer. If it is unknown, we use the pointer and the length.
9388 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009389static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009390{
9391 int len, meth;
9392
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009393 len = strlen(text);
9394 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009395
9396 pattern->val.i = meth;
9397 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009398 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009399 pattern->len = len;
9400 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009401 else {
9402 pattern->ptr.str = NULL;
9403 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009404 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009405 return 1;
9406}
9407
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009408/* This function fetches the method of current HTTP request and stores
9409 * it in the global pattern struct as a chunk. There are two possibilities :
9410 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9411 * in <len> and <ptr> is NULL ;
9412 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9413 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009414 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009415 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009416static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009417smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009418{
9419 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009420 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009421
Willy Tarreau24e32d82012-04-23 23:55:44 +02009422 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009423
Willy Tarreaube508f12016-03-10 11:47:01 +01009424 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009425 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009426 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009427 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009428 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009429 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9430 /* ensure the indexes are not affected */
9431 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009432 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009433 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9434 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009435 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009436 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009437 return 1;
9438}
9439
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009440/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009441static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009442{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009443 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009444 struct pattern_list *lst;
9445 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009446
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009447 list_for_each_entry(lst, &expr->patterns, list) {
9448 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009449
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009450 /* well-known method */
9451 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009452 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009453 return pattern;
9454 else
9455 continue;
9456 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009457
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009458 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009459 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009460 continue;
9461
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009462 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009463 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9464 (!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 +01009465 return pattern;
9466 }
9467 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009468}
9469
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009470static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009471smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009472{
Willy Tarreaube508f12016-03-10 11:47:01 +01009473 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009474 char *ptr;
9475 int len;
9476
Willy Tarreauc0239e02012-04-16 14:42:55 +02009477 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009478
Willy Tarreaube508f12016-03-10 11:47:01 +01009479 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009480 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009481 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009482
9483 while ((len-- > 0) && (*ptr++ != '/'));
9484 if (len <= 0)
9485 return 0;
9486
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009487 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009488 smp->data.u.str.str = ptr;
9489 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009490
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009491 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009492 return 1;
9493}
9494
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009496smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009497{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009498 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009499 char *ptr;
9500 int len;
9501
Willy Tarreauc0239e02012-04-16 14:42:55 +02009502 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009503
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009504 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009505 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9506 return 0;
9507
Willy Tarreau8797c062007-05-07 00:55:35 +02009508 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009509 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009510
9511 while ((len-- > 0) && (*ptr++ != '/'));
9512 if (len <= 0)
9513 return 0;
9514
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009515 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009516 smp->data.u.str.str = ptr;
9517 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009518
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009519 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009520 return 1;
9521}
9522
9523/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009524static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009525smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009526{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009527 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009528 char *ptr;
9529 int len;
9530
Willy Tarreauc0239e02012-04-16 14:42:55 +02009531 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009532
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009533 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009534 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9535 return 0;
9536
Willy Tarreau8797c062007-05-07 00:55:35 +02009537 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009538 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009539
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009540 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009541 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009542 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009543 return 1;
9544}
9545
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009546static int
9547smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9548{
9549 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9550 return 0;
9551
9552 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009553 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009554 return 0;
9555 smp->strm->unique_id[0] = '\0';
9556 }
9557 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9558 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9559
9560 smp->data.type = SMP_T_STR;
9561 smp->data.u.str.str = smp->strm->unique_id;
9562 smp->flags = SMP_F_CONST;
9563 return 1;
9564}
9565
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009566/* Returns a string block containing all headers including the
9567 * empty line wich separes headers from the body. This is useful
9568 * form some headers analysis.
9569 */
9570static int
9571smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9572{
9573 struct http_msg *msg;
9574 struct hdr_idx *idx;
9575 struct http_txn *txn;
9576
9577 CHECK_HTTP_MESSAGE_FIRST();
9578
9579 txn = smp->strm->txn;
9580 idx = &txn->hdr_idx;
9581 msg = &txn->req;
9582
9583 smp->data.type = SMP_T_STR;
9584 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9585 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9586 (msg->chn->buf->p[msg->eoh] == '\r');
9587
9588 return 1;
9589}
9590
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009591/* Returns the header request in a length/value encoded format.
9592 * This is useful for exchanges with the SPOE.
9593 *
9594 * A "length value" is a multibyte code encoding numbers. It uses the
9595 * SPOE format. The encoding is the following:
9596 *
9597 * Each couple "header name" / "header value" is composed
9598 * like this:
9599 * "length value" "header name bytes"
9600 * "length value" "header value bytes"
9601 * When the last header is reached, the header name and the header
9602 * value are empty. Their length are 0
9603 */
9604static int
9605smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9606{
9607 struct http_msg *msg;
9608 struct chunk *temp;
9609 struct hdr_idx *idx;
9610 const char *cur_ptr, *cur_next, *p;
9611 int old_idx, cur_idx;
9612 struct hdr_idx_elem *cur_hdr;
9613 const char *hn, *hv;
9614 int hnl, hvl;
9615 int ret;
9616 struct http_txn *txn;
9617 char *buf;
9618 char *end;
9619
9620 CHECK_HTTP_MESSAGE_FIRST();
9621
9622 temp = get_trash_chunk();
9623 buf = temp->str;
9624 end = temp->str + temp->size;
9625
9626 txn = smp->strm->txn;
9627 idx = &txn->hdr_idx;
9628 msg = &txn->req;
9629
9630 /* Build array of headers. */
9631 old_idx = 0;
9632 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9633 while (1) {
9634 cur_idx = idx->v[old_idx].next;
9635 if (!cur_idx)
9636 break;
9637 old_idx = cur_idx;
9638
9639 cur_hdr = &idx->v[cur_idx];
9640 cur_ptr = cur_next;
9641 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9642
9643 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9644 * and the next header starts at cur_next. We'll check
9645 * this header in the list as well as against the default
9646 * rule.
9647 */
9648
9649 /* look for ': *'. */
9650 hn = cur_ptr;
9651 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9652 if (p >= cur_ptr+cur_hdr->len)
9653 continue;
9654 hnl = p - hn;
9655 p++;
9656 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9657 p++;
9658 if (p >= cur_ptr + cur_hdr->len)
9659 continue;
9660 hv = p;
9661 hvl = cur_ptr + cur_hdr->len-p;
9662
9663 /* encode the header name. */
9664 ret = encode_varint(hnl, &buf, end);
9665 if (ret == -1)
9666 return 0;
9667 if (buf + hnl > end)
9668 return 0;
9669 memcpy(buf, hn, hnl);
9670 buf += hnl;
9671
9672 /* encode and copy the value. */
9673 ret = encode_varint(hvl, &buf, end);
9674 if (ret == -1)
9675 return 0;
9676 if (buf + hvl > end)
9677 return 0;
9678 memcpy(buf, hv, hvl);
9679 buf += hvl;
9680 }
9681
9682 /* encode the end of the header list with empty
9683 * header name and header value.
9684 */
9685 ret = encode_varint(0, &buf, end);
9686 if (ret == -1)
9687 return 0;
9688 ret = encode_varint(0, &buf, end);
9689 if (ret == -1)
9690 return 0;
9691
9692 /* Initialise sample data which will be filled. */
9693 smp->data.type = SMP_T_BIN;
9694 smp->data.u.str.str = temp->str;
9695 smp->data.u.str.len = buf - temp->str;
9696 smp->data.u.str.size = temp->size;
9697
9698 return 1;
9699}
9700
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009701/* returns the longest available part of the body. This requires that the body
9702 * has been waited for using http-buffer-request.
9703 */
9704static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009705smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009706{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009707 struct http_msg *msg;
9708 unsigned long len;
9709 unsigned long block1;
9710 char *body;
9711 struct chunk *temp;
9712
9713 CHECK_HTTP_MESSAGE_FIRST();
9714
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009715 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009716 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009717 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009718 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009719
9720 len = http_body_bytes(msg);
9721 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9722
9723 block1 = len;
9724 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9725 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9726
9727 if (block1 == len) {
9728 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009729 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009730 smp->data.u.str.str = body;
9731 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009732 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9733 }
9734 else {
9735 /* buffer is wrapped, we need to defragment it */
9736 temp = get_trash_chunk();
9737 memcpy(temp->str, body, block1);
9738 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009739 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009740 smp->data.u.str.str = temp->str;
9741 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009742 smp->flags = SMP_F_VOL_TEST;
9743 }
9744 return 1;
9745}
9746
9747
9748/* returns the available length of the body. This requires that the body
9749 * has been waited for using http-buffer-request.
9750 */
9751static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009752smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009753{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009754 struct http_msg *msg;
9755
9756 CHECK_HTTP_MESSAGE_FIRST();
9757
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009758 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009759 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009760 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009761 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009762
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009763 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009764 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009765
9766 smp->flags = SMP_F_VOL_TEST;
9767 return 1;
9768}
9769
9770
9771/* returns the advertised length of the body, or the advertised size of the
9772 * chunks available in the buffer. This requires that the body has been waited
9773 * for using http-buffer-request.
9774 */
9775static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009776smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009777{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009778 struct http_msg *msg;
9779
9780 CHECK_HTTP_MESSAGE_FIRST();
9781
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009782 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009783 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009784 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009785 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009786
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009787 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009788 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009789
9790 smp->flags = SMP_F_VOL_TEST;
9791 return 1;
9792}
9793
9794
Willy Tarreau8797c062007-05-07 00:55:35 +02009795/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009796static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009797smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009798{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009799 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009800
Willy Tarreauc0239e02012-04-16 14:42:55 +02009801 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009802
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009803 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009804 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009805 smp->data.u.str.len = txn->req.sl.rq.u_l;
9806 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009807 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009808 return 1;
9809}
9810
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009811static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009812smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009813{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009814 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009815 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009816
Willy Tarreauc0239e02012-04-16 14:42:55 +02009817 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009818
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009819 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009820 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 +02009821 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009822 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009823
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009824 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009825 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009826 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009827 return 1;
9828}
9829
9830static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009831smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009832{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009833 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009834 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009835
Willy Tarreauc0239e02012-04-16 14:42:55 +02009836 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009837
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009838 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009839 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 +02009840 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9841 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009842
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009843 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009844 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009845 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009846 return 1;
9847}
9848
Willy Tarreau185b5c42012-04-26 15:11:51 +02009849/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9850 * Accepts an optional argument of type string containing the header field name,
9851 * and an optional argument of type signed or unsigned integer to request an
9852 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009853 * headers are considered from the first one. It does not stop on commas and
9854 * returns full lines instead (useful for User-Agent or Date for example).
9855 */
9856static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009857smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009858{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009859 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009860 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009861 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009862 int occ = 0;
9863 const char *name_str = NULL;
9864 int name_len = 0;
9865
9866 if (!ctx) {
9867 /* first call */
9868 ctx = &static_hdr_ctx;
9869 ctx->idx = 0;
9870 smp->ctx.a[0] = ctx;
9871 }
9872
9873 if (args) {
9874 if (args[0].type != ARGT_STR)
9875 return 0;
9876 name_str = args[0].data.str.str;
9877 name_len = args[0].data.str.len;
9878
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009879 if (args[1].type == ARGT_SINT)
9880 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009881 }
9882
9883 CHECK_HTTP_MESSAGE_FIRST();
9884
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009885 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009886 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 +02009887
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009888 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9889 /* search for header from the beginning */
9890 ctx->idx = 0;
9891
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009892 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009893 /* no explicit occurrence and single fetch => last header by default */
9894 occ = -1;
9895
9896 if (!occ)
9897 /* prepare to report multiple occurrences for ACL fetches */
9898 smp->flags |= SMP_F_NOT_LAST;
9899
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009900 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009901 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009902 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 +02009903 return 1;
9904
9905 smp->flags &= ~SMP_F_NOT_LAST;
9906 return 0;
9907}
9908
9909/* 6. Check on HTTP header count. The number of occurrences is returned.
9910 * Accepts exactly 1 argument of type string. It does not stop on commas and
9911 * returns full lines instead (useful for User-Agent or Date for example).
9912 */
9913static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009914smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009915{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009916 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009917 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009918 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009919 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009920 const char *name = NULL;
9921 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009922
Willy Tarreau601a4d12015-04-01 19:16:09 +02009923 if (args && args->type == ARGT_STR) {
9924 name = args->data.str.str;
9925 len = args->data.str.len;
9926 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009927
9928 CHECK_HTTP_MESSAGE_FIRST();
9929
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009930 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009931 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 +02009932
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009933 ctx.idx = 0;
9934 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009935 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009936 cnt++;
9937
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009938 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009939 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009940 smp->flags = SMP_F_VOL_HDR;
9941 return 1;
9942}
9943
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009944static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009945smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009946{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009947 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009948 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009949 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009950 struct chunk *temp;
9951 char del = ',';
9952
9953 if (args && args->type == ARGT_STR)
9954 del = *args[0].data.str.str;
9955
9956 CHECK_HTTP_MESSAGE_FIRST();
9957
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009958 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009959 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 +02009960
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009961 temp = get_trash_chunk();
9962
9963 ctx.idx = 0;
9964 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9965 if (temp->len)
9966 temp->str[temp->len++] = del;
9967 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9968 temp->len += ctx.del;
9969 }
9970
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009971 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009972 smp->data.u.str.str = temp->str;
9973 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009974 smp->flags = SMP_F_VOL_HDR;
9975 return 1;
9976}
9977
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009978/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9979 * Accepts an optional argument of type string containing the header field name,
9980 * and an optional argument of type signed or unsigned integer to request an
9981 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009982 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009983 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009984static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009985smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009986{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009987 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009988 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009989 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009990 int occ = 0;
9991 const char *name_str = NULL;
9992 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009993
Willy Tarreaua890d072013-04-02 12:01:06 +02009994 if (!ctx) {
9995 /* first call */
9996 ctx = &static_hdr_ctx;
9997 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009998 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009999 }
10000
Willy Tarreau185b5c42012-04-26 15:11:51 +020010001 if (args) {
10002 if (args[0].type != ARGT_STR)
10003 return 0;
10004 name_str = args[0].data.str.str;
10005 name_len = args[0].data.str.len;
10006
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010007 if (args[1].type == ARGT_SINT)
10008 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010009 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010010
Willy Tarreaue333ec92012-04-16 16:26:40 +020010011 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010012
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010013 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010014 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 +020010015
Willy Tarreau185b5c42012-04-26 15:11:51 +020010016 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010017 /* search for header from the beginning */
10018 ctx->idx = 0;
10019
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010020 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010021 /* no explicit occurrence and single fetch => last header by default */
10022 occ = -1;
10023
10024 if (!occ)
10025 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010026 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010027
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010028 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010029 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010030 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 +020010031 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010032
Willy Tarreau37406352012-04-23 16:16:37 +020010033 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010034 return 0;
10035}
10036
Willy Tarreauc11416f2007-06-17 16:58:38 +020010037/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010038 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010039 */
10040static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010041smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010042{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010043 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010044 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010045 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010046 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010047 const char *name = NULL;
10048 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010049
Willy Tarreau601a4d12015-04-01 19:16:09 +020010050 if (args && args->type == ARGT_STR) {
10051 name = args->data.str.str;
10052 len = args->data.str.len;
10053 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010054
Willy Tarreaue333ec92012-04-16 16:26:40 +020010055 CHECK_HTTP_MESSAGE_FIRST();
10056
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010057 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010058 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010059
Willy Tarreau33a7e692007-06-10 19:45:56 +020010060 ctx.idx = 0;
10061 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010062 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010063 cnt++;
10064
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010065 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010066 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010067 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010068 return 1;
10069}
10070
Willy Tarreau185b5c42012-04-26 15:11:51 +020010071/* Fetch an HTTP header's integer value. The integer value is returned. It
10072 * takes a mandatory argument of type string and an optional one of type int
10073 * to designate a specific occurrence. It returns an unsigned integer, which
10074 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010075 */
10076static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010077smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010078{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010079 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010080
Willy Tarreauf853c462012-04-23 18:53:56 +020010081 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010082 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010083 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010084 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010085
Willy Tarreaud53e2422012-04-16 17:21:11 +020010086 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010087}
10088
Cyril Bonté69fa9922012-10-25 00:01:06 +020010089/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10090 * and an optional one of type int to designate a specific occurrence.
10091 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010092 */
10093static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010094smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010095{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010096 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010097
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010098 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010099 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010100 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010101 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010102 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010103 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010104 if (smp->data.u.str.len < temp->size - 1) {
10105 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10106 temp->str[smp->data.u.str.len] = '\0';
10107 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010108 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010109 break;
10110 }
10111 }
10112 }
10113
Willy Tarreaud53e2422012-04-16 17:21:11 +020010114 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010115 if (!(smp->flags & SMP_F_NOT_LAST))
10116 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010117 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010118 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010119}
10120
Willy Tarreau737b0c12007-06-10 21:28:46 +020010121/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10122 * the first '/' after the possible hostname, and ends before the possible '?'.
10123 */
10124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010125smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010126{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010127 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010128 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010129
Willy Tarreauc0239e02012-04-16 14:42:55 +020010130 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010131
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010132 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010133 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010134 ptr = http_get_path(txn);
10135 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010136 return 0;
10137
10138 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010139 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010140 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010141
10142 while (ptr < end && *ptr != '?')
10143 ptr++;
10144
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010145 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010146 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010147 return 1;
10148}
10149
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010150/* This produces a concatenation of the first occurrence of the Host header
10151 * followed by the path component if it begins with a slash ('/'). This means
10152 * that '*' will not be added, resulting in exactly the first Host entry.
10153 * If no Host header is found, then the path is returned as-is. The returned
10154 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010155 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010156 */
10157static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010158smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010159{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010160 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010161 char *ptr, *end, *beg;
10162 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010163 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010164
10165 CHECK_HTTP_MESSAGE_FIRST();
10166
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010167 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010168 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010169 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010170 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010171
10172 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010173 temp = get_trash_chunk();
10174 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010175 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010176 smp->data.u.str.str = temp->str;
10177 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010178
10179 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010180 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010181 beg = http_get_path(txn);
10182 if (!beg)
10183 beg = end;
10184
10185 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10186
10187 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010188 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10189 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010190 }
10191
10192 smp->flags = SMP_F_VOL_1ST;
10193 return 1;
10194}
10195
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010196/* This produces a 32-bit hash of the concatenation of the first occurrence of
10197 * the Host header followed by the path component if it begins with a slash ('/').
10198 * This means that '*' will not be added, resulting in exactly the first Host
10199 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010200 * is hashed using the path hash followed by a full avalanche hash and provides a
10201 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010202 * high-traffic sites without having to store whole paths.
10203 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010204int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010205smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010206{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010207 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010208 struct hdr_ctx ctx;
10209 unsigned int hash = 0;
10210 char *ptr, *beg, *end;
10211 int len;
10212
10213 CHECK_HTTP_MESSAGE_FIRST();
10214
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010215 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010216 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010217 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010218 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10219 ptr = ctx.line + ctx.val;
10220 len = ctx.vlen;
10221 while (len--)
10222 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10223 }
10224
10225 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010226 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010227 beg = http_get_path(txn);
10228 if (!beg)
10229 beg = end;
10230
10231 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10232
10233 if (beg < ptr && *beg == '/') {
10234 while (beg < ptr)
10235 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10236 }
10237 hash = full_hash(hash);
10238
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010239 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010240 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010241 smp->flags = SMP_F_VOL_1ST;
10242 return 1;
10243}
10244
Willy Tarreau4a550602012-12-09 14:53:32 +010010245/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010246 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10247 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10248 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010249 * that in environments where IPv6 is insignificant, truncating the output to
10250 * 8 bytes would still work.
10251 */
10252static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010253smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010254{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010255 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010256 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010257
10258 if (!cli_conn)
10259 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010260
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010261 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010262 return 0;
10263
Willy Tarreau47ca5452012-12-23 20:22:19 +010010264 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010265 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010266 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010267
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010268 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010269 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010270 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010271 temp->len += 4;
10272 break;
10273 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010274 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010275 temp->len += 16;
10276 break;
10277 default:
10278 return 0;
10279 }
10280
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010281 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010282 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010283 return 1;
10284}
10285
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010286/* Extracts the query string, which comes after the question mark '?'. If no
10287 * question mark is found, nothing is returned. Otherwise it returns a sample
10288 * of type string carrying the whole query string.
10289 */
10290static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010291smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010292{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010293 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010294 char *ptr, *end;
10295
10296 CHECK_HTTP_MESSAGE_FIRST();
10297
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010298 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010299 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10300 end = ptr + txn->req.sl.rq.u_l;
10301
10302 /* look up the '?' */
10303 do {
10304 if (ptr == end)
10305 return 0;
10306 } while (*ptr++ != '?');
10307
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010308 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010309 smp->data.u.str.str = ptr;
10310 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010311 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10312 return 1;
10313}
10314
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010315static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010316smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010317{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010318 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10319 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10320 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010321
Willy Tarreau24e32d82012-04-23 23:55:44 +020010322 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010323
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010324 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010325 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010326 return 1;
10327}
10328
Willy Tarreau7f18e522010-10-22 20:04:13 +020010329/* return a valid test if the current request is the first one on the connection */
10330static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010331smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010332{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010333 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010334 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010335 return 1;
10336}
10337
Willy Tarreau34db1082012-04-19 17:16:54 +020010338/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010339static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010340smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010341{
10342
Willy Tarreau24e32d82012-04-23 23:55:44 +020010343 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010344 return 0;
10345
Willy Tarreauc0239e02012-04-16 14:42:55 +020010346 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010347
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010348 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010349 return 0;
10350
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010351 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010352 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010353 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010354 return 1;
10355}
Willy Tarreau8797c062007-05-07 00:55:35 +020010356
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010357/* Accepts exactly 1 argument of type userlist */
10358static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010359smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010360{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010361 if (!args || args->type != ARGT_USR)
10362 return 0;
10363
10364 CHECK_HTTP_MESSAGE_FIRST();
10365
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010366 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010367 return 0;
10368
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010369 /* if the user does not belong to the userlist or has a wrong password,
10370 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010371 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010372 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010373 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10374 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010375 return 0;
10376
10377 /* pat_match_auth() will need the user list */
10378 smp->ctx.a[0] = args->data.usr;
10379
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010380 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010381 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010382 smp->data.u.str.str = smp->strm->txn->auth.user;
10383 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010384
10385 return 1;
10386}
10387
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010388/* Try to find the next occurrence of a cookie name in a cookie header value.
10389 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10390 * the cookie value is returned into *value and *value_l, and the function
10391 * returns a pointer to the next pointer to search from if the value was found.
10392 * Otherwise if the cookie was not found, NULL is returned and neither value
10393 * nor value_l are touched. The input <hdr> string should first point to the
10394 * header's value, and the <hdr_end> pointer must point to the first character
10395 * not part of the value. <list> must be non-zero if value may represent a list
10396 * of values (cookie headers). This makes it faster to abort parsing when no
10397 * list is expected.
10398 */
David Carlier4686f792015-09-25 14:10:50 +010010399char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010400extract_cookie_value(char *hdr, const char *hdr_end,
10401 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010402 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010403{
10404 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10405 char *next;
10406
10407 /* we search at least a cookie name followed by an equal, and more
10408 * generally something like this :
10409 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10410 */
10411 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10412 /* Iterate through all cookies on this line */
10413
Willy Tarreau2235b262016-11-05 15:50:20 +010010414 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010415 att_beg++;
10416
10417 /* find att_end : this is the first character after the last non
10418 * space before the equal. It may be equal to hdr_end.
10419 */
10420 equal = att_end = att_beg;
10421
10422 while (equal < hdr_end) {
10423 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10424 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010425 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010426 continue;
10427 att_end = equal;
10428 }
10429
10430 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10431 * is between <att_beg> and <equal>, both may be identical.
10432 */
10433
10434 /* look for end of cookie if there is an equal sign */
10435 if (equal < hdr_end && *equal == '=') {
10436 /* look for the beginning of the value */
10437 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010438 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010439 val_beg++;
10440
10441 /* find the end of the value, respecting quotes */
10442 next = find_cookie_value_end(val_beg, hdr_end);
10443
10444 /* make val_end point to the first white space or delimitor after the value */
10445 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010446 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010447 val_end--;
10448 } else {
10449 val_beg = val_end = next = equal;
10450 }
10451
10452 /* We have nothing to do with attributes beginning with '$'. However,
10453 * they will automatically be removed if a header before them is removed,
10454 * since they're supposed to be linked together.
10455 */
10456 if (*att_beg == '$')
10457 continue;
10458
10459 /* Ignore cookies with no equal sign */
10460 if (equal == next)
10461 continue;
10462
10463 /* Now we have the cookie name between att_beg and att_end, and
10464 * its value between val_beg and val_end.
10465 */
10466
10467 if (att_end - att_beg == cookie_name_l &&
10468 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10469 /* let's return this value and indicate where to go on from */
10470 *value = val_beg;
10471 *value_l = val_end - val_beg;
10472 return next + 1;
10473 }
10474
10475 /* Set-Cookie headers only have the name in the first attr=value part */
10476 if (!list)
10477 break;
10478 }
10479
10480 return NULL;
10481}
10482
William Lallemanda43ba4e2014-01-28 18:14:25 +010010483/* Fetch a captured HTTP request header. The index is the position of
10484 * the "capture" option in the configuration file
10485 */
10486static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010487smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010488{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010489 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010490 int idx;
10491
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010492 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010493 return 0;
10494
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010495 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010496
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010497 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 +010010498 return 0;
10499
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010500 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010501 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010502 smp->data.u.str.str = smp->strm->req_cap[idx];
10503 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010504
10505 return 1;
10506}
10507
10508/* Fetch a captured HTTP response header. The index is the position of
10509 * the "capture" option in the configuration file
10510 */
10511static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010512smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010513{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010514 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010515 int idx;
10516
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010517 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010518 return 0;
10519
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010520 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010521
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010522 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 +010010523 return 0;
10524
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010525 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010526 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010527 smp->data.u.str.str = smp->strm->res_cap[idx];
10528 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010529
10530 return 1;
10531}
10532
William Lallemand65ad6e12014-01-31 15:08:02 +010010533/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10534static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010535smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010536{
10537 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010538 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010539 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010540
Willy Tarreau15e91e12015-04-04 00:52:09 +020010541 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010542 return 0;
10543
William Lallemand96a77852014-02-05 00:30:02 +010010544 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010545
William Lallemand96a77852014-02-05 00:30:02 +010010546 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10547 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010548
William Lallemand96a77852014-02-05 00:30:02 +010010549 temp = get_trash_chunk();
10550 temp->str = txn->uri;
10551 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010552 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010553 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010554 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010555
10556 return 1;
10557
10558}
10559
10560/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10561static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010562smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010563{
10564 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010565 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010566 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010567
Willy Tarreau15e91e12015-04-04 00:52:09 +020010568 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010569 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010570
William Lallemand65ad6e12014-01-31 15:08:02 +010010571 ptr = txn->uri;
10572
10573 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10574 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010575
William Lallemand65ad6e12014-01-31 15:08:02 +010010576 if (!*ptr)
10577 return 0;
10578
10579 ptr++; /* skip the space */
10580
10581 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010582 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010583 if (!ptr)
10584 return 0;
10585 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10586 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010587
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010588 smp->data.u.str = *temp;
10589 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010590 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010591 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010592
10593 return 1;
10594}
10595
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010596/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10597 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10598 */
10599static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010600smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010601{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010602 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010603
Willy Tarreau15e91e12015-04-04 00:52:09 +020010604 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010605 return 0;
10606
10607 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010608 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010609 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010610 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010611
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010612 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010613 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010614 smp->flags = SMP_F_CONST;
10615 return 1;
10616
10617}
10618
10619/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10620 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10621 */
10622static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010623smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010624{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010625 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010626
Willy Tarreau15e91e12015-04-04 00:52:09 +020010627 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010628 return 0;
10629
10630 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010631 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010632 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010633 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010634
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010635 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010636 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010637 smp->flags = SMP_F_CONST;
10638 return 1;
10639
10640}
10641
William Lallemand65ad6e12014-01-31 15:08:02 +010010642
Willy Tarreaue333ec92012-04-16 16:26:40 +020010643/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010644 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010645 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010646 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010647 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010648 * Accepts exactly 1 argument of type string. If the input options indicate
10649 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010650 * The returned sample is of type CSTR. Can be used to parse cookies in other
10651 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010652 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010653int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010654{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010655 struct http_txn *txn;
10656 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010657 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010658 const struct http_msg *msg;
10659 const char *hdr_name;
10660 int hdr_name_len;
10661 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010662 int occ = 0;
10663 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010664
Willy Tarreau24e32d82012-04-23 23:55:44 +020010665 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010666 return 0;
10667
Willy Tarreaua890d072013-04-02 12:01:06 +020010668 if (!ctx) {
10669 /* first call */
10670 ctx = &static_hdr_ctx;
10671 ctx->idx = 0;
10672 smp->ctx.a[2] = ctx;
10673 }
10674
Willy Tarreaue333ec92012-04-16 16:26:40 +020010675 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010676
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010677 txn = smp->strm->txn;
10678 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010679
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010680 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010681 msg = &txn->req;
10682 hdr_name = "Cookie";
10683 hdr_name_len = 6;
10684 } else {
10685 msg = &txn->rsp;
10686 hdr_name = "Set-Cookie";
10687 hdr_name_len = 10;
10688 }
10689
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010690 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010691 /* no explicit occurrence and single fetch => last cookie by default */
10692 occ = -1;
10693
10694 /* OK so basically here, either we want only one value and it's the
10695 * last one, or we want to iterate over all of them and we fetch the
10696 * next one.
10697 */
10698
Willy Tarreau9b28e032012-10-12 23:49:43 +020010699 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010700 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010701 /* search for the header from the beginning, we must first initialize
10702 * the search parameters.
10703 */
Willy Tarreau37406352012-04-23 16:16:37 +020010704 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010705 ctx->idx = 0;
10706 }
10707
Willy Tarreau28376d62012-04-26 21:26:10 +020010708 smp->flags |= SMP_F_VOL_HDR;
10709
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010710 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010711 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10712 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010713 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10714 goto out;
10715
Willy Tarreau24e32d82012-04-23 23:55:44 +020010716 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010717 continue;
10718
Willy Tarreau37406352012-04-23 16:16:37 +020010719 smp->ctx.a[0] = ctx->line + ctx->val;
10720 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010721 }
10722
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010723 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010724 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010725 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010726 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010727 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010728 &smp->data.u.str.str,
10729 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010730 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010731 found = 1;
10732 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010733 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010734 smp->flags |= SMP_F_NOT_LAST;
10735 return 1;
10736 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010737 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010738 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010739 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010740 /* all cookie headers and values were scanned. If we're looking for the
10741 * last occurrence, we may return it now.
10742 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010743 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010744 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010745 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010746}
10747
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010748/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010749 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010750 * multiple cookies may be parsed on the same line. The returned sample is of
10751 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010752 */
10753static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010754smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010755{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010756 struct http_txn *txn;
10757 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010758 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010759 const struct http_msg *msg;
10760 const char *hdr_name;
10761 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010762 int cnt;
10763 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010764 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010765
Willy Tarreau24e32d82012-04-23 23:55:44 +020010766 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010767 return 0;
10768
Willy Tarreaue333ec92012-04-16 16:26:40 +020010769 CHECK_HTTP_MESSAGE_FIRST();
10770
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010771 txn = smp->strm->txn;
10772 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010773
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010774 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010775 msg = &txn->req;
10776 hdr_name = "Cookie";
10777 hdr_name_len = 6;
10778 } else {
10779 msg = &txn->rsp;
10780 hdr_name = "Set-Cookie";
10781 hdr_name_len = 10;
10782 }
10783
Willy Tarreau9b28e032012-10-12 23:49:43 +020010784 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010785 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010786 ctx.idx = 0;
10787 cnt = 0;
10788
10789 while (1) {
10790 /* Note: val_beg == NULL every time we need to fetch a new header */
10791 if (!val_beg) {
10792 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10793 break;
10794
Willy Tarreau24e32d82012-04-23 23:55:44 +020010795 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010796 continue;
10797
10798 val_beg = ctx.line + ctx.val;
10799 val_end = val_beg + ctx.vlen;
10800 }
10801
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010802 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010803 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010804 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010805 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010806 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010807 &smp->data.u.str.str,
10808 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010809 cnt++;
10810 }
10811 }
10812
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010813 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010814 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010815 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010816 return 1;
10817}
10818
Willy Tarreau51539362012-05-08 12:46:28 +020010819/* Fetch an cookie's integer value. The integer value is returned. It
10820 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10821 */
10822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010823smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010824{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010825 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010826
10827 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010828 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010829 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010830 }
10831
10832 return ret;
10833}
10834
Willy Tarreau8797c062007-05-07 00:55:35 +020010835/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010836/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010837/************************************************************************/
10838
David Cournapeau16023ee2010-12-23 20:55:41 +090010839/*
10840 * Given a path string and its length, find the position of beginning of the
10841 * query string. Returns NULL if no query string is found in the path.
10842 *
10843 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10844 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010845 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010846 */
bedis4c75cca2012-10-05 08:38:24 +020010847static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010848{
10849 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010850
bedis4c75cca2012-10-05 08:38:24 +020010851 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010852 return p ? p + 1 : NULL;
10853}
10854
bedis4c75cca2012-10-05 08:38:24 +020010855static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010856{
bedis4c75cca2012-10-05 08:38:24 +020010857 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010858}
10859
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010860/* after increasing a pointer value, it can exceed the first buffer
10861 * size. This function transform the value of <ptr> according with
10862 * the expected position. <chunks> is an array of the one or two
10863 * avalaible chunks. The first value is the start of the first chunk,
10864 * the second value if the end+1 of the first chunks. The third value
10865 * is NULL or the start of the second chunk and the fourth value is
10866 * the end+1 of the second chunk. The function returns 1 if does a
10867 * wrap, else returns 0.
10868 */
10869static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10870{
10871 if (*ptr < chunks[1])
10872 return 0;
10873 if (!chunks[2])
10874 return 0;
10875 *ptr = chunks[2] + ( *ptr - chunks[1] );
10876 return 1;
10877}
10878
David Cournapeau16023ee2010-12-23 20:55:41 +090010879/*
10880 * Given a url parameter, find the starting position of the first occurence,
10881 * or NULL if the parameter is not found.
10882 *
10883 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10884 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010885 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010886 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010887 * or the second chunk. The caller must be check the position before using the
10888 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010889 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010890static const char *
10891find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010892 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010893 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010894{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010895 const char *pos, *last, *equal;
10896 const char **bufs = chunks;
10897 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010898
David Cournapeau16023ee2010-12-23 20:55:41 +090010899
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010900 pos = bufs[0];
10901 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010902 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010903 /* Check the equal. */
10904 equal = pos + url_param_name_l;
10905 if (fix_pointer_if_wrap(chunks, &equal)) {
10906 if (equal >= chunks[3])
10907 return NULL;
10908 } else {
10909 if (equal >= chunks[1])
10910 return NULL;
10911 }
10912 if (*equal == '=') {
10913 if (pos + url_param_name_l > last) {
10914 /* process wrap case, we detect a wrap. In this case, the
10915 * comparison is performed in two parts.
10916 */
10917
10918 /* This is the end, we dont have any other chunk. */
10919 if (bufs != chunks || !bufs[2])
10920 return NULL;
10921
10922 /* Compute the length of each part of the comparison. */
10923 l1 = last - pos;
10924 l2 = url_param_name_l - l1;
10925
10926 /* The second buffer is too short to contain the compared string. */
10927 if (bufs[2] + l2 > bufs[3])
10928 return NULL;
10929
10930 if (memcmp(pos, url_param_name, l1) == 0 &&
10931 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10932 return pos;
10933
10934 /* Perform wrapping and jump the string who fail the comparison. */
10935 bufs += 2;
10936 pos = bufs[0] + l2;
10937 last = bufs[1];
10938
10939 } else {
10940 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010941 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10942 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010943 pos += url_param_name_l + 1;
10944 if (fix_pointer_if_wrap(chunks, &pos))
10945 last = bufs[2];
10946 }
10947 }
10948
10949 while (1) {
10950 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010951 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010952 pos++;
10953 if (pos < last)
10954 break;
10955 /* process buffer wrapping. */
10956 if (bufs != chunks || !bufs[2])
10957 return NULL;
10958 bufs += 2;
10959 pos = bufs[0];
10960 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010961 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010962 pos++;
10963 }
10964 return NULL;
10965}
10966
10967/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010968 * Given a url parameter name and a query string, find the next value.
10969 * An empty url_param_name matches the first available parameter.
10970 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10971 * respectively provide a pointer to the value and its end.
10972 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010973 */
10974static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010975find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010976 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010977 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010978{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010979 const char *arg_start, *qs_end;
10980 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010981
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010982 arg_start = chunks[0];
10983 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010984 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010985 /* Looks for an argument name. */
10986 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010987 url_param_name, url_param_name_l,
10988 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010989 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010990 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010991 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010992 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010993 if (!arg_start)
10994 return 0;
10995
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010996 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010997 while (1) {
10998 /* looks for the first argument. */
10999 value_start = memchr(arg_start, '=', qs_end - arg_start);
11000 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011001 /* Check for wrapping. */
11002 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011003 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011004 chunks[2]) {
11005 arg_start = chunks[2];
11006 qs_end = chunks[3];
11007 continue;
11008 }
11009 return 0;
11010 }
11011 break;
11012 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011013 value_start++;
11014 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011015 else {
11016 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011017 value_start = arg_start + url_param_name_l + 1;
11018
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011019 /* Check for pointer wrapping. */
11020 if (fix_pointer_if_wrap(chunks, &value_start)) {
11021 /* Update the end pointer. */
11022 qs_end = chunks[3];
11023
11024 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011025 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011026 return 0;
11027 }
11028 }
11029
David Cournapeau16023ee2010-12-23 20:55:41 +090011030 value_end = value_start;
11031
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011032 while (1) {
11033 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11034 value_end++;
11035 if (value_end < qs_end)
11036 break;
11037 /* process buffer wrapping. */
11038 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011039 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011040 chunks[2]) {
11041 value_end = chunks[2];
11042 qs_end = chunks[3];
11043 continue;
11044 }
11045 break;
11046 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011047
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011048 *vstart = value_start;
11049 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011050 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011051}
11052
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011053/* This scans a URL-encoded query string. It takes an optionally wrapping
11054 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11055 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11056 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011057 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011058static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011059smp_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 +090011060{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011061 const char *vstart, *vend;
11062 struct chunk *temp;
11063 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011064
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011065 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011066 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011067 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011068 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011069 return 0;
11070
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011071 /* Create sample. If the value is contiguous, return the pointer as CONST,
11072 * if the value is wrapped, copy-it in a buffer.
11073 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011074 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011075 if (chunks[2] &&
11076 vstart >= chunks[0] && vstart <= chunks[1] &&
11077 vend >= chunks[2] && vend <= chunks[3]) {
11078 /* Wrapped case. */
11079 temp = get_trash_chunk();
11080 memcpy(temp->str, vstart, chunks[1] - vstart);
11081 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011082 smp->data.u.str.str = temp->str;
11083 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011084 } else {
11085 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011086 smp->data.u.str.str = (char *)vstart;
11087 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011088 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11089 }
11090
11091 /* Update context, check wrapping. */
11092 chunks[0] = vend;
11093 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11094 chunks[1] = chunks[3];
11095 chunks[2] = NULL;
11096 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011097
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011098 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011099 smp->flags |= SMP_F_NOT_LAST;
11100
David Cournapeau16023ee2010-12-23 20:55:41 +090011101 return 1;
11102}
11103
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011104/* This function iterates over each parameter of the query string. It uses
11105 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011106 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11107 * An optional parameter name is passed in args[0], otherwise any parameter is
11108 * considered. It supports an optional delimiter argument for the beginning of
11109 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011110 */
11111static int
11112smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11113{
11114 struct http_msg *msg;
11115 char delim = '?';
11116 const char *name;
11117 int name_len;
11118
Dragan Dosen26f77e52015-05-25 10:02:11 +020011119 if (!args ||
11120 (args[0].type && args[0].type != ARGT_STR) ||
11121 (args[1].type && args[1].type != ARGT_STR))
11122 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011123
Dragan Dosen26f77e52015-05-25 10:02:11 +020011124 name = "";
11125 name_len = 0;
11126 if (args->type == ARGT_STR) {
11127 name = args->data.str.str;
11128 name_len = args->data.str.len;
11129 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011130
Dragan Dosen26f77e52015-05-25 10:02:11 +020011131 if (args[1].type)
11132 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011133
Dragan Dosen26f77e52015-05-25 10:02:11 +020011134 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011135 CHECK_HTTP_MESSAGE_FIRST();
11136
11137 msg = &smp->strm->txn->req;
11138
11139 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11140 msg->sl.rq.u_l, delim);
11141 if (!smp->ctx.a[0])
11142 return 0;
11143
11144 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011145
11146 /* Assume that the context is filled with NULL pointer
11147 * before the first call.
11148 * smp->ctx.a[2] = NULL;
11149 * smp->ctx.a[3] = NULL;
11150 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011151 }
11152
11153 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11154}
11155
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011156/* This function iterates over each parameter of the body. This requires
11157 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011158 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11159 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11160 * optional second part if the body wraps at the end of the buffer. An optional
11161 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011162 */
11163static int
11164smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11165{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011166 struct http_msg *msg;
11167 unsigned long len;
11168 unsigned long block1;
11169 char *body;
11170 const char *name;
11171 int name_len;
11172
11173 if (!args || (args[0].type && args[0].type != ARGT_STR))
11174 return 0;
11175
11176 name = "";
11177 name_len = 0;
11178 if (args[0].type == ARGT_STR) {
11179 name = args[0].data.str.str;
11180 name_len = args[0].data.str.len;
11181 }
11182
11183 if (!smp->ctx.a[0]) { // first call, find the query string
11184 CHECK_HTTP_MESSAGE_FIRST();
11185
11186 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011187 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011188 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011189 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011190
11191 len = http_body_bytes(msg);
11192 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11193
11194 block1 = len;
11195 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11196 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11197
11198 if (block1 == len) {
11199 /* buffer is not wrapped (or empty) */
11200 smp->ctx.a[0] = body;
11201 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011202
11203 /* Assume that the context is filled with NULL pointer
11204 * before the first call.
11205 * smp->ctx.a[2] = NULL;
11206 * smp->ctx.a[3] = NULL;
11207 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011208 }
11209 else {
11210 /* buffer is wrapped, we need to defragment it */
11211 smp->ctx.a[0] = body;
11212 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011213 smp->ctx.a[2] = msg->chn->buf->data;
11214 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011215 }
11216 }
11217 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11218}
11219
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011220/* Return the signed integer value for the specified url parameter (see url_param
11221 * above).
11222 */
11223static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011224smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011225{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011226 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011227
11228 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011229 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011230 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011231 }
11232
11233 return ret;
11234}
11235
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011236/* This produces a 32-bit hash of the concatenation of the first occurrence of
11237 * the Host header followed by the path component if it begins with a slash ('/').
11238 * This means that '*' will not be added, resulting in exactly the first Host
11239 * entry. If no Host header is found, then the path is used. The resulting value
11240 * is hashed using the url hash followed by a full avalanche hash and provides a
11241 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11242 * high-traffic sites without having to store whole paths.
11243 * this differs from the base32 functions in that it includes the url parameters
11244 * as well as the path
11245 */
11246static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011247smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011248{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011249 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011250 struct hdr_ctx ctx;
11251 unsigned int hash = 0;
11252 char *ptr, *beg, *end;
11253 int len;
11254
11255 CHECK_HTTP_MESSAGE_FIRST();
11256
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011257 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011258 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011259 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011260 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11261 ptr = ctx.line + ctx.val;
11262 len = ctx.vlen;
11263 while (len--)
11264 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11265 }
11266
11267 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011268 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 +000011269 beg = http_get_path(txn);
11270 if (!beg)
11271 beg = end;
11272
11273 for (ptr = beg; ptr < end ; ptr++);
11274
11275 if (beg < ptr && *beg == '/') {
11276 while (beg < ptr)
11277 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11278 }
11279 hash = full_hash(hash);
11280
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011281 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011282 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011283 smp->flags = SMP_F_VOL_1ST;
11284 return 1;
11285}
11286
11287/* This concatenates the source address with the 32-bit hash of the Host and
11288 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11289 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11290 * on the source address length. The URL hash is stored before the address so
11291 * that in environments where IPv6 is insignificant, truncating the output to
11292 * 8 bytes would still work.
11293 */
11294static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011295smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011296{
11297 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011298 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011299
Dragan Dosendb5af612016-06-16 11:23:01 +020011300 if (!cli_conn)
11301 return 0;
11302
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011303 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011304 return 0;
11305
11306 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011307 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11308 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011309
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011310 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011311 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011312 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011313 temp->len += 4;
11314 break;
11315 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011316 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011317 temp->len += 16;
11318 break;
11319 default:
11320 return 0;
11321 }
11322
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011323 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011324 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011325 return 1;
11326}
11327
Willy Tarreau185b5c42012-04-26 15:11:51 +020011328/* This function is used to validate the arguments passed to any "hdr" fetch
11329 * keyword. These keywords support an optional positive or negative occurrence
11330 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11331 * is assumed that the types are already the correct ones. Returns 0 on error,
11332 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11333 * error message in case of error, that the caller is responsible for freeing.
11334 * The initial location must either be freeable or NULL.
11335 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011336int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011337{
11338 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011339 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011340 return 0;
11341 }
11342 return 1;
11343}
11344
Willy Tarreau276fae92013-07-25 14:36:01 +020011345/* takes an UINT value on input supposed to represent the time since EPOCH,
11346 * adds an optional offset found in args[0] and emits a string representing
11347 * the date in RFC-1123/5322 format.
11348 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011349static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011350{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011351 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011352 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11353 struct chunk *temp;
11354 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011355 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011356 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011357
11358 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011359 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011360 curr_date += args[0].data.sint;
11361
11362 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011363 if (!tm)
11364 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011365
11366 temp = get_trash_chunk();
11367 temp->len = snprintf(temp->str, temp->size - temp->len,
11368 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11369 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11370 tm->tm_hour, tm->tm_min, tm->tm_sec);
11371
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011372 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011373 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011374 return 1;
11375}
11376
Thierry FOURNIERad903512014-04-11 17:51:01 +020011377/* Match language range with language tag. RFC2616 14.4:
11378 *
11379 * A language-range matches a language-tag if it exactly equals
11380 * the tag, or if it exactly equals a prefix of the tag such
11381 * that the first tag character following the prefix is "-".
11382 *
11383 * Return 1 if the strings match, else return 0.
11384 */
11385static inline int language_range_match(const char *range, int range_len,
11386 const char *tag, int tag_len)
11387{
11388 const char *end = range + range_len;
11389 const char *tend = tag + tag_len;
11390 while (range < end) {
11391 if (*range == '-' && tag == tend)
11392 return 1;
11393 if (*range != *tag || tag == tend)
11394 return 0;
11395 range++;
11396 tag++;
11397 }
11398 /* Return true only if the last char of the tag is matched. */
11399 return tag == tend;
11400}
11401
11402/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011403static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011404{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011405 const char *al = smp->data.u.str.str;
11406 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011407 const char *token;
11408 int toklen;
11409 int qvalue;
11410 const char *str;
11411 const char *w;
11412 int best_q = 0;
11413
11414 /* Set the constant to the sample, because the output of the
11415 * function will be peek in the constant configuration string.
11416 */
11417 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011418 smp->data.u.str.size = 0;
11419 smp->data.u.str.str = "";
11420 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011421
11422 /* Parse the accept language */
11423 while (1) {
11424
11425 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011426 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011427 al++;
11428 if (al >= end)
11429 break;
11430
11431 /* Start of the fisrt word. */
11432 token = al;
11433
11434 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011435 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011436 al++;
11437 if (al == token)
11438 goto expect_comma;
11439
11440 /* Length of the token. */
11441 toklen = al - token;
11442 qvalue = 1000;
11443
11444 /* Check if the token exists in the list. If the token not exists,
11445 * jump to the next token.
11446 */
11447 str = args[0].data.str.str;
11448 w = str;
11449 while (1) {
11450 if (*str == ';' || *str == '\0') {
11451 if (language_range_match(token, toklen, w, str-w))
11452 goto look_for_q;
11453 if (*str == '\0')
11454 goto expect_comma;
11455 w = str + 1;
11456 }
11457 str++;
11458 }
11459 goto expect_comma;
11460
11461look_for_q:
11462
11463 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011464 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011465 al++;
11466 if (al >= end)
11467 goto process_value;
11468
11469 /* If ',' is found, process the result */
11470 if (*al == ',')
11471 goto process_value;
11472
11473 /* If the character is different from ';', look
11474 * for the end of the header part in best effort.
11475 */
11476 if (*al != ';')
11477 goto expect_comma;
11478
11479 /* Assumes that the char is ';', now expect "q=". */
11480 al++;
11481
11482 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011483 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011484 al++;
11485 if (al >= end)
11486 goto process_value;
11487
11488 /* Expect 'q'. If no 'q', continue in best effort */
11489 if (*al != 'q')
11490 goto process_value;
11491 al++;
11492
11493 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011494 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011495 al++;
11496 if (al >= end)
11497 goto process_value;
11498
11499 /* Expect '='. If no '=', continue in best effort */
11500 if (*al != '=')
11501 goto process_value;
11502 al++;
11503
11504 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011505 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011506 al++;
11507 if (al >= end)
11508 goto process_value;
11509
11510 /* Parse the q value. */
11511 qvalue = parse_qvalue(al, &al);
11512
11513process_value:
11514
11515 /* If the new q value is the best q value, then store the associated
11516 * language in the response. If qvalue is the biggest value (1000),
11517 * break the process.
11518 */
11519 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011520 smp->data.u.str.str = (char *)w;
11521 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011522 if (qvalue >= 1000)
11523 break;
11524 best_q = qvalue;
11525 }
11526
11527expect_comma:
11528
11529 /* Expect comma or end. If the end is detected, quit the loop. */
11530 while (al < end && *al != ',')
11531 al++;
11532 if (al >= end)
11533 break;
11534
11535 /* Comma is found, jump it and restart the analyzer. */
11536 al++;
11537 }
11538
11539 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011540 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11541 smp->data.u.str.str = args[1].data.str.str;
11542 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011543 }
11544
11545 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011546 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011547}
11548
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011549/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011550static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011551{
11552 /* If the constant flag is set or if not size is avalaible at
11553 * the end of the buffer, copy the string in other buffer
11554 * before decoding.
11555 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011556 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011557 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011558 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11559 smp->data.u.str.str = str->str;
11560 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011561 smp->flags &= ~SMP_F_CONST;
11562 }
11563
11564 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011565 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11566 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011567 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011568}
11569
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011570static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11571{
11572 struct proxy *fe = strm_fe(smp->strm);
11573 int idx, i;
11574 struct cap_hdr *hdr;
11575 int len;
11576
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011577 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011578 return 0;
11579
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011580 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011581
11582 /* Check the availibity of the capture id. */
11583 if (idx > fe->nb_req_cap - 1)
11584 return 0;
11585
11586 /* Look for the original configuration. */
11587 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11588 hdr != NULL && i != idx ;
11589 i--, hdr = hdr->next);
11590 if (!hdr)
11591 return 0;
11592
11593 /* check for the memory allocation */
11594 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011595 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011596 if (smp->strm->req_cap[hdr->index] == NULL)
11597 return 0;
11598
11599 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011600 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011601 if (len > hdr->len)
11602 len = hdr->len;
11603
11604 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011605 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011606 smp->strm->req_cap[idx][len] = '\0';
11607
11608 return 1;
11609}
11610
11611static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11612{
11613 struct proxy *fe = strm_fe(smp->strm);
11614 int idx, i;
11615 struct cap_hdr *hdr;
11616 int len;
11617
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011618 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011619 return 0;
11620
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011621 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011622
11623 /* Check the availibity of the capture id. */
11624 if (idx > fe->nb_rsp_cap - 1)
11625 return 0;
11626
11627 /* Look for the original configuration. */
11628 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11629 hdr != NULL && i != idx ;
11630 i--, hdr = hdr->next);
11631 if (!hdr)
11632 return 0;
11633
11634 /* check for the memory allocation */
11635 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011636 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011637 if (smp->strm->res_cap[hdr->index] == NULL)
11638 return 0;
11639
11640 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011641 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011642 if (len > hdr->len)
11643 len = hdr->len;
11644
11645 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011646 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011647 smp->strm->res_cap[idx][len] = '\0';
11648
11649 return 1;
11650}
11651
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011652/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011653 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011654 * the relevant part of the request line accordingly. Then it updates various
11655 * pointers to the next elements which were moved, and the total buffer length.
11656 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011657 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11658 * error, though this can be revisited when this code is finally exploited.
11659 *
11660 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11661 * query string and 3 to replace uri.
11662 *
11663 * In query string case, the mark question '?' must be set at the start of the
11664 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011665 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011666int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011667 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011668{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011669 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011670 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011671 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011672 int delta;
11673
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011674 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011675 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011676 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011677 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11678
11679 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011680 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011681 txn->req.sl.rq.m_l += delta;
11682 txn->req.sl.rq.u += delta;
11683 txn->req.sl.rq.v += delta;
11684 break;
11685
11686 case 1: // path
11687 cur_ptr = http_get_path(txn);
11688 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011689 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011690
11691 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011692 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 +010011693 cur_end++;
11694
11695 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011696 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011697 txn->req.sl.rq.u_l += delta;
11698 txn->req.sl.rq.v += delta;
11699 break;
11700
11701 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011702 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011703 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011704 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11705 while (cur_ptr < cur_end && *cur_ptr != '?')
11706 cur_ptr++;
11707
11708 /* skip the question mark or indicate that we must insert it
11709 * (but only if the format string is not empty then).
11710 */
11711 if (cur_ptr < cur_end)
11712 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011713 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011714 offset = 0;
11715
11716 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011717 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011718 txn->req.sl.rq.u_l += delta;
11719 txn->req.sl.rq.v += delta;
11720 break;
11721
11722 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011723 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011724 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11725
11726 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011727 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011728 txn->req.sl.rq.u_l += delta;
11729 txn->req.sl.rq.v += delta;
11730 break;
11731
11732 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011733 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011734 }
11735
11736 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011737 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011738 txn->req.sl.rq.l += delta;
11739 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011740 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011741 return 0;
11742}
11743
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011744/* This function replace the HTTP status code and the associated message. The
11745 * variable <status> contains the new status code. This function never fails.
11746 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011747void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011748{
11749 struct http_txn *txn = s->txn;
11750 char *cur_ptr, *cur_end;
11751 int delta;
11752 char *res;
11753 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011754 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011755 int msg_len;
11756
11757 chunk_reset(&trash);
11758
11759 res = ultoa_o(status, trash.str, trash.size);
11760 c_l = res - trash.str;
11761
11762 trash.str[c_l] = ' ';
11763 trash.len = c_l + 1;
11764
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011765 /* Do we have a custom reason format string? */
11766 if (msg == NULL)
11767 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011768 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011769 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11770 trash.len += msg_len;
11771
11772 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11773 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11774
11775 /* commit changes and adjust message */
11776 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11777
11778 /* adjust res line offsets and lengths */
11779 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11780 txn->rsp.sl.st.c_l = c_l;
11781 txn->rsp.sl.st.r_l = msg_len;
11782
11783 delta = trash.len - (cur_end - cur_ptr);
11784 txn->rsp.sl.st.l += delta;
11785 txn->hdr_idx.v[0].len += delta;
11786 http_msg_move_end(&txn->rsp, delta);
11787}
11788
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011789/* This function executes one of the set-{method,path,query,uri} actions. It
11790 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011791 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011792 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011793 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11794 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011795 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011796enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011797 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011798{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011799 struct chunk *replace;
11800 enum act_return ret = ACT_RET_ERR;
11801
11802 replace = alloc_trash_chunk();
11803 if (!replace)
11804 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011805
11806 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011807 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011808 replace->str[replace->len++] = '?';
11809 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11810 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011811
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011812 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11813
11814 ret = ACT_RET_CONT;
11815
11816leave:
11817 free_trash_chunk(replace);
11818 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011819}
11820
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011821/* This function is just a compliant action wrapper for "set-status". */
11822enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011823 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011824{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011825 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011826 return ACT_RET_CONT;
11827}
11828
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011829/* parse an http-request action among :
11830 * set-method
11831 * set-path
11832 * set-query
11833 * set-uri
11834 *
11835 * All of them accept a single argument of type string representing a log-format.
11836 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11837 * 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 +020011838 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011839 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011840enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11841 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011842{
11843 int cur_arg = *orig_arg;
11844
Thierry FOURNIER42148732015-09-02 17:17:33 +020011845 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011846
11847 switch (args[0][4]) {
11848 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011849 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011850 rule->action_ptr = http_action_set_req_line;
11851 break;
11852 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011853 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011854 rule->action_ptr = http_action_set_req_line;
11855 break;
11856 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011857 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011858 rule->action_ptr = http_action_set_req_line;
11859 break;
11860 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011861 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011862 rule->action_ptr = http_action_set_req_line;
11863 break;
11864 default:
11865 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011866 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011867 }
11868
11869 if (!*args[cur_arg] ||
11870 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11871 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011872 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011873 }
11874
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011875 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011876 px->conf.args.ctx = ARGC_HRQ;
11877 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
11878 (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 +010011879 return ACT_RET_PRS_ERR;
11880 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011881
11882 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011883 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011884}
11885
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011886/* parse set-status action:
11887 * This action accepts a single argument of type int representing
11888 * an http status code. It returns ACT_RET_PRS_OK on success,
11889 * ACT_RET_PRS_ERR on error.
11890 */
11891enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11892 struct act_rule *rule, char **err)
11893{
11894 char *error;
11895
Thierry FOURNIER42148732015-09-02 17:17:33 +020011896 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011897 rule->action_ptr = action_http_set_status;
11898
11899 /* Check if an argument is available */
11900 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011901 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011902 return ACT_RET_PRS_ERR;
11903 }
11904
11905 /* convert status code as integer */
11906 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11907 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11908 memprintf(err, "expects an integer status code between 100 and 999");
11909 return ACT_RET_PRS_ERR;
11910 }
11911
11912 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011913
11914 /* set custom reason string */
11915 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11916 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11917 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11918 (*orig_arg)++;
11919 rule->arg.status.reason = strdup(args[*orig_arg]);
11920 (*orig_arg)++;
11921 }
11922
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011923 return ACT_RET_PRS_OK;
11924}
11925
Willy Tarreau53275e82017-11-24 07:52:01 +010011926/* This function executes the "reject" HTTP action. It clears the request and
11927 * response buffer without sending any response. It can be useful as an HTTP
11928 * alternative to the silent-drop action to defend against DoS attacks, and may
11929 * also be used with HTTP/2 to close a connection instead of just a stream.
11930 * The txn status is unchanged, indicating no response was sent. The termination
11931 * flags will indicate "PR". It always returns ACT_RET_STOP.
11932 */
11933enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
11934 struct session *sess, struct stream *s, int flags)
11935{
11936 channel_abort(&s->req);
11937 channel_abort(&s->res);
11938 s->req.analysers = 0;
11939 s->res.analysers = 0;
11940
11941 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
11942 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
11943 if (sess->listener && sess->listener->counters)
11944 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
11945
11946 if (!(s->flags & SF_ERR_MASK))
11947 s->flags |= SF_ERR_PRXCOND;
11948 if (!(s->flags & SF_FINST_MASK))
11949 s->flags |= SF_FINST_R;
11950
11951 return ACT_RET_CONT;
11952}
11953
11954/* parse the "reject" action:
11955 * This action takes no argument and returns ACT_RET_PRS_OK on success,
11956 * ACT_RET_PRS_ERR on error.
11957 */
11958enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
11959 struct act_rule *rule, char **err)
11960{
11961 rule->action = ACT_CUSTOM;
11962 rule->action_ptr = http_action_reject;
11963 return ACT_RET_PRS_OK;
11964}
11965
Willy Tarreaua9083d02015-05-08 15:27:59 +020011966/* This function executes the "capture" action. It executes a fetch expression,
11967 * turns the result into a string and puts it in a capture slot. It always
11968 * returns 1. If an error occurs the action is cancelled, but the rule
11969 * processing continues.
11970 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011971enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011972 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011973{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011974 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011975 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011976 char **cap = s->req_cap;
11977 int len;
11978
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011979 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 +020011980 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011981 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011982
11983 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011984 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011985
11986 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011987 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011988
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011989 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011990 if (len > h->len)
11991 len = h->len;
11992
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011993 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011994 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011995 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011996}
11997
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011998/* This function executes the "capture" action and store the result in a
11999 * capture slot if exists. It executes a fetch expression, turns the result
12000 * into a string and puts it in a capture slot. It always returns 1. If an
12001 * error occurs the action is cancelled, but the rule processing continues.
12002 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012003enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012004 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012005{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012006 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012007 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012008 char **cap = s->req_cap;
12009 struct proxy *fe = strm_fe(s);
12010 int len;
12011 int i;
12012
12013 /* Look for the original configuration. */
12014 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012015 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012016 i--, h = h->next);
12017 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012018 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012019
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012020 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 +020012021 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012022 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012023
12024 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012025 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012026
12027 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012028 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012029
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012030 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012031 if (len > h->len)
12032 len = h->len;
12033
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012034 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012035 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012036 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012037}
12038
Christopher Faulet29730ba2017-09-18 15:26:32 +020012039/* Check an "http-request capture" action.
12040 *
12041 * The function returns 1 in success case, otherwise, it returns 0 and err is
12042 * filled.
12043 */
12044int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12045{
12046 if (rule->arg.capid.idx >= px->nb_req_cap) {
12047 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12048 rule->arg.capid.idx);
12049 return 0;
12050 }
12051
12052 return 1;
12053}
12054
Willy Tarreaua9083d02015-05-08 15:27:59 +020012055/* parse an "http-request capture" action. It takes a single argument which is
12056 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012057 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012058 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012059 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012060enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12061 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012062{
12063 struct sample_expr *expr;
12064 struct cap_hdr *hdr;
12065 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012066 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012067
12068 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12069 if (strcmp(args[cur_arg], "if") == 0 ||
12070 strcmp(args[cur_arg], "unless") == 0)
12071 break;
12072
12073 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012074 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012075 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012076 }
12077
Willy Tarreaua9083d02015-05-08 15:27:59 +020012078 cur_arg = *orig_arg;
12079 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12080 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012081 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012082
12083 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12084 memprintf(err,
12085 "fetch method '%s' extracts information from '%s', none of which is available here",
12086 args[cur_arg-1], sample_src_names(expr->fetch->use));
12087 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012088 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012089 }
12090
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012091 if (!args[cur_arg] || !*args[cur_arg]) {
12092 memprintf(err, "expects 'len or 'id'");
12093 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012094 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012095 }
12096
Willy Tarreaua9083d02015-05-08 15:27:59 +020012097 if (strcmp(args[cur_arg], "len") == 0) {
12098 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012099
12100 if (!(px->cap & PR_CAP_FE)) {
12101 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012102 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012103 }
12104
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012105 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012106
Willy Tarreaua9083d02015-05-08 15:27:59 +020012107 if (!args[cur_arg]) {
12108 memprintf(err, "missing length value");
12109 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012110 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012111 }
12112 /* we copy the table name for now, it will be resolved later */
12113 len = atoi(args[cur_arg]);
12114 if (len <= 0) {
12115 memprintf(err, "length must be > 0");
12116 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012117 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012118 }
12119 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012120
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012121 if (!len) {
12122 memprintf(err, "a positive 'len' argument is mandatory");
12123 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012124 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012125 }
12126
Vincent Bernat02779b62016-04-03 13:48:43 +020012127 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012128 hdr->next = px->req_cap;
12129 hdr->name = NULL; /* not a header capture */
12130 hdr->namelen = 0;
12131 hdr->len = len;
12132 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12133 hdr->index = px->nb_req_cap++;
12134
12135 px->req_cap = hdr;
12136 px->to_log |= LW_REQHDR;
12137
Thierry FOURNIER42148732015-09-02 17:17:33 +020012138 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012139 rule->action_ptr = http_action_req_capture;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012140 rule->check_ptr = check_http_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012141 rule->arg.cap.expr = expr;
12142 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012143 }
12144
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012145 else if (strcmp(args[cur_arg], "id") == 0) {
12146 int id;
12147 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012148
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012149 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012150
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012151 if (!args[cur_arg]) {
12152 memprintf(err, "missing id value");
12153 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012154 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012155 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012156
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012157 id = strtol(args[cur_arg], &error, 10);
12158 if (*error != '\0') {
12159 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12160 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012161 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012162 }
12163 cur_arg++;
12164
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012165 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012166
Thierry FOURNIER42148732015-09-02 17:17:33 +020012167 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012168 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012169 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012170 rule->arg.capid.expr = expr;
12171 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012172 }
12173
12174 else {
12175 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12176 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012177 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012178 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012179
12180 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012181 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012182}
12183
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012184/* This function executes the "capture" action and store the result in a
12185 * capture slot if exists. It executes a fetch expression, turns the result
12186 * into a string and puts it in a capture slot. It always returns 1. If an
12187 * error occurs the action is cancelled, but the rule processing continues.
12188 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012189enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012190 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012191{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012192 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012193 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012194 char **cap = s->res_cap;
12195 struct proxy *fe = strm_fe(s);
12196 int len;
12197 int i;
12198
12199 /* Look for the original configuration. */
12200 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012201 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012202 i--, h = h->next);
12203 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012204 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012205
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012206 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 +020012207 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012208 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012209
12210 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012211 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012212
12213 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012214 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012215
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012216 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012217 if (len > h->len)
12218 len = h->len;
12219
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012220 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012221 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012222 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012223}
12224
Christopher Faulet29730ba2017-09-18 15:26:32 +020012225/* Check an "http-response capture" action.
12226 *
12227 * The function returns 1 in success case, otherwise, it returns 0 and err is
12228 * filled.
12229 */
12230int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12231{
12232 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12233 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12234 rule->arg.capid.idx);
12235 return 0;
12236 }
12237
12238 return 1;
12239}
12240
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012241/* parse an "http-response capture" action. It takes a single argument which is
12242 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12243 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012244 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012245 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012246enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12247 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012248{
12249 struct sample_expr *expr;
12250 int cur_arg;
12251 int id;
12252 char *error;
12253
12254 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12255 if (strcmp(args[cur_arg], "if") == 0 ||
12256 strcmp(args[cur_arg], "unless") == 0)
12257 break;
12258
12259 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012260 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012261 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012262 }
12263
12264 cur_arg = *orig_arg;
12265 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12266 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012267 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012268
12269 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12270 memprintf(err,
12271 "fetch method '%s' extracts information from '%s', none of which is available here",
12272 args[cur_arg-1], sample_src_names(expr->fetch->use));
12273 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012274 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012275 }
12276
12277 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012278 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012279 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012280 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012281 }
12282
12283 if (strcmp(args[cur_arg], "id") != 0) {
12284 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12285 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012286 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012287 }
12288
12289 cur_arg++;
12290
12291 if (!args[cur_arg]) {
12292 memprintf(err, "missing id value");
12293 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012294 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012295 }
12296
12297 id = strtol(args[cur_arg], &error, 10);
12298 if (*error != '\0') {
12299 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12300 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012301 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012302 }
12303 cur_arg++;
12304
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012305 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012306
Thierry FOURNIER42148732015-09-02 17:17:33 +020012307 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012308 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012309 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012310 rule->arg.capid.expr = expr;
12311 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012312
12313 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012314 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012315}
12316
William Lallemand73025dd2014-04-24 14:38:37 +020012317/*
12318 * Return the struct http_req_action_kw associated to a keyword.
12319 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012320struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012321{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012322 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012323}
12324
12325/*
12326 * Return the struct http_res_action_kw associated to a keyword.
12327 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012328struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012329{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012330 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012331}
12332
Willy Tarreau12207b32016-11-22 19:48:51 +010012333
12334/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12335 * now.
12336 */
12337static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12338{
12339 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12340 return 1;
12341
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012342 if (*args[2]) {
12343 struct proxy *px;
12344
12345 px = proxy_find_by_name(args[2], 0, 0);
12346 if (px)
12347 appctx->ctx.errors.iid = px->uuid;
12348 else
12349 appctx->ctx.errors.iid = atoi(args[2]);
12350
12351 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012352 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012353 appctx->ctx.cli.msg = "No such proxy.\n";
12354 appctx->st0 = CLI_ST_PRINT;
12355 return 1;
12356 }
12357 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012358 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012359 appctx->ctx.errors.iid = -1; // dump all proxies
12360
Willy Tarreau35069f82016-11-25 09:16:37 +010012361 appctx->ctx.errors.flag = 0;
12362 if (strcmp(args[3], "request") == 0)
12363 appctx->ctx.errors.flag |= 4; // ignore response
12364 else if (strcmp(args[3], "response") == 0)
12365 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012366 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012367 return 0;
12368}
12369
12370/* This function dumps all captured errors onto the stream interface's
12371 * read buffer. It returns 0 if the output buffer is full and it needs
12372 * to be called again, otherwise non-zero.
12373 */
12374static int cli_io_handler_show_errors(struct appctx *appctx)
12375{
12376 struct stream_interface *si = appctx->owner;
12377 extern const char *monthname[12];
12378
12379 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12380 return 1;
12381
12382 chunk_reset(&trash);
12383
12384 if (!appctx->ctx.errors.px) {
12385 /* the function had not been called yet, let's prepare the
12386 * buffer for a response.
12387 */
12388 struct tm tm;
12389
12390 get_localtime(date.tv_sec, &tm);
12391 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12392 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12393 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12394 error_snapshot_id);
12395
Willy Tarreau06d80a92017-10-19 14:32:15 +020012396 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012397 /* Socket buffer full. Let's try again later from the same point */
12398 si_applet_cant_put(si);
12399 return 0;
12400 }
12401
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012402 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012403 appctx->ctx.errors.bol = 0;
12404 appctx->ctx.errors.ptr = -1;
12405 }
12406
12407 /* we have two inner loops here, one for the proxy, the other one for
12408 * the buffer.
12409 */
12410 while (appctx->ctx.errors.px) {
12411 struct error_snapshot *es;
12412
Willy Tarreau35069f82016-11-25 09:16:37 +010012413 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012414 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012415 if (appctx->ctx.errors.flag & 2) // skip req
12416 goto next;
12417 }
12418 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012419 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012420 if (appctx->ctx.errors.flag & 4) // skip resp
12421 goto next;
12422 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012423
12424 if (!es->when.tv_sec)
12425 goto next;
12426
12427 if (appctx->ctx.errors.iid >= 0 &&
12428 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12429 es->oe->uuid != appctx->ctx.errors.iid)
12430 goto next;
12431
12432 if (appctx->ctx.errors.ptr < 0) {
12433 /* just print headers now */
12434
12435 char pn[INET6_ADDRSTRLEN];
12436 struct tm tm;
12437 int port;
12438
12439 get_localtime(es->when.tv_sec, &tm);
12440 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12441 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12442 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12443
12444 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12445 case AF_INET:
12446 case AF_INET6:
12447 port = get_host_port(&es->src);
12448 break;
12449 default:
12450 port = 0;
12451 }
12452
Willy Tarreau35069f82016-11-25 09:16:37 +010012453 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012454 case 0:
12455 chunk_appendf(&trash,
12456 " frontend %s (#%d): invalid request\n"
12457 " backend %s (#%d)",
12458 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12459 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12460 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12461 break;
12462 case 1:
12463 chunk_appendf(&trash,
12464 " backend %s (#%d): invalid response\n"
12465 " frontend %s (#%d)",
12466 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12467 es->oe->id, es->oe->uuid);
12468 break;
12469 }
12470
12471 chunk_appendf(&trash,
12472 ", server %s (#%d), event #%u\n"
12473 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012474 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012475 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12476 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12477 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12478 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12479 es->ev_id,
12480 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012481 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012482 es->m_clen, es->m_blen,
12483 es->b_flags, es->b_out, es->b_tot,
12484 es->len, es->b_wrap, es->pos);
12485
Willy Tarreau06d80a92017-10-19 14:32:15 +020012486 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012487 /* Socket buffer full. Let's try again later from the same point */
12488 si_applet_cant_put(si);
12489 return 0;
12490 }
12491 appctx->ctx.errors.ptr = 0;
12492 appctx->ctx.errors.sid = es->sid;
12493 }
12494
12495 if (appctx->ctx.errors.sid != es->sid) {
12496 /* the snapshot changed while we were dumping it */
12497 chunk_appendf(&trash,
12498 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012499 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012500 si_applet_cant_put(si);
12501 return 0;
12502 }
12503 goto next;
12504 }
12505
12506 /* OK, ptr >= 0, so we have to dump the current line */
12507 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12508 int newptr;
12509 int newline;
12510
12511 newline = appctx->ctx.errors.bol;
12512 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12513 if (newptr == appctx->ctx.errors.ptr)
12514 return 0;
12515
Willy Tarreau06d80a92017-10-19 14:32:15 +020012516 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012517 /* Socket buffer full. Let's try again later from the same point */
12518 si_applet_cant_put(si);
12519 return 0;
12520 }
12521 appctx->ctx.errors.ptr = newptr;
12522 appctx->ctx.errors.bol = newline;
12523 };
12524 next:
12525 appctx->ctx.errors.bol = 0;
12526 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012527 appctx->ctx.errors.flag ^= 1;
12528 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012529 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012530 }
12531
12532 /* dump complete */
12533 return 1;
12534}
12535
12536/* register cli keywords */
12537static struct cli_kw_list cli_kws = {{ },{
12538 { { "show", "errors", NULL },
12539 "show errors : report last request and response errors for each proxy",
12540 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12541 },
12542 {{},}
12543}};
12544
Willy Tarreau4a568972010-05-12 08:08:50 +020012545/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012546/* All supported ACL keywords must be declared here. */
12547/************************************************************************/
12548
12549/* Note: must not be declared <const> as its list will be overwritten.
12550 * Please take care of keeping this list alphabetically sorted.
12551 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012552static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012553 { "base", "base", PAT_MATCH_STR },
12554 { "base_beg", "base", PAT_MATCH_BEG },
12555 { "base_dir", "base", PAT_MATCH_DIR },
12556 { "base_dom", "base", PAT_MATCH_DOM },
12557 { "base_end", "base", PAT_MATCH_END },
12558 { "base_len", "base", PAT_MATCH_LEN },
12559 { "base_reg", "base", PAT_MATCH_REG },
12560 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012561
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012562 { "cook", "req.cook", PAT_MATCH_STR },
12563 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12564 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12565 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12566 { "cook_end", "req.cook", PAT_MATCH_END },
12567 { "cook_len", "req.cook", PAT_MATCH_LEN },
12568 { "cook_reg", "req.cook", PAT_MATCH_REG },
12569 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012570
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012571 { "hdr", "req.hdr", PAT_MATCH_STR },
12572 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12573 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12574 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12575 { "hdr_end", "req.hdr", PAT_MATCH_END },
12576 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12577 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12578 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012579
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012580 /* these two declarations uses strings with list storage (in place
12581 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12582 * and delete functions are relative to the list management. The parse
12583 * and match method are related to the corresponding fetch methods. This
12584 * is very particular ACL declaration mode.
12585 */
12586 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12587 { "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 +020012588
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012589 { "path", "path", PAT_MATCH_STR },
12590 { "path_beg", "path", PAT_MATCH_BEG },
12591 { "path_dir", "path", PAT_MATCH_DIR },
12592 { "path_dom", "path", PAT_MATCH_DOM },
12593 { "path_end", "path", PAT_MATCH_END },
12594 { "path_len", "path", PAT_MATCH_LEN },
12595 { "path_reg", "path", PAT_MATCH_REG },
12596 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012597
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012598 { "req_ver", "req.ver", PAT_MATCH_STR },
12599 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012600
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012601 { "scook", "res.cook", PAT_MATCH_STR },
12602 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12603 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12604 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12605 { "scook_end", "res.cook", PAT_MATCH_END },
12606 { "scook_len", "res.cook", PAT_MATCH_LEN },
12607 { "scook_reg", "res.cook", PAT_MATCH_REG },
12608 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012609
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012610 { "shdr", "res.hdr", PAT_MATCH_STR },
12611 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12612 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12613 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12614 { "shdr_end", "res.hdr", PAT_MATCH_END },
12615 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12616 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12617 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012618
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012619 { "url", "url", PAT_MATCH_STR },
12620 { "url_beg", "url", PAT_MATCH_BEG },
12621 { "url_dir", "url", PAT_MATCH_DIR },
12622 { "url_dom", "url", PAT_MATCH_DOM },
12623 { "url_end", "url", PAT_MATCH_END },
12624 { "url_len", "url", PAT_MATCH_LEN },
12625 { "url_reg", "url", PAT_MATCH_REG },
12626 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012627
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012628 { "urlp", "urlp", PAT_MATCH_STR },
12629 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12630 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12631 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12632 { "urlp_end", "urlp", PAT_MATCH_END },
12633 { "urlp_len", "urlp", PAT_MATCH_LEN },
12634 { "urlp_reg", "urlp", PAT_MATCH_REG },
12635 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012636
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012637 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012638}};
12639
12640/************************************************************************/
12641/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012642/************************************************************************/
12643/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012644static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012645 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012646 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012647 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12648
Willy Tarreau87b09662015-04-03 00:22:06 +020012649 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012650 { "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 +020012651
12652 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012653 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12654 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12655 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012656
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012657 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12658 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012659
Willy Tarreau409bcde2013-01-08 00:31:00 +010012660 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12661 * are only here to match the ACL's name, are request-only and are used
12662 * for ACL compatibility only.
12663 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012664 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12665 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012666 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12667 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012668
12669 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12670 * only here to match the ACL's name, are request-only and are used for
12671 * ACL compatibility only.
12672 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012673 { "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 +020012674 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012675 { "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 +020012676 { "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 +010012677
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012678 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012679 { "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 +010012680 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012681 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012682 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012683 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012684
12685 /* HTTP protocol on the request path */
12686 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012687 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012688
12689 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012690 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12691 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012692
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012693 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012694 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12695 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012696 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012697
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012698 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012699 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12700
Willy Tarreau18ed2562013-01-14 15:56:36 +010012701 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012702 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12703 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012704
Willy Tarreau18ed2562013-01-14 15:56:36 +010012705 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012706 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012707 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12708 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012709
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012710 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012711 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012712 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012713 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012714 { "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 +010012715 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012716 { "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 +010012717
12718 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012719 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012720 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12721 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012722
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012723 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012724 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012725 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012726 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012727 { "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 +010012728 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012729 { "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 +010012730
Willy Tarreau409bcde2013-01-08 00:31:00 +010012731 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012732 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012733 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12734 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012735 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012736
12737 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012738 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012739 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012740 { "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 +020012741 { "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 +010012742
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012743 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012744 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012745 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012746 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012747 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012748 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012749 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012750 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12751 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012752 { "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 +010012753 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012754}};
12755
Willy Tarreau8797c062007-05-07 00:55:35 +020012756
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012757/************************************************************************/
12758/* All supported converter keywords must be declared here. */
12759/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012760/* Note: must not be declared <const> as its list will be overwritten */
12761static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012762 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012763 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012764 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12765 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012766 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012767 { NULL, NULL, 0, 0, 0 },
12768}};
12769
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012770
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012771/************************************************************************/
12772/* All supported http-request action keywords must be declared here. */
12773/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012774struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012775 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012776 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012777 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012778 { "set-method", parse_set_req_line },
12779 { "set-path", parse_set_req_line },
12780 { "set-query", parse_set_req_line },
12781 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012782 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012783 }
12784};
12785
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012786struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012787 .kw = {
12788 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012789 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012790 { NULL, NULL }
12791 }
12792};
12793
Willy Tarreau8797c062007-05-07 00:55:35 +020012794__attribute__((constructor))
12795static void __http_protocol_init(void)
12796{
12797 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012798 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012799 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012800 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012801 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012802 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012803}
12804
12805
Willy Tarreau58f10d72006-12-04 02:26:12 +010012806/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012807 * Local variables:
12808 * c-indent-level: 8
12809 * c-basic-offset: 8
12810 * End:
12811 */