blob: 1330ac8648d96941d37943edaf9345d09f19550e [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 Brun0fed0b02017-11-29 16:15:07 +01002757
2758 /* If data was modified, we need to touch to re-schedule sync */
2759 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002760 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002761
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002762 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002763 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002764 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002765 }
2766 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002767 break;
2768
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002769 /* other flags exists, but normaly, they never be matched. */
2770 default:
2771 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002772 }
2773 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002774
2775 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002776 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002777}
2778
Willy Tarreau71241ab2012-12-27 11:30:54 +01002779
Willy Tarreau51d861a2015-05-22 17:30:48 +02002780/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2781 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2782 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2783 * is returned, the process can continue the evaluation of next rule list. If
2784 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2785 * is returned, it means the operation could not be processed and a server error
2786 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2787 * deny rule. If *YIELD is returned, the caller must call again the function
2788 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002789 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002790static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002791http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002792{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002793 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002794 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002795 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002796 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002797 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002798 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002799
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002800 /* If "the current_rule_list" match the executed rule list, we are in
2801 * resume condition. If a resume is needed it is always in the action
2802 * and never in the ACL or converters. In this case, we initialise the
2803 * current rule, and go to the action execution point.
2804 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002805 if (s->current_rule) {
2806 rule = s->current_rule;
2807 s->current_rule = NULL;
2808 if (s->current_rule_list == rules)
2809 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002810 }
2811 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002812
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002813 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002814
2815 /* check optional condition */
2816 if (rule->cond) {
2817 int ret;
2818
Willy Tarreau192252e2015-04-04 01:47:55 +02002819 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002820 ret = acl_pass(ret);
2821
2822 if (rule->cond->pol == ACL_COND_UNLESS)
2823 ret = !ret;
2824
2825 if (!ret) /* condition not matched */
2826 continue;
2827 }
2828
Willy Tarreauacc98002015-09-27 23:34:39 +02002829 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002830resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002831 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002832 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002833 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002834
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002835 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002836 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002837 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002838
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002839 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002840 s->task->nice = rule->arg.nice;
2841 break;
2842
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002843 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002844 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002845 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002846 break;
2847
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002848 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002849#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002850 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002851 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002852#endif
2853 break;
2854
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002855 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002856 s->logs.level = rule->arg.loglevel;
2857 break;
2858
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002859 case ACT_HTTP_REPLACE_HDR:
2860 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002861 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2862 rule->arg.hdr_add.name_len,
2863 &rule->arg.hdr_add.fmt,
2864 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002865 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002866 break;
2867
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002868 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002869 ctx.idx = 0;
2870 /* remove all occurrences of the header */
2871 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2872 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2873 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2874 }
Willy Tarreau85603282015-01-21 20:39:27 +01002875 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002876
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002877 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002878 case ACT_HTTP_ADD_HDR: {
2879 struct chunk *replace;
2880
2881 replace = alloc_trash_chunk();
2882 if (!replace)
2883 return HTTP_RULE_RES_BADREQ;
2884
2885 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2886 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2887 replace->len = rule->arg.hdr_add.name_len;
2888 replace->str[replace->len++] = ':';
2889 replace->str[replace->len++] = ' ';
2890 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2891 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002892
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002893 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002894 /* remove all occurrences of the header */
2895 ctx.idx = 0;
2896 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2897 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2898 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2899 }
2900 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002901 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2902
2903 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002904 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002905 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002907 case ACT_HTTP_DEL_ACL:
2908 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002909 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002910 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002911
2912 /* collect reference */
2913 ref = pat_ref_lookup(rule->arg.map.ref);
2914 if (!ref)
2915 continue;
2916
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002917 /* allocate key */
2918 key = alloc_trash_chunk();
2919 if (!key)
2920 return HTTP_RULE_RES_BADREQ;
2921
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002922 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002923 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2924 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002925
2926 /* perform update */
2927 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002928 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002929 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002930 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002931
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002932 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002933 break;
2934 }
2935
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002936 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002937 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002938 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002939
2940 /* collect reference */
2941 ref = pat_ref_lookup(rule->arg.map.ref);
2942 if (!ref)
2943 continue;
2944
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002945 /* allocate key */
2946 key = alloc_trash_chunk();
2947 if (!key)
2948 return HTTP_RULE_RES_BADREQ;
2949
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002950 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002951 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2952 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002953
2954 /* perform update */
2955 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002956 if (pat_ref_find_elt(ref, key->str) == NULL)
2957 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002958
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002959 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002960 break;
2961 }
2962
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002963 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002964 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002965 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002966
2967 /* collect reference */
2968 ref = pat_ref_lookup(rule->arg.map.ref);
2969 if (!ref)
2970 continue;
2971
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002972 /* allocate key */
2973 key = alloc_trash_chunk();
2974 if (!key)
2975 return HTTP_RULE_RES_BADREQ;
2976
2977 /* allocate value */
2978 value = alloc_trash_chunk();
2979 if (!value) {
2980 free_trash_chunk(key);
2981 return HTTP_RULE_RES_BADREQ;
2982 }
2983
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002984 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002985 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2986 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002987
2988 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002989 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2990 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002991
2992 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002993 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002994 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002995 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002996 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002997 else
2998 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002999 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003000 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003001 free_trash_chunk(key);
3002 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003003 break;
3004 }
William Lallemand73025dd2014-04-24 14:38:37 +02003005
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003006 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003007 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3008 return HTTP_RULE_RES_BADREQ;
3009 return HTTP_RULE_RES_DONE;
3010
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003011 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3012 /* Note: only the first valid tracking parameter of each
3013 * applies.
3014 */
3015
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003016 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003017 struct stktable *t;
3018 struct stksess *ts;
3019 struct stktable_key *key;
3020 void *ptr;
3021
3022 t = rule->arg.trk_ctr.table.t;
3023 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3024
3025 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003026 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003027
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003028 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003029
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003030 /* let's count a new HTTP request as it's the first time we do it */
3031 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3032 if (ptr)
3033 stktable_data_cast(ptr, http_req_cnt)++;
3034
3035 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3036 if (ptr)
3037 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3038 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3039
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003040 /* When the client triggers a 4xx from the server, it's most often due
3041 * to a missing object or permission. These events should be tracked
3042 * because if they happen often, it may indicate a brute force or a
3043 * vulnerability scan. Normally this is done when receiving the response
3044 * but here we're tracking after this ought to have been done so we have
3045 * to do it on purpose.
3046 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003047 if ((unsigned)(txn->status - 400) < 100) {
3048 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3049 if (ptr)
3050 stktable_data_cast(ptr, http_err_cnt)++;
3051
3052 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3053 if (ptr)
3054 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3055 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3056 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003057
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003058 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003059
Emeric Brun0fed0b02017-11-29 16:15:07 +01003060 /* If data was modified, we need to touch to re-schedule sync */
3061 stktable_touch_local(t, ts, 0);
3062
Emeric Brun819fc6f2017-06-13 19:37:32 +02003063 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3064 if (sess->fe != s->be)
3065 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3066
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003067 }
3068 }
3069 break;
3070
Thierry FOURNIER42148732015-09-02 17:17:33 +02003071 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003072 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3073 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003074
Willy Tarreauacc98002015-09-27 23:34:39 +02003075 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003076 case ACT_RET_ERR:
3077 case ACT_RET_CONT:
3078 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003079 case ACT_RET_STOP:
3080 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003081 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003082 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003083 return HTTP_RULE_RES_YIELD;
3084 }
William Lallemand73025dd2014-04-24 14:38:37 +02003085 break;
3086
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003087 /* other flags exists, but normaly, they never be matched. */
3088 default:
3089 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003090 }
3091 }
3092
3093 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003094 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003095}
3096
3097
Willy Tarreau71241ab2012-12-27 11:30:54 +01003098/* Perform an HTTP redirect based on the information in <rule>. The function
3099 * returns non-zero on success, or zero in case of a, irrecoverable error such
3100 * as too large a request to build a valid response.
3101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003102static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003103{
Willy Tarreaub329a312015-05-22 16:27:37 +02003104 struct http_msg *req = &txn->req;
3105 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003106 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003107 struct chunk *chunk;
3108 int ret = 0;
3109
3110 chunk = alloc_trash_chunk();
3111 if (!chunk)
3112 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003113
3114 /* build redirect message */
3115 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003116 case 308:
3117 msg_fmt = HTTP_308;
3118 break;
3119 case 307:
3120 msg_fmt = HTTP_307;
3121 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003122 case 303:
3123 msg_fmt = HTTP_303;
3124 break;
3125 case 301:
3126 msg_fmt = HTTP_301;
3127 break;
3128 case 302:
3129 default:
3130 msg_fmt = HTTP_302;
3131 break;
3132 }
3133
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003134 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3135 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003136
3137 switch(rule->type) {
3138 case REDIRECT_TYPE_SCHEME: {
3139 const char *path;
3140 const char *host;
3141 struct hdr_ctx ctx;
3142 int pathlen;
3143 int hostlen;
3144
3145 host = "";
3146 hostlen = 0;
3147 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003148 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003149 host = ctx.line + ctx.val;
3150 hostlen = ctx.vlen;
3151 }
3152
3153 path = http_get_path(txn);
3154 /* build message using path */
3155 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003156 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003157 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3158 int qs = 0;
3159 while (qs < pathlen) {
3160 if (path[qs] == '?') {
3161 pathlen = qs;
3162 break;
3163 }
3164 qs++;
3165 }
3166 }
3167 } else {
3168 path = "/";
3169 pathlen = 1;
3170 }
3171
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003172 if (rule->rdr_str) { /* this is an old "redirect" rule */
3173 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003174 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3175 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003176
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003177 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003178 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3179 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003180 }
3181 else {
3182 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003183 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003184
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003185 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003186 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3187 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003188 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003189 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003190 memcpy(chunk->str + chunk->len, "://", 3);
3191 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003192
3193 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003194 memcpy(chunk->str + chunk->len, host, hostlen);
3195 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003196
3197 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003198 memcpy(chunk->str + chunk->len, path, pathlen);
3199 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003200
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003201 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003202 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003203 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003204 if (chunk->len > chunk->size - 5)
3205 goto leave;
3206 chunk->str[chunk->len] = '/';
3207 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003208 }
3209
3210 break;
3211 }
3212 case REDIRECT_TYPE_PREFIX: {
3213 const char *path;
3214 int pathlen;
3215
3216 path = http_get_path(txn);
3217 /* build message using path */
3218 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003219 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003220 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3221 int qs = 0;
3222 while (qs < pathlen) {
3223 if (path[qs] == '?') {
3224 pathlen = qs;
3225 break;
3226 }
3227 qs++;
3228 }
3229 }
3230 } else {
3231 path = "/";
3232 pathlen = 1;
3233 }
3234
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003235 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003236 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3237 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003238
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003239 /* add prefix. Note that if prefix == "/", we don't want to
3240 * add anything, otherwise it makes it hard for the user to
3241 * configure a self-redirection.
3242 */
3243 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003244 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3245 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003246 }
3247 }
3248 else {
3249 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003250 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003251
3252 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003253 if (chunk->len + pathlen > chunk->size - 4)
3254 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003255 }
3256
3257 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003258 memcpy(chunk->str + chunk->len, path, pathlen);
3259 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003260
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003261 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003262 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003263 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003264 if (chunk->len > chunk->size - 5)
3265 goto leave;
3266 chunk->str[chunk->len] = '/';
3267 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003268 }
3269
3270 break;
3271 }
3272 case REDIRECT_TYPE_LOCATION:
3273 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003274 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003275 if (chunk->len + rule->rdr_len > chunk->size - 4)
3276 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003277
3278 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003279 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3280 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003281 }
3282 else {
3283 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003284 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003285
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003286 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003287 if (chunk->len > chunk->size - 4)
3288 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003289 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003290 break;
3291 }
3292
3293 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003294 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3295 chunk->len += 14;
3296 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3297 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003298 }
3299
Willy Tarreau19b14122017-02-28 09:48:11 +01003300 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003301 txn->status = rule->code;
3302 /* let's log the request time */
3303 s->logs.tv_request = now;
3304
Christopher Fauletbe821b92017-03-30 11:21:53 +02003305 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003306 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3307 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3308 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003309 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003310 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003311 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3312 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003313 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003314 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3315 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003316 }
3317 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003318 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3319 chunk->len += 4;
3320 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003321 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003322 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003323 bi_fast_delete(req->chn->buf, req->sov);
3324 req->next -= req->sov;
3325 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003326 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003327 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003328 req->msg_state = HTTP_MSG_CLOSED;
3329 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003330 /* Trim any possible response */
3331 res->chn->buf->i = 0;
3332 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003333 /* let the server side turn to SI_ST_CLO */
3334 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003335 } else {
3336 /* keep-alive not possible */
3337 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003338 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3339 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003340 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003341 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3342 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003343 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003344 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003345 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003346 }
3347
Willy Tarreaue7dff022015-04-03 01:14:29 +02003348 if (!(s->flags & SF_ERR_MASK))
3349 s->flags |= SF_ERR_LOCAL;
3350 if (!(s->flags & SF_FINST_MASK))
3351 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003352
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003353 ret = 1;
3354 leave:
3355 free_trash_chunk(chunk);
3356 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003357}
3358
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003359/* This stream analyser runs all HTTP request processing which is common to
3360 * frontends and backends, which means blocking ACLs, filters, connection-close,
3361 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003362 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003363 * either needs more data or wants to immediately abort the request (eg: deny,
3364 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003365 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003366int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003367{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003368 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003369 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003370 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003371 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003372 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003373 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003374 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003375 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003376
Willy Tarreau655dce92009-11-08 13:10:58 +01003377 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003378 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003379 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003380 }
3381
Willy Tarreau87b09662015-04-03 00:22:06 +02003382 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 +02003383 now_ms, __FUNCTION__,
3384 s,
3385 req,
3386 req->rex, req->wex,
3387 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003388 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003389 req->analysers);
3390
Willy Tarreau65410832014-04-28 21:25:43 +02003391 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003392 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003393
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003394 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003395 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003396 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003397
Willy Tarreau0b748332014-04-29 00:13:29 +02003398 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003399 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3400 goto return_prx_yield;
3401
Willy Tarreau0b748332014-04-29 00:13:29 +02003402 case HTTP_RULE_RES_CONT:
3403 case HTTP_RULE_RES_STOP: /* nothing to do */
3404 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003405
Willy Tarreau0b748332014-04-29 00:13:29 +02003406 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3407 if (txn->flags & TX_CLTARPIT)
3408 goto tarpit;
3409 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003410
Willy Tarreau0b748332014-04-29 00:13:29 +02003411 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3412 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003413
Willy Tarreau0b748332014-04-29 00:13:29 +02003414 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003415 goto done;
3416
Willy Tarreau0b748332014-04-29 00:13:29 +02003417 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3418 goto return_bad_req;
3419 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003420 }
3421
Olivier Houchardc2aae742017-09-22 18:26:28 +02003422 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3423 struct hdr_ctx ctx;
3424
3425 ctx.idx = 0;
3426 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3427 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3428 if (unlikely(http_header_add_tail2(&txn->req,
3429 &txn->hdr_idx, "Early-Data: 1",
3430 strlen("Early-Data: 1"))) < 0) {
3431 goto return_bad_req;
3432 }
3433 }
3434
3435 }
3436
Willy Tarreau52542592014-04-28 18:33:26 +02003437 /* OK at this stage, we know that the request was accepted according to
3438 * the http-request rules, we can check for the stats. Note that the
3439 * URI is detected *before* the req* rules in order not to be affected
3440 * by a possible reqrep, while they are processed *after* so that a
3441 * reqdeny can still block them. This clearly needs to change in 1.6!
3442 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003443 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003444 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003445 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003446 txn->status = 500;
3447 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003448 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003449
Willy Tarreaue7dff022015-04-03 01:14:29 +02003450 if (!(s->flags & SF_ERR_MASK))
3451 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003452 goto return_prx_cond;
3453 }
3454
3455 /* parse the whole stats request and extract the relevant information */
3456 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003457 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003458 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003459
Willy Tarreau0b748332014-04-29 00:13:29 +02003460 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3461 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003462
Willy Tarreau0b748332014-04-29 00:13:29 +02003463 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3464 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003465 }
3466
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003467 /* evaluate the req* rules except reqadd */
3468 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003469 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003470 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003471
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003472 if (txn->flags & TX_CLDENY)
3473 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003474
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003475 if (txn->flags & TX_CLTARPIT) {
3476 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003477 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003478 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003479 }
Willy Tarreau06619262006-12-17 08:37:22 +01003480
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003481 /* add request headers from the rule sets in the same order */
3482 list_for_each_entry(wl, &px->req_add, list) {
3483 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003484 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003485 ret = acl_pass(ret);
3486 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3487 ret = !ret;
3488 if (!ret)
3489 continue;
3490 }
3491
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003492 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003493 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003494 }
3495
Willy Tarreau52542592014-04-28 18:33:26 +02003496
3497 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003498 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3499 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003500 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003501 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003502 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003503
Willy Tarreaue7dff022015-04-03 01:14:29 +02003504 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3505 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3506 if (!(s->flags & SF_FINST_MASK))
3507 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003508
Willy Tarreau70730dd2014-04-24 18:06:27 +02003509 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003510 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3511 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003512 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003513 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003514 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003515
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003516 /* check whether we have some ACLs set to redirect this request */
3517 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003518 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003519 int ret;
3520
Willy Tarreau192252e2015-04-04 01:47:55 +02003521 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003522 ret = acl_pass(ret);
3523 if (rule->cond->pol == ACL_COND_UNLESS)
3524 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003525 if (!ret)
3526 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003527 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003528 if (!http_apply_redirect_rule(rule, s, txn))
3529 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003530 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003531 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003532
Willy Tarreau2be39392010-01-03 17:24:51 +01003533 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3534 * If this happens, then the data will not come immediately, so we must
3535 * send all what we have without waiting. Note that due to the small gain
3536 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003537 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003538 * itself once used.
3539 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003540 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003541
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003542 done: /* done with this analyser, continue with next ones that the calling
3543 * points will have set, if any.
3544 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003545 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003546 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3547 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003548 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003549
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003550 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003551 /* Allow cookie logging
3552 */
3553 if (s->be->cookie_name || sess->fe->capture_name)
3554 manage_client_side_cookies(s, req);
3555
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003556 /* When a connection is tarpitted, we use the tarpit timeout,
3557 * which may be the same as the connect timeout if unspecified.
3558 * If unset, then set it to zero because we really want it to
3559 * eventually expire. We build the tarpit as an analyser.
3560 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003561 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003562
3563 /* wipe the request out so that we can drop the connection early
3564 * if the client closes first.
3565 */
3566 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003567
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003568 txn->status = http_err_codes[deny_status];
3569
Christopher Faulet0184ea72017-01-05 14:06:34 +01003570 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003571 req->analysers |= AN_REQ_HTTP_TARPIT;
3572 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3573 if (!req->analyse_exp)
3574 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003575 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003576 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003577 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003578 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003579 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003580 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003581 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003582
3583 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003584
3585 /* Allow cookie logging
3586 */
3587 if (s->be->cookie_name || sess->fe->capture_name)
3588 manage_client_side_cookies(s, req);
3589
Willy Tarreau0b748332014-04-29 00:13:29 +02003590 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003591 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003592 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003593 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003594 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003595 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003596 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003597 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003598 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003599 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003600 goto return_prx_cond;
3601
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003602 return_bad_req:
3603 /* We centralize bad requests processing here */
3604 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3605 /* we detected a parsing error. We want to archive this request
3606 * in the dedicated proxy area for later troubleshooting.
3607 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003608 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003609 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003610
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003611 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003612 txn->req.msg_state = HTTP_MSG_ERROR;
3613 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003614 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003615
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003616 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003617 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003618 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003619
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003620 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003621 if (!(s->flags & SF_ERR_MASK))
3622 s->flags |= SF_ERR_PRXCOND;
3623 if (!(s->flags & SF_FINST_MASK))
3624 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003625
Christopher Faulet0184ea72017-01-05 14:06:34 +01003626 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003627 req->analyse_exp = TICK_ETERNITY;
3628 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003629
3630 return_prx_yield:
3631 channel_dont_connect(req);
3632 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003633}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003634
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003635/* This function performs all the processing enabled for the current request.
3636 * It returns 1 if the processing can continue on next analysers, or zero if it
3637 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003638 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003640int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003641{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003642 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003643 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003644 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003645 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003646
Willy Tarreau655dce92009-11-08 13:10:58 +01003647 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003648 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003649 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003650 return 0;
3651 }
3652
Willy Tarreau87b09662015-04-03 00:22:06 +02003653 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 +02003654 now_ms, __FUNCTION__,
3655 s,
3656 req,
3657 req->rex, req->wex,
3658 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003659 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003660 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003661
Willy Tarreau59234e92008-11-30 23:51:27 +01003662 /*
3663 * Right now, we know that we have processed the entire headers
3664 * and that unwanted requests have been filtered out. We can do
3665 * whatever we want with the remaining request. Also, now we
3666 * may have separate values for ->fe, ->be.
3667 */
Willy Tarreau06619262006-12-17 08:37:22 +01003668
Willy Tarreau59234e92008-11-30 23:51:27 +01003669 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003670 * If HTTP PROXY is set we simply get remote server address parsing
3671 * incoming request. Note that this requires that a connection is
3672 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003673 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003674 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003675 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003676 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003677
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003678 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003679 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003680 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003681 txn->req.msg_state = HTTP_MSG_ERROR;
3682 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003683 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003684 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003685
Willy Tarreaue7dff022015-04-03 01:14:29 +02003686 if (!(s->flags & SF_ERR_MASK))
3687 s->flags |= SF_ERR_RESOURCE;
3688 if (!(s->flags & SF_FINST_MASK))
3689 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003690
3691 return 0;
3692 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003693
3694 path = http_get_path(txn);
3695 url2sa(req->buf->p + msg->sl.rq.u,
3696 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003697 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003698 /* if the path was found, we have to remove everything between
3699 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3700 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3701 * u_l characters by a single "/".
3702 */
3703 if (path) {
3704 char *cur_ptr = req->buf->p;
3705 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3706 int delta;
3707
3708 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3709 http_msg_move_end(&txn->req, delta);
3710 cur_end += delta;
3711 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3712 goto return_bad_req;
3713 }
3714 else {
3715 char *cur_ptr = req->buf->p;
3716 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3717 int delta;
3718
3719 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3720 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3721 http_msg_move_end(&txn->req, delta);
3722 cur_end += delta;
3723 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3724 goto return_bad_req;
3725 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003726 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003727
Willy Tarreau59234e92008-11-30 23:51:27 +01003728 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003729 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003730 * Note that doing so might move headers in the request, but
3731 * the fields will stay coherent and the URI will not move.
3732 * This should only be performed in the backend.
3733 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003734 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003735 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003736
William Lallemanda73203e2012-03-12 12:48:57 +01003737 /* add unique-id if "header-unique-id" is specified */
3738
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003739 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003740 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003741 goto return_bad_req;
3742 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003743 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003744 }
William Lallemanda73203e2012-03-12 12:48:57 +01003745
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003746 if (sess->fe->header_unique_id && s->unique_id) {
3747 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003748 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003749 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003750 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003751 goto return_bad_req;
3752 }
3753
Cyril Bontéb21570a2009-11-29 20:04:48 +01003754 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003755 * 9: add X-Forwarded-For if either the frontend or the backend
3756 * asks for it.
3757 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003758 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003759 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003760 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3761 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3762 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003763 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003764 /* The header is set to be added only if none is present
3765 * and we found it, so don't do anything.
3766 */
3767 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003768 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 /* Add an X-Forwarded-For header unless the source IP is
3770 * in the 'except' network range.
3771 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003772 if ((!sess->fe->except_mask.s_addr ||
3773 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3774 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003775 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003776 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003777 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003778 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003779 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003780 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003781
3782 /* Note: we rely on the backend to get the header name to be used for
3783 * x-forwarded-for, because the header is really meant for the backends.
3784 * However, if the backend did not specify any option, we have to rely
3785 * on the frontend's header name.
3786 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003787 if (s->be->fwdfor_hdr_len) {
3788 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003789 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003790 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003791 len = sess->fe->fwdfor_hdr_len;
3792 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003793 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003794 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 +01003795
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003796 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003797 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003798 }
3799 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003800 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003801 /* FIXME: for the sake of completeness, we should also support
3802 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003803 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003804 int len;
3805 char pn[INET6_ADDRSTRLEN];
3806 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003807 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003808 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003809
Willy Tarreau59234e92008-11-30 23:51:27 +01003810 /* Note: we rely on the backend to get the header name to be used for
3811 * x-forwarded-for, because the header is really meant for the backends.
3812 * However, if the backend did not specify any option, we have to rely
3813 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003814 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003815 if (s->be->fwdfor_hdr_len) {
3816 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003817 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003818 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003819 len = sess->fe->fwdfor_hdr_len;
3820 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003821 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003822 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003823
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003824 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003825 goto return_bad_req;
3826 }
3827 }
3828
3829 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003830 * 10: add X-Original-To if either the frontend or the backend
3831 * asks for it.
3832 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003833 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003834
3835 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003836 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003837 /* Add an X-Original-To header unless the destination IP is
3838 * in the 'except' network range.
3839 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003840 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003841
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003842 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003843 ((!sess->fe->except_mask_to.s_addr ||
3844 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3845 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003846 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003847 (((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 +02003848 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003849 int len;
3850 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003851 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003852
3853 /* Note: we rely on the backend to get the header name to be used for
3854 * x-original-to, because the header is really meant for the backends.
3855 * However, if the backend did not specify any option, we have to rely
3856 * on the frontend's header name.
3857 */
3858 if (s->be->orgto_hdr_len) {
3859 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003860 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003861 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003862 len = sess->fe->orgto_hdr_len;
3863 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003864 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003865 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 +02003866
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003867 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003868 goto return_bad_req;
3869 }
3870 }
3871 }
3872
Willy Tarreau50fc7772012-11-11 22:19:57 +01003873 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3874 * If an "Upgrade" token is found, the header is left untouched in order not to have
3875 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3876 * "Upgrade" is present in the Connection header.
3877 */
3878 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3879 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
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 Tarreaubbf0b372010-01-18 16:54:40 +01003882 unsigned int want_flags = 0;
3883
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003884 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003885 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
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_CLO_SET;
3890 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003891 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003892 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003893 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003894 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003895 want_flags |= TX_CON_KAL_SET;
3896 }
3897
3898 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003899 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003900 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003901
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003902
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903 /* If we have no server assigned yet and we're balancing on url_param
3904 * with a POST request, we may be interested in checking the body for
3905 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003906 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003907 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003908 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003909 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003910 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003911 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003912 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003913
Christopher Fauletbe821b92017-03-30 11:21:53 +02003914 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3915 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003916#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003917 /* We expect some data from the client. Unless we know for sure
3918 * we already have a full request, we have to re-enable quick-ack
3919 * in case we previously disabled it, otherwise we might cause
3920 * the client to delay further data.
3921 */
3922 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3923 cli_conn && conn_ctrl_ready(cli_conn) &&
3924 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3925 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003926 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003927#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003928
Willy Tarreau59234e92008-11-30 23:51:27 +01003929 /*************************************************************
3930 * OK, that's finished for the headers. We have done what we *
3931 * could. Let's switch to the DATA state. *
3932 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003933 req->analyse_exp = TICK_ETERNITY;
3934 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003935
Willy Tarreau59234e92008-11-30 23:51:27 +01003936 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003937 /* OK let's go on with the BODY now */
3938 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003939
Willy Tarreau59234e92008-11-30 23:51:27 +01003940 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003941 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003942 /* we detected a parsing error. We want to archive this request
3943 * in the dedicated proxy area for later troubleshooting.
3944 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003945 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003946 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003947
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003948 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003949 txn->req.msg_state = HTTP_MSG_ERROR;
3950 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003951 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003952 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003953
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003954 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003955 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003956 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003957
Willy Tarreaue7dff022015-04-03 01:14:29 +02003958 if (!(s->flags & SF_ERR_MASK))
3959 s->flags |= SF_ERR_PRXCOND;
3960 if (!(s->flags & SF_FINST_MASK))
3961 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003962 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003963}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003964
Willy Tarreau60b85b02008-11-30 23:28:40 +01003965/* This function is an analyser which processes the HTTP tarpit. It always
3966 * returns zero, at the beginning because it prevents any other processing
3967 * from occurring, and at the end because it terminates the request.
3968 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003969int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003970{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003971 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003972
3973 /* This connection is being tarpitted. The CLIENT side has
3974 * already set the connect expiration date to the right
3975 * timeout. We just have to check that the client is still
3976 * there and that the timeout has not expired.
3977 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003978 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003979 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003980 !tick_is_expired(req->analyse_exp, now_ms))
3981 return 0;
3982
3983 /* We will set the queue timer to the time spent, just for
3984 * logging purposes. We fake a 500 server error, so that the
3985 * attacker will not suspect his connection has been tarpitted.
3986 * It will not cause trouble to the logs because we can exclude
3987 * the tarpitted connections by filtering on the 'PT' status flags.
3988 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003989 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3990
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003991 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003992 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003993
Christopher Faulet0184ea72017-01-05 14:06:34 +01003994 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003995 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003996
Willy Tarreaue7dff022015-04-03 01:14:29 +02003997 if (!(s->flags & SF_ERR_MASK))
3998 s->flags |= SF_ERR_PRXCOND;
3999 if (!(s->flags & SF_FINST_MASK))
4000 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004001 return 0;
4002}
4003
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004004/* This function is an analyser which waits for the HTTP request body. It waits
4005 * for either the buffer to be full, or the full advertised contents to have
4006 * reached the buffer. It must only be called after the standard HTTP request
4007 * processing has occurred, because it expects the request to be parsed and will
4008 * look for the Expect header. It may send a 100-Continue interim response. It
4009 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4010 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4011 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004012 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004013int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004014{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004015 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004016 struct http_txn *txn = s->txn;
4017 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004018
4019 /* We have to parse the HTTP request body to find any required data.
4020 * "balance url_param check_post" should have been the only way to get
4021 * into this. We were brought here after HTTP header analysis, so all
4022 * related structures are ready.
4023 */
4024
Willy Tarreau890988f2014-04-10 11:59:33 +02004025 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4026 /* This is the first call */
4027 if (msg->msg_state < HTTP_MSG_BODY)
4028 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004029
Willy Tarreau890988f2014-04-10 11:59:33 +02004030 if (msg->msg_state < HTTP_MSG_100_SENT) {
4031 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4032 * send an HTTP/1.1 100 Continue intermediate response.
4033 */
4034 if (msg->flags & HTTP_MSGF_VER_11) {
4035 struct hdr_ctx ctx;
4036 ctx.idx = 0;
4037 /* Expect is allowed in 1.1, look for it */
4038 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4039 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004040 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004041 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004042 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004043 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004044 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004045 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004046
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004047 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004048 * req->buf->p still points to the beginning of the message. We
4049 * must save the body in msg->next because it survives buffer
4050 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004051 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004052 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004053
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004054 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004055 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4056 else
4057 msg->msg_state = HTTP_MSG_DATA;
4058 }
4059
Willy Tarreau890988f2014-04-10 11:59:33 +02004060 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4061 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004062 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004063 goto missing_data;
4064
4065 /* OK we have everything we need now */
4066 goto http_end;
4067 }
4068
4069 /* OK here we're parsing a chunked-encoded message */
4070
Willy Tarreau522d6c02009-12-06 18:49:18 +01004071 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004072 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004073 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004074 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004075 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004076 unsigned int chunk;
4077 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004078
Willy Tarreau115acb92009-12-26 13:56:06 +01004079 if (!ret)
4080 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004081 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004082 msg->err_pos = req->buf->i + ret;
4083 if (msg->err_pos < 0)
4084 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004085 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004086 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004087 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004088
4089 msg->chunk_len = chunk;
4090 msg->body_len += chunk;
4091
4092 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004093 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004094 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004095 }
4096
Willy Tarreaud98cf932009-12-27 22:54:55 +01004097 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004098 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4099 * for at least a whole chunk or the whole content length bytes after
4100 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004101 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004102 if (msg->msg_state == HTTP_MSG_TRAILERS)
4103 goto http_end;
4104
Willy Tarreaue115b492015-05-01 23:05:14 +02004105 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004106 goto http_end;
4107
4108 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004109 /* we get here if we need to wait for more data. If the buffer is full,
4110 * we have the maximum we can expect.
4111 */
4112 if (buffer_full(req->buf, global.tune.maxrewrite))
4113 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004114
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004115 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004116 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004117 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004118
Willy Tarreaue7dff022015-04-03 01:14:29 +02004119 if (!(s->flags & SF_ERR_MASK))
4120 s->flags |= SF_ERR_CLITO;
4121 if (!(s->flags & SF_FINST_MASK))
4122 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004123 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004124 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004125
4126 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004127 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004128 /* Not enough data. We'll re-use the http-request
4129 * timeout here. Ideally, we should set the timeout
4130 * relative to the accept() date. We just set the
4131 * request timeout once at the beginning of the
4132 * request.
4133 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004134 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004135 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004136 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004137 return 0;
4138 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004139
4140 http_end:
4141 /* The situation will not evolve, so let's give up on the analysis. */
4142 s->logs.tv_request = now; /* update the request timer to reflect full request */
4143 req->analysers &= ~an_bit;
4144 req->analyse_exp = TICK_ETERNITY;
4145 return 1;
4146
4147 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004148 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004149 txn->req.msg_state = HTTP_MSG_ERROR;
4150 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004151 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004152
Willy Tarreaue7dff022015-04-03 01:14:29 +02004153 if (!(s->flags & SF_ERR_MASK))
4154 s->flags |= SF_ERR_PRXCOND;
4155 if (!(s->flags & SF_FINST_MASK))
4156 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004157
Willy Tarreau522d6c02009-12-06 18:49:18 +01004158 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004159 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004160 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004161 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004162 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004163 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004164}
4165
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004166/* send a server's name with an outgoing request over an established connection.
4167 * Note: this function is designed to be called once the request has been scheduled
4168 * for being forwarded. This is the reason why it rewinds the buffer before
4169 * proceeding.
4170 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004171int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004172
4173 struct hdr_ctx ctx;
4174
Mark Lamourinec2247f02012-01-04 13:02:01 -05004175 char *hdr_name = be->server_id_hdr_name;
4176 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004177 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004178 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004179 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004180
William Lallemandd9e90662012-01-30 17:27:17 +01004181 ctx.idx = 0;
4182
Willy Tarreau211cdec2014-04-17 20:18:08 +02004183 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004184 if (old_o) {
4185 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004186 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004187 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004188 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004189 }
4190
Willy Tarreau9b28e032012-10-12 23:49:43 +02004191 old_i = chn->buf->i;
4192 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 -05004193 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004194 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004195 }
4196
4197 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004198 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004199 memcpy(hdr_val, hdr_name, hdr_name_len);
4200 hdr_val += hdr_name_len;
4201 *hdr_val++ = ':';
4202 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004203 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4204 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004205
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004206 if (old_o) {
4207 /* If this was a forwarded request, we must readjust the amount of
4208 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004209 * variations. Note that the current state is >= HTTP_MSG_BODY,
4210 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004211 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004212 old_o += chn->buf->i - old_i;
4213 b_adv(chn->buf, old_o);
4214 txn->req.next -= old_o;
4215 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004216 }
4217
Mark Lamourinec2247f02012-01-04 13:02:01 -05004218 return 0;
4219}
4220
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221/* Terminate current transaction and prepare a new one. This is very tricky
4222 * right now but it works.
4223 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004224void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004225{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004226 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004227 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004228 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004229 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004230 struct connection *srv_conn;
4231 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004232 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004233
Willy Tarreau610ecce2010-01-04 21:15:02 +01004234 /* FIXME: We need a more portable way of releasing a backend's and a
4235 * server's connections. We need a safer way to reinitialize buffer
4236 * flags. We also need a more accurate method for computing per-request
4237 * data.
4238 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004239 /*
4240 * XXX cognet: This is probably wrong, this is killing a whole
4241 * connection, in the new world order, we probably want to just kill
4242 * the stream, this is to be revisited the day we handle multiple
4243 * streams in one server connection.
4244 */
4245 cs = objt_cs(s->si[1].end);
4246 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247
Willy Tarreau4213a112013-12-15 10:25:42 +01004248 /* unless we're doing keep-alive, we want to quickly close the connection
4249 * to the server.
4250 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004251 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004252 !si_conn_ready(&s->si[1])) {
4253 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4254 si_shutr(&s->si[1]);
4255 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004256 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257
Willy Tarreaue7dff022015-04-03 01:14:29 +02004258 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004259 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004260 if (unlikely(s->srv_conn))
4261 sess_change_server(s, NULL);
4262 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263
4264 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004265 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266
Willy Tarreaueee5b512015-04-03 23:46:31 +02004267 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 int n;
4269
Willy Tarreaueee5b512015-04-03 23:46:31 +02004270 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 if (n < 1 || n > 5)
4272 n = 0;
4273
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004274 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004275 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004276 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004277 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004278 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004279 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4280 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004281 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004282 }
4283
4284 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004285 s->logs.bytes_in -= s->req.buf->i;
4286 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287
4288 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004289 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004290 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004291 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292 s->do_log(s);
4293 }
4294
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004295 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004296 stream_stop_content_counters(s);
4297 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004298
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 s->logs.accept_date = date; /* user-visible date for logging */
4300 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004301 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4302 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004303 tv_zero(&s->logs.tv_request);
4304 s->logs.t_queue = -1;
4305 s->logs.t_connect = -1;
4306 s->logs.t_data = -1;
4307 s->logs.t_close = 0;
4308 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4309 s->logs.srv_queue_size = 0; /* we will get this number soon */
4310
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004311 s->logs.bytes_in = s->req.total = s->req.buf->i;
4312 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313
4314 if (s->pend_pos)
4315 pendconn_free(s->pend_pos);
4316
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004317 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004318 if (s->flags & SF_CURR_SESS) {
4319 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004320 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004321 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004322 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004323 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004324 }
4325
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004326 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327
Willy Tarreau4213a112013-12-15 10:25:42 +01004328 /* only release our endpoint if we don't intend to reuse the
4329 * connection.
4330 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004331 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004332 !si_conn_ready(&s->si[1])) {
4333 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004334 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004335 }
4336
Willy Tarreau350f4872014-11-28 14:42:25 +01004337 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4338 s->si[1].err_type = SI_ET_NONE;
4339 s->si[1].conn_retries = 0; /* used for logging too */
4340 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004341 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 +01004342 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 +01004343 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 +02004344 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4345 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4346 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004347
Willy Tarreaueee5b512015-04-03 23:46:31 +02004348 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004350 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004351
4352 if (prev_status == 401 || prev_status == 407) {
4353 /* In HTTP keep-alive mode, if we receive a 401, we still have
4354 * a chance of being able to send the visitor again to the same
4355 * server over the same connection. This is required by some
4356 * broken protocols such as NTLM, and anyway whenever there is
4357 * an opportunity for sending the challenge to the proper place,
4358 * it's better to do it (at least it helps with debugging).
4359 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004360 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004361 if (srv_conn)
4362 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004363 }
4364
Willy Tarreau53f96852016-02-02 18:50:47 +01004365 /* Never ever allow to reuse a connection from a non-reuse backend */
4366 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4367 srv_conn->flags |= CO_FL_PRIVATE;
4368
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004369 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004370 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004371
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004372 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004373 s->req.flags |= CF_NEVER_WAIT;
4374 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004375 }
4376
Willy Tarreau714ea782015-11-25 20:11:11 +01004377 /* we're removing the analysers, we MUST re-enable events detection.
4378 * We don't enable close on the response channel since it's either
4379 * already closed, or in keep-alive with an idle connection handler.
4380 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004381 channel_auto_read(&s->req);
4382 channel_auto_close(&s->req);
4383 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004384
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004385 /* we're in keep-alive with an idle connection, monitor it if not already done */
4386 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004387 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004388 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004389 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004390 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004391 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004392 else if (prev_flags & TX_NOT_FIRST)
4393 /* note: we check the request, not the connection, but
4394 * this is valid for strategies SAFE and AGGR, and in
4395 * case of ALWS, we don't care anyway.
4396 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004397 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004398 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004399 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004400 }
Christopher Faulete6006242017-03-10 11:52:44 +01004401 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4402 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004403}
4404
4405
4406/* This function updates the request state machine according to the response
4407 * state machine and buffer flags. It returns 1 if it changes anything (flag
4408 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4409 * it is only used to find when a request/response couple is complete. Both
4410 * this function and its equivalent should loop until both return zero. It
4411 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4412 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004413int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004414{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004415 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004416 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004417 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004418 unsigned int old_state = txn->req.msg_state;
4419
Christopher Faulet4be98032017-07-18 10:48:24 +02004420 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004421 return 0;
4422
4423 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004424 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004425 * We can shut the read side unless we want to abort_on_close,
4426 * or we have a POST request. The issue with POST requests is
4427 * that some browsers still send a CRLF after the request, and
4428 * this CRLF must be read so that it does not remain in the kernel
4429 * buffers, otherwise a close could cause an RST on some systems
4430 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004431 * Note that if we're using keep-alive on the client side, we'd
4432 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004433 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004434 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004435 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004436 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4437 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4438 !(s->be->options & PR_O_ABRT_CLOSE) &&
4439 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004440 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441
Willy Tarreau40f151a2012-12-20 12:10:09 +01004442 /* if the server closes the connection, we want to immediately react
4443 * and close the socket to save packets and syscalls.
4444 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004445 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004446
Willy Tarreau7f876a12015-11-18 11:59:55 +01004447 /* In any case we've finished parsing the request so we must
4448 * disable Nagle when sending data because 1) we're not going
4449 * to shut this side, and 2) the server is waiting for us to
4450 * send pending data.
4451 */
4452 chn->flags |= CF_NEVER_WAIT;
4453
Willy Tarreau610ecce2010-01-04 21:15:02 +01004454 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4455 goto wait_other_side;
4456
4457 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4458 /* The server has not finished to respond, so we
4459 * don't want to move in order not to upset it.
4460 */
4461 goto wait_other_side;
4462 }
4463
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 /* When we get here, it means that both the request and the
4465 * response have finished receiving. Depending on the connection
4466 * mode, we'll have to wait for the last bytes to leave in either
4467 * direction, and sometimes for a close to be effective.
4468 */
4469
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004470 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4471 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004472 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4473 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004474 }
4475 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4476 /* Option forceclose is set, or either side wants to close,
4477 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004478 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004479 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004480 *
4481 * However, there is an exception if the response
4482 * length is undefined. In this case, we need to wait
4483 * the close from the server. The response will be
4484 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004485 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004486 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4487 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4488 goto check_channel_flags;
4489
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004490 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4491 channel_shutr_now(chn);
4492 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004494 }
4495 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004496 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4497 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004498 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004499 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4500 channel_auto_read(chn);
4501 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004502 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004503 }
4504
Christopher Faulet4be98032017-07-18 10:48:24 +02004505 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 }
4507
4508 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4509 http_msg_closing:
4510 /* nothing else to forward, just waiting for the output buffer
4511 * to be empty and for the shutw_now to take effect.
4512 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004513 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 txn->req.msg_state = HTTP_MSG_CLOSED;
4515 goto http_msg_closed;
4516 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004517 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004518 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004519 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004521 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 }
4523
4524 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4525 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004526 /* if we don't know whether the server will close, we need to hard close */
4527 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4528 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4529
Willy Tarreau3988d932013-12-27 23:03:08 +01004530 /* see above in MSG_DONE why we only do this in these states */
4531 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4532 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4533 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004534 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004535 goto wait_other_side;
4536 }
4537
Christopher Faulet4be98032017-07-18 10:48:24 +02004538 check_channel_flags:
4539 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4540 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4541 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004542 txn->req.msg_state = HTTP_MSG_CLOSING;
4543 goto http_msg_closing;
4544 }
4545
4546
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004548 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004549}
4550
4551
4552/* This function updates the response state machine according to the request
4553 * state machine and buffer flags. It returns 1 if it changes anything (flag
4554 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4555 * it is only used to find when a request/response couple is complete. Both
4556 * this function and its equivalent should loop until both return zero. It
4557 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4558 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004559int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004560{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004561 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004562 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004563 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 unsigned int old_state = txn->rsp.msg_state;
4565
Christopher Faulet4be98032017-07-18 10:48:24 +02004566 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 return 0;
4568
4569 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4570 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004571 * still monitor the server connection for a possible close
4572 * while the request is being uploaded, so we don't disable
4573 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004574 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004575 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576
4577 if (txn->req.msg_state == HTTP_MSG_ERROR)
4578 goto wait_other_side;
4579
4580 if (txn->req.msg_state < HTTP_MSG_DONE) {
4581 /* The client seems to still be sending data, probably
4582 * because we got an error response during an upload.
4583 * We have the choice of either breaking the connection
4584 * or letting it pass through. Let's do the later.
4585 */
4586 goto wait_other_side;
4587 }
4588
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 /* When we get here, it means that both the request and the
4590 * response have finished receiving. Depending on the connection
4591 * mode, we'll have to wait for the last bytes to leave in either
4592 * direction, and sometimes for a close to be effective.
4593 */
4594
4595 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4596 /* Server-close mode : shut read and wait for the request
4597 * side to close its output buffer. The caller will detect
4598 * when we're in DONE and the other is in CLOSED and will
4599 * catch that for the final cleanup.
4600 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4602 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004603 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004604 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4605 /* Option forceclose is set, or either side wants to close,
4606 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004607 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004608 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004609 *
4610 * However, there is an exception if the response length
4611 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004612 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004613 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4614 channel_auto_read(chn);
4615 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4616 chn->flags |= CF_NEVER_WAIT;
4617 }
4618 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004619 channel_shutr_now(chn);
4620 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004621 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622 }
4623 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004624 /* The last possible modes are keep-alive and tunnel. Tunnel will
4625 * need to forward remaining data. Keep-alive will need to monitor
4626 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004628 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004629 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004630 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4631 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004632 }
4633
Christopher Faulet4be98032017-07-18 10:48:24 +02004634 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 }
4636
4637 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4638 http_msg_closing:
4639 /* nothing else to forward, just waiting for the output buffer
4640 * to be empty and for the shutw_now to take effect.
4641 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004642 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004643 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4644 goto http_msg_closed;
4645 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004646 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004647 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004648 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004649 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004650 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004651 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004652 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004653 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004654 }
4655
4656 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4657 http_msg_closed:
4658 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004659 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004660 channel_auto_close(chn);
4661 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 goto wait_other_side;
4663 }
4664
Christopher Faulet4be98032017-07-18 10:48:24 +02004665 check_channel_flags:
4666 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4667 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4668 /* if we've just closed an output, let's switch */
4669 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4670 goto http_msg_closing;
4671 }
4672
Willy Tarreau610ecce2010-01-04 21:15:02 +01004673 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004674 /* We force the response to leave immediately if we're waiting for the
4675 * other side, since there is no pending shutdown to push it out.
4676 */
4677 if (!channel_is_empty(chn))
4678 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004679 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004680}
4681
4682
Christopher Faulet894da4c2017-07-18 11:29:07 +02004683/* Resync the request and response state machines. */
4684void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004686 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004687#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004688 int old_req_state = txn->req.msg_state;
4689 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004690#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004691
Willy Tarreau610ecce2010-01-04 21:15:02 +01004692 http_sync_req_state(s);
4693 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004694 if (!http_sync_res_state(s))
4695 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004696 if (!http_sync_req_state(s))
4697 break;
4698 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004699
Christopher Faulet894da4c2017-07-18 11:29:07 +02004700 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4701 "req->analysers=0x%08x res->analysers=0x%08x\n",
4702 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004703 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4704 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004705 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004706
4707
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 /* OK, both state machines agree on a compatible state.
4709 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4711 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004712 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4713 * means we must abort the request.
4714 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4715 * corresponding channel.
4716 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4717 * on the response with server-close mode means we've completed one
4718 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004719 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004720 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4721 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004722 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004723 channel_auto_close(&s->req);
4724 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004725 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004726 channel_auto_close(&s->res);
4727 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004728 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004729 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4730 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004731 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004732 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004733 channel_auto_close(&s->res);
4734 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004735 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004736 channel_abort(&s->req);
4737 channel_auto_close(&s->req);
4738 channel_auto_read(&s->req);
4739 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004740 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004741 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4742 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4743 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4744 s->req.analysers &= AN_REQ_FLT_END;
4745 if (HAS_REQ_DATA_FILTERS(s))
4746 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4747 }
4748 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4749 s->res.analysers &= AN_RES_FLT_END;
4750 if (HAS_RSP_DATA_FILTERS(s))
4751 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4752 }
4753 channel_auto_close(&s->req);
4754 channel_auto_read(&s->req);
4755 channel_auto_close(&s->res);
4756 channel_auto_read(&s->res);
4757 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004758 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4759 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004760 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004761 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4762 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4763 /* server-close/keep-alive: terminate this transaction,
4764 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004765 * a fresh-new transaction, but only once we're sure there's
4766 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004767 * another request. They must not hold any pending output data
4768 * and the response buffer must realigned
4769 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004770 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004771 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004772 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004773 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004774 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004775 else {
4776 s->req.analysers = AN_REQ_FLT_END;
4777 s->res.analysers = AN_RES_FLT_END;
4778 txn->flags |= TX_WAIT_CLEANUP;
4779 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004780 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004781}
4782
Willy Tarreaud98cf932009-12-27 22:54:55 +01004783/* This function is an analyser which forwards request body (including chunk
4784 * sizes if any). It is called as soon as we must forward, even if we forward
4785 * zero byte. The only situation where it must not be called is when we're in
4786 * tunnel mode and we want to forward till the close. It's used both to forward
4787 * remaining data and to resync after end of body. It expects the msg_state to
4788 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004789 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004790 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004791 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004792 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004793int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004794{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004795 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004796 struct http_txn *txn = s->txn;
4797 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004798 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004799
Christopher Faulet814d2702017-03-30 11:33:44 +02004800 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4801 now_ms, __FUNCTION__,
4802 s,
4803 req,
4804 req->rex, req->wex,
4805 req->flags,
4806 req->buf->i,
4807 req->analysers);
4808
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004809 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4810 return 0;
4811
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004812 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004813 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004814 /* Output closed while we were sending data. We must abort and
4815 * wake the other side up.
4816 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004817 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004818 msg->msg_state = HTTP_MSG_ERROR;
4819 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004820 return 1;
4821 }
4822
Willy Tarreaud98cf932009-12-27 22:54:55 +01004823 /* Note that we don't have to send 100-continue back because we don't
4824 * need the data to complete our job, and it's up to the server to
4825 * decide whether to return 100, 417 or anything else in return of
4826 * an "Expect: 100-continue" header.
4827 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004828 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004829 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4830 ? HTTP_MSG_CHUNK_SIZE
4831 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004832
4833 /* TODO/filters: when http-buffer-request option is set or if a
4834 * rule on url_param exists, the first chunk size could be
4835 * already parsed. In that case, msg->next is after the chunk
4836 * size (including the CRLF after the size). So this case should
4837 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004838 }
4839
Willy Tarreau7ba23542014-04-17 21:50:00 +02004840 /* Some post-connect processing might want us to refrain from starting to
4841 * forward data. Currently, the only reason for this is "balance url_param"
4842 * whichs need to parse/process the request after we've enabled forwarding.
4843 */
4844 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004845 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004846 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004847 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004848 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004849 }
4850 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4851 }
4852
Willy Tarreau80a92c02014-03-12 10:41:13 +01004853 /* in most states, we should abort in case of early close */
4854 channel_auto_close(req);
4855
Willy Tarreauefdf0942014-04-24 20:08:57 +02004856 if (req->to_forward) {
4857 /* We can't process the buffer's contents yet */
4858 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004859 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004860 }
4861
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004862 if (msg->msg_state < HTTP_MSG_DONE) {
4863 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4864 ? http_msg_forward_chunked_body(s, msg)
4865 : http_msg_forward_body(s, msg));
4866 if (!ret)
4867 goto missing_data_or_waiting;
4868 if (ret < 0)
4869 goto return_bad_req;
4870 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004871
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004872 /* other states, DONE...TUNNEL */
4873 /* we don't want to forward closes on DONE except in tunnel mode. */
4874 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4875 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004876
Christopher Faulet894da4c2017-07-18 11:29:07 +02004877 http_resync_states(s);
4878 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004879 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4880 if (req->flags & CF_SHUTW) {
4881 /* request errors are most likely due to the
4882 * server aborting the transfer. */
4883 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004884 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004885 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004886 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004887 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004888 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004889 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004890 }
4891
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004892 /* If "option abortonclose" is set on the backend, we want to monitor
4893 * the client's connection and forward any shutdown notification to the
4894 * server, which will decide whether to close or to go on processing the
4895 * request. We only do that in tunnel mode, and not in other modes since
4896 * it can be abused to exhaust source ports. */
4897 if (s->be->options & PR_O_ABRT_CLOSE) {
4898 channel_auto_read(req);
4899 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4900 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4901 s->si[1].flags |= SI_FL_NOLINGER;
4902 channel_auto_close(req);
4903 }
4904 else if (s->txn->meth == HTTP_METH_POST) {
4905 /* POST requests may require to read extra CRLF sent by broken
4906 * browsers and which could cause an RST to be sent upon close
4907 * on some systems (eg: Linux). */
4908 channel_auto_read(req);
4909 }
4910 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004911
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004912 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004913 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004914 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004915 if (!(s->flags & SF_ERR_MASK))
4916 s->flags |= SF_ERR_CLICL;
4917 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004918 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004919 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004920 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004921 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004922 }
4923
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004924 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4925 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004926 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004927 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004928
4929 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004930 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004931
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004932 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004933 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004934 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004935
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004936 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004937 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004938 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004939 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004940 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004941
Willy Tarreau5c620922011-05-11 19:56:11 +02004942 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004943 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004944 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004945 * modes are already handled by the stream sock layer. We must not do
4946 * this in content-length mode because it could present the MSG_MORE
4947 * flag with the last block of forwarded data, which would cause an
4948 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004949 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004950 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004951 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004952
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004953 return 0;
4954
Willy Tarreaud98cf932009-12-27 22:54:55 +01004955 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004956 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004957 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004958 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004959
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004960 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004961 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004962 txn->req.msg_state = HTTP_MSG_ERROR;
4963 if (txn->status) {
4964 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004965 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004966 } else {
4967 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004968 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004969 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004970 req->analysers &= AN_REQ_FLT_END;
4971 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 +01004972
Willy Tarreaue7dff022015-04-03 01:14:29 +02004973 if (!(s->flags & SF_ERR_MASK))
4974 s->flags |= SF_ERR_PRXCOND;
4975 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004976 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004977 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004978 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004979 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004980 }
4981 return 0;
4982
4983 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004984 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004985 txn->req.msg_state = HTTP_MSG_ERROR;
4986 if (txn->status) {
4987 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004988 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004989 } else {
4990 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004991 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004992 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004993 req->analysers &= AN_REQ_FLT_END;
4994 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 +01004995
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004996 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4997 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004998 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004999 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005000
Willy Tarreaue7dff022015-04-03 01:14:29 +02005001 if (!(s->flags & SF_ERR_MASK))
5002 s->flags |= SF_ERR_SRVCL;
5003 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005004 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005005 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005006 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005007 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005008 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005009 return 0;
5010}
5011
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005012/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5013 * processing can continue on next analysers, or zero if it either needs more
5014 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005015 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005016 * when it has nothing left to do, and may remove any analyser when it wants to
5017 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005018 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005019int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005020{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005021 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005022 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005023 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005024 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005025 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005026 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005027 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005028
Willy Tarreau87b09662015-04-03 00:22:06 +02005029 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 +02005030 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005031 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005032 rep,
5033 rep->rex, rep->wex,
5034 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005035 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005036 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005037
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005038 /*
5039 * Now parse the partial (or complete) lines.
5040 * We will check the response syntax, and also join multi-line
5041 * headers. An index of all the lines will be elaborated while
5042 * parsing.
5043 *
5044 * For the parsing, we use a 28 states FSM.
5045 *
5046 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005047 * rep->buf->p = beginning of response
5048 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5049 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005050 * msg->eol = end of current header or line (LF or CRLF)
5051 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052 */
5053
Willy Tarreau628c40c2014-04-24 19:11:26 +02005054 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005055 /* There's a protected area at the end of the buffer for rewriting
5056 * purposes. We don't want to start to parse the request if the
5057 * protected area is affected, because we may have to move processed
5058 * data later, which is much more complicated.
5059 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005060 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005061 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005062 /* some data has still not left the buffer, wake us once that's done */
5063 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5064 goto abort_response;
5065 channel_dont_close(rep);
5066 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005067 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005068 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005069 }
5070
Willy Tarreau379357a2013-06-08 12:55:46 +02005071 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5072 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5073 buffer_slow_realign(rep->buf);
5074
Willy Tarreau9b28e032012-10-12 23:49:43 +02005075 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005076 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005077 }
5078
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 /* 1: we might have to print this header in debug mode */
5080 if (unlikely((global.mode & MODE_DEBUG) &&
5081 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005082 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005083 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005084
Willy Tarreau9b28e032012-10-12 23:49:43 +02005085 sol = rep->buf->p;
5086 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005087 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089 sol += hdr_idx_first_pos(&txn->hdr_idx);
5090 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092 while (cur_idx) {
5093 eol = sol + txn->hdr_idx.v[cur_idx].len;
5094 debug_hdr("srvhdr", s, sol, eol);
5095 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5096 cur_idx = txn->hdr_idx.v[cur_idx].next;
5097 }
5098 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005099
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005100 /*
5101 * Now we quickly check if we have found a full valid response.
5102 * If not so, we check the FD and buffer states before leaving.
5103 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005104 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005105 * responses are checked first.
5106 *
5107 * Depending on whether the client is still there or not, we
5108 * may send an error response back or not. Note that normally
5109 * we should only check for HTTP status there, and check I/O
5110 * errors somewhere else.
5111 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005112
Willy Tarreau655dce92009-11-08 13:10:58 +01005113 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005114 /* Invalid response */
5115 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5116 /* we detected a parsing error. We want to archive this response
5117 * in the dedicated proxy area for later troubleshooting.
5118 */
5119 hdr_response_bad:
5120 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005121 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005122
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005123 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005124 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005125 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005126 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005127 }
Willy Tarreau64648412010-03-05 10:41:54 +01005128 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005129 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005130 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005131 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005132 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005133 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005134 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005135
Willy Tarreaue7dff022015-04-03 01:14:29 +02005136 if (!(s->flags & SF_ERR_MASK))
5137 s->flags |= SF_ERR_PRXCOND;
5138 if (!(s->flags & SF_FINST_MASK))
5139 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005140
5141 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005142 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005143
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005144 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005145 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005146 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005147 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 goto hdr_response_bad;
5149 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005152 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005153 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005154 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005155 else if (txn->flags & TX_NOT_FIRST)
5156 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005157
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005158 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005159 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005160 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005161 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005162 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005163
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005164 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005165 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005166 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005167
5168 /* Check to see if the server refused the early data.
5169 * If so, just send a 425
5170 */
5171 if (objt_cs(s->si[1].end)) {
5172 struct connection *conn = objt_cs(s->si[1].end)->conn;
5173
5174 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5175 txn->status = 425;
5176 }
5177
Willy Tarreau350f4872014-11-28 14:42:25 +01005178 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005179 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005180 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005181
Willy Tarreaue7dff022015-04-03 01:14:29 +02005182 if (!(s->flags & SF_ERR_MASK))
5183 s->flags |= SF_ERR_SRVCL;
5184 if (!(s->flags & SF_FINST_MASK))
5185 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005186 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005187 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005188
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005189 /* read timeout : return a 504 to the client. */
5190 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005192 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005193
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005194 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005196 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005197 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005198 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005199
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005200 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005201 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005203 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005204 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005205 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005206
Willy Tarreaue7dff022015-04-03 01:14:29 +02005207 if (!(s->flags & SF_ERR_MASK))
5208 s->flags |= SF_ERR_SRVTO;
5209 if (!(s->flags & SF_FINST_MASK))
5210 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005211 return 0;
5212 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005213
Willy Tarreauf003d372012-11-26 13:35:37 +01005214 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005215 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005216 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5217 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005218 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005219 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005220
Christopher Faulet0184ea72017-01-05 14:06:34 +01005221 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005222 channel_auto_close(rep);
5223
5224 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005225 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005226 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005227
Willy Tarreaue7dff022015-04-03 01:14:29 +02005228 if (!(s->flags & SF_ERR_MASK))
5229 s->flags |= SF_ERR_CLICL;
5230 if (!(s->flags & SF_FINST_MASK))
5231 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005232
Willy Tarreau87b09662015-04-03 00:22:06 +02005233 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005234 return 0;
5235 }
5236
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005237 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005238 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005239 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005240 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005241 else if (txn->flags & TX_NOT_FIRST)
5242 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005243
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005244 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005245 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005246 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005247 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005248 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005249
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005250 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005251 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005252 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005253 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005254 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005255 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005256
Willy Tarreaue7dff022015-04-03 01:14:29 +02005257 if (!(s->flags & SF_ERR_MASK))
5258 s->flags |= SF_ERR_SRVCL;
5259 if (!(s->flags & SF_FINST_MASK))
5260 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005261 return 0;
5262 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005263
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005264 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005265 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005266 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005267 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005268 else if (txn->flags & TX_NOT_FIRST)
5269 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005270
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005271 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005272 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005273 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005274
Willy Tarreaue7dff022015-04-03 01:14:29 +02005275 if (!(s->flags & SF_ERR_MASK))
5276 s->flags |= SF_ERR_CLICL;
5277 if (!(s->flags & SF_FINST_MASK))
5278 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005279
Willy Tarreau87b09662015-04-03 00:22:06 +02005280 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005281 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005282 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005283
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005284 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005285 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005286 return 0;
5287 }
5288
5289 /* More interesting part now : we know that we have a complete
5290 * response which at least looks like HTTP. We have an indicator
5291 * of each header's length, so we can parse them quickly.
5292 */
5293
5294 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005295 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005296
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005297 /*
5298 * 1: get the status code
5299 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005300 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005301 if (n < 1 || n > 5)
5302 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005303 /* when the client triggers a 4xx from the server, it's most often due
5304 * to a missing object or permission. These events should be tracked
5305 * because if they happen often, it may indicate a brute force or a
5306 * vulnerability scan.
5307 */
5308 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005309 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005310
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005311 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005312 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313
Willy Tarreau91852eb2015-05-01 13:26:00 +02005314 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5315 * exactly one digit "." one digit. This check may be disabled using
5316 * option accept-invalid-http-response.
5317 */
5318 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5319 if (msg->sl.st.v_l != 8) {
5320 msg->err_pos = 0;
5321 goto hdr_response_bad;
5322 }
5323
5324 if (rep->buf->p[4] != '/' ||
5325 !isdigit((unsigned char)rep->buf->p[5]) ||
5326 rep->buf->p[6] != '.' ||
5327 !isdigit((unsigned char)rep->buf->p[7])) {
5328 msg->err_pos = 4;
5329 goto hdr_response_bad;
5330 }
5331 }
5332
Willy Tarreau5b154472009-12-21 20:11:07 +01005333 /* check if the response is HTTP/1.1 or above */
5334 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005335 ((rep->buf->p[5] > '1') ||
5336 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005337 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005338
5339 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005340 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 +01005341
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005342 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005343 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005344
Willy Tarreau9b28e032012-10-12 23:49:43 +02005345 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005346
Willy Tarreau39650402010-03-15 19:44:39 +01005347 /* Adjust server's health based on status code. Note: status codes 501
5348 * and 505 are triggered on demand by client request, so we must not
5349 * count them as server failures.
5350 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005351 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005352 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005353 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005354 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005355 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005356 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005357
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005358 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005359 * We may be facing a 100-continue response, or any other informational
5360 * 1xx response which is non-final, in which case this is not the right
5361 * response, and we're waiting for the next one. Let's allow this response
5362 * to go to the client and wait for the next one. There's an exception for
5363 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005364 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005365 if (txn->status < 200 &&
5366 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005367 hdr_idx_init(&txn->hdr_idx);
5368 msg->next -= channel_forward(rep, msg->next);
5369 msg->msg_state = HTTP_MSG_RPBEFORE;
5370 txn->status = 0;
5371 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005372 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005373 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005374 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005375
Willy Tarreaua14ad722017-07-07 11:36:32 +02005376 /*
5377 * 2: check for cacheability.
5378 */
5379
5380 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005381 case 200:
5382 case 203:
5383 case 206:
5384 case 300:
5385 case 301:
5386 case 410:
5387 /* RFC2616 @13.4:
5388 * "A response received with a status code of
5389 * 200, 203, 206, 300, 301 or 410 MAY be stored
5390 * by a cache (...) unless a cache-control
5391 * directive prohibits caching."
5392 *
5393 * RFC2616 @9.5: POST method :
5394 * "Responses to this method are not cacheable,
5395 * unless the response includes appropriate
5396 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005397 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005399 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005400 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5401 break;
5402 default:
5403 break;
5404 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005405
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005406 /*
5407 * 3: we may need to capture headers
5408 */
5409 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005410 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005411 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005412 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413
Willy Tarreau557f1992015-05-01 10:05:17 +02005414 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5415 * by RFC7230#3.3.3 :
5416 *
5417 * The length of a message body is determined by one of the following
5418 * (in order of precedence):
5419 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005420 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5421 * the connection will become a tunnel immediately after the empty
5422 * line that concludes the header fields. A client MUST ignore
5423 * any Content-Length or Transfer-Encoding header fields received
5424 * in such a message. Any 101 response (Switching Protocols) is
5425 * managed in the same manner.
5426 *
5427 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005428 * (Informational), 204 (No Content), or 304 (Not Modified) status
5429 * code is always terminated by the first empty line after the
5430 * header fields, regardless of the header fields present in the
5431 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005432 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005433 * 3. If a Transfer-Encoding header field is present and the chunked
5434 * transfer coding (Section 4.1) is the final encoding, the message
5435 * body length is determined by reading and decoding the chunked
5436 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005437 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005438 * If a Transfer-Encoding header field is present in a response and
5439 * the chunked transfer coding is not the final encoding, the
5440 * message body length is determined by reading the connection until
5441 * it is closed by the server. If a Transfer-Encoding header field
5442 * is present in a request and the chunked transfer coding is not
5443 * the final encoding, the message body length cannot be determined
5444 * reliably; the server MUST respond with the 400 (Bad Request)
5445 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005446 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005447 * If a message is received with both a Transfer-Encoding and a
5448 * Content-Length header field, the Transfer-Encoding overrides the
5449 * Content-Length. Such a message might indicate an attempt to
5450 * perform request smuggling (Section 9.5) or response splitting
5451 * (Section 9.4) and ought to be handled as an error. A sender MUST
5452 * remove the received Content-Length field prior to forwarding such
5453 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005454 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005455 * 4. If a message is received without Transfer-Encoding and with
5456 * either multiple Content-Length header fields having differing
5457 * field-values or a single Content-Length header field having an
5458 * invalid value, then the message framing is invalid and the
5459 * recipient MUST treat it as an unrecoverable error. If this is a
5460 * request message, the server MUST respond with a 400 (Bad Request)
5461 * status code and then close the connection. If this is a response
5462 * message received by a proxy, the proxy MUST close the connection
5463 * to the server, discard the received response, and send a 502 (Bad
5464 * Gateway) response to the client. If this is a response message
5465 * received by a user agent, the user agent MUST close the
5466 * connection to the server and discard the received response.
5467 *
5468 * 5. If a valid Content-Length header field is present without
5469 * Transfer-Encoding, its decimal value defines the expected message
5470 * body length in octets. If the sender closes the connection or
5471 * the recipient times out before the indicated number of octets are
5472 * received, the recipient MUST consider the message to be
5473 * incomplete and close the connection.
5474 *
5475 * 6. If this is a request message and none of the above are true, then
5476 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005477 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005478 * 7. Otherwise, this is a response message without a declared message
5479 * body length, so the message body length is determined by the
5480 * number of octets received prior to the server closing the
5481 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005482 */
5483
5484 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005485 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005486 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005487 * FIXME: should we parse anyway and return an error on chunked encoding ?
5488 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005489 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5490 txn->status == 101)) {
5491 /* Either we've established an explicit tunnel, or we're
5492 * switching the protocol. In both cases, we're very unlikely
5493 * to understand the next protocols. We have to switch to tunnel
5494 * mode, so that we transfer the request and responses then let
5495 * this protocol pass unmodified. When we later implement specific
5496 * parsers for such protocols, we'll want to check the Upgrade
5497 * header which contains information about that protocol for
5498 * responses with status 101 (eg: see RFC2817 about TLS).
5499 */
5500 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5501 msg->flags |= HTTP_MSGF_XFER_LEN;
5502 goto end;
5503 }
5504
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005505 if (txn->meth == HTTP_METH_HEAD ||
5506 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005507 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005508 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005509 goto skip_content_length;
5510 }
5511
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005512 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005513 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005514 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005515 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005516 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5517 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005518 /* bad transfer-encoding (chunked followed by something else) */
5519 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005520 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005521 break;
5522 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005523 }
5524
Willy Tarreau1c913912015-04-30 10:57:51 +02005525 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005526 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005527 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005528 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5529 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5530 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005531 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005532 signed long long cl;
5533
Willy Tarreauad14f752011-09-02 20:33:27 +02005534 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005535 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005536 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005537 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005538
Willy Tarreauad14f752011-09-02 20:33:27 +02005539 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005540 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005541 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005542 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005543
Willy Tarreauad14f752011-09-02 20:33:27 +02005544 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005545 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005546 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005547 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005548
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005549 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005550 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005551 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005552 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005553
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005554 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005555 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005556 }
5557
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005558 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005559 /* Now we have to check if we need to modify the Connection header.
5560 * This is more difficult on the response than it is on the request,
5561 * because we can have two different HTTP versions and we don't know
5562 * how the client will interprete a response. For instance, let's say
5563 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5564 * HTTP/1.1 response without any header. Maybe it will bound itself to
5565 * HTTP/1.0 because it only knows about it, and will consider the lack
5566 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5567 * the lack of header as a keep-alive. Thus we will use two flags
5568 * indicating how a request MAY be understood by the client. In case
5569 * of multiple possibilities, we'll fix the header to be explicit. If
5570 * ambiguous cases such as both close and keepalive are seen, then we
5571 * will fall back to explicit close. Note that we won't take risks with
5572 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005573 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005574 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005575 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5576 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5577 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5578 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005579 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005580
Willy Tarreau70dffda2014-01-30 03:07:23 +01005581 /* this situation happens when combining pretend-keepalive with httpclose. */
5582 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005583 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005584 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005585 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5586
Willy Tarreau60466522010-01-18 19:08:45 +01005587 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005588 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005589 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5590 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005591
Willy Tarreau60466522010-01-18 19:08:45 +01005592 /* now adjust header transformations depending on current state */
5593 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5594 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5595 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005596 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005597 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005598 }
Willy Tarreau60466522010-01-18 19:08:45 +01005599 else { /* SCL / KAL */
5600 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005601 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005602 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005603 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005604
Willy Tarreau60466522010-01-18 19:08:45 +01005605 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005606 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005607
Willy Tarreau60466522010-01-18 19:08:45 +01005608 /* Some keep-alive responses are converted to Server-close if
5609 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005610 */
Willy Tarreau60466522010-01-18 19:08:45 +01005611 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5612 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005613 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005614 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005615 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005616 }
5617
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005618 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005619 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005620 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005621
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005622 /* end of job, return OK */
5623 rep->analysers &= ~an_bit;
5624 rep->analyse_exp = TICK_ETERNITY;
5625 channel_auto_close(rep);
5626 return 1;
5627
5628 abort_keep_alive:
5629 /* A keep-alive request to the server failed on a network error.
5630 * The client is required to retry. We need to close without returning
5631 * any other information so that the client retries.
5632 */
5633 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005634 rep->analysers &= AN_RES_FLT_END;
5635 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005636 channel_auto_close(rep);
5637 s->logs.logwait = 0;
5638 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005639 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005640 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005641 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005642 return 0;
5643}
5644
5645/* This function performs all the processing enabled for the current response.
5646 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005647 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005648 * other functions. It works like process_request (see indications above).
5649 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005650int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005651{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005652 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005653 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005654 struct http_msg *msg = &txn->rsp;
5655 struct proxy *cur_proxy;
5656 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005657 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005658
Willy Tarreau87b09662015-04-03 00:22:06 +02005659 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 +02005660 now_ms, __FUNCTION__,
5661 s,
5662 rep,
5663 rep->rex, rep->wex,
5664 rep->flags,
5665 rep->buf->i,
5666 rep->analysers);
5667
5668 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5669 return 0;
5670
Willy Tarreau70730dd2014-04-24 18:06:27 +02005671 /* The stats applet needs to adjust the Connection header but we don't
5672 * apply any filter there.
5673 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005674 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5675 rep->analysers &= ~an_bit;
5676 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005677 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005678 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005679
Willy Tarreau58975672014-04-24 21:13:57 +02005680 /*
5681 * We will have to evaluate the filters.
5682 * As opposed to version 1.2, now they will be evaluated in the
5683 * filters order and not in the header order. This means that
5684 * each filter has to be validated among all headers.
5685 *
5686 * Filters are tried with ->be first, then with ->fe if it is
5687 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005688 *
5689 * Maybe we are in resume condiion. In this case I choose the
5690 * "struct proxy" which contains the rule list matching the resume
5691 * pointer. If none of theses "struct proxy" match, I initialise
5692 * the process with the first one.
5693 *
5694 * In fact, I check only correspondance betwwen the current list
5695 * pointer and the ->fe rule list. If it doesn't match, I initialize
5696 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005697 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005698 if (s->current_rule_list == &sess->fe->http_res_rules)
5699 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005700 else
5701 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005702 while (1) {
5703 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005704
Willy Tarreau58975672014-04-24 21:13:57 +02005705 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005706 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005707 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005708
Willy Tarreau51d861a2015-05-22 17:30:48 +02005709 if (ret == HTTP_RULE_RES_BADREQ)
5710 goto return_srv_prx_502;
5711
5712 if (ret == HTTP_RULE_RES_DONE) {
5713 rep->analysers &= ~an_bit;
5714 rep->analyse_exp = TICK_ETERNITY;
5715 return 1;
5716 }
5717 }
5718
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005719 /* we need to be called again. */
5720 if (ret == HTTP_RULE_RES_YIELD) {
5721 channel_dont_close(rep);
5722 return 0;
5723 }
5724
Willy Tarreau58975672014-04-24 21:13:57 +02005725 /* try headers filters */
5726 if (rule_set->rsp_exp != NULL) {
5727 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5728 return_bad_resp:
5729 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005730 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005731 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005732 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005733 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005734 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005735 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005736 txn->status = 502;
5737 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005738 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005739 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005740 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005741 if (!(s->flags & SF_ERR_MASK))
5742 s->flags |= SF_ERR_PRXCOND;
5743 if (!(s->flags & SF_FINST_MASK))
5744 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005745 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005746 }
Willy Tarreau58975672014-04-24 21:13:57 +02005747 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005748
Willy Tarreau58975672014-04-24 21:13:57 +02005749 /* has the response been denied ? */
5750 if (txn->flags & TX_SVDENY) {
5751 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005752 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005753
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005754 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5755 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005756 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005757 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005758
Willy Tarreau58975672014-04-24 21:13:57 +02005759 goto return_srv_prx_502;
5760 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005761
Willy Tarreau58975672014-04-24 21:13:57 +02005762 /* add response headers from the rule sets in the same order */
5763 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005764 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005765 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005766 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005767 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005768 ret = acl_pass(ret);
5769 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5770 ret = !ret;
5771 if (!ret)
5772 continue;
5773 }
5774 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5775 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005776 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005777
Willy Tarreau58975672014-04-24 21:13:57 +02005778 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005779 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005780 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005781 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005782 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005783
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005784 /* After this point, this anayzer can't return yield, so we can
5785 * remove the bit corresponding to this analyzer from the list.
5786 *
5787 * Note that the intermediate returns and goto found previously
5788 * reset the analyzers.
5789 */
5790 rep->analysers &= ~an_bit;
5791 rep->analyse_exp = TICK_ETERNITY;
5792
Willy Tarreau58975672014-04-24 21:13:57 +02005793 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005794 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005795 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005796
Willy Tarreau58975672014-04-24 21:13:57 +02005797 /*
5798 * Now check for a server cookie.
5799 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005800 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005801 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005802
Willy Tarreau58975672014-04-24 21:13:57 +02005803 /*
5804 * Check for cache-control or pragma headers if required.
5805 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005806 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 +02005807 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005808
Willy Tarreau58975672014-04-24 21:13:57 +02005809 /*
5810 * Add server cookie in the response if needed
5811 */
5812 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5813 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005814 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005815 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5816 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5817 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5818 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5819 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005820 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005821 /* the server is known, it's not the one the client requested, or the
5822 * cookie's last seen date needs to be refreshed. We have to
5823 * insert a set-cookie here, except if we want to insert only on POST
5824 * requests and this one isn't. Note that servers which don't have cookies
5825 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005826 */
Willy Tarreau58975672014-04-24 21:13:57 +02005827 if (!objt_server(s->target)->cookie) {
5828 chunk_printf(&trash,
5829 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5830 s->be->cookie_name);
5831 }
5832 else {
5833 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005834
Willy Tarreau58975672014-04-24 21:13:57 +02005835 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5836 /* emit last_date, which is mandatory */
5837 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5838 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5839 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005840
Willy Tarreau58975672014-04-24 21:13:57 +02005841 if (s->be->cookie_maxlife) {
5842 /* emit first_date, which is either the original one or
5843 * the current date.
5844 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005845 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005846 s30tob64(txn->cookie_first_date ?
5847 txn->cookie_first_date >> 2 :
5848 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005849 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005850 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005851 }
Willy Tarreau58975672014-04-24 21:13:57 +02005852 chunk_appendf(&trash, "; path=/");
5853 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005854
Willy Tarreau58975672014-04-24 21:13:57 +02005855 if (s->be->cookie_domain)
5856 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005857
Willy Tarreau58975672014-04-24 21:13:57 +02005858 if (s->be->ck_opts & PR_CK_HTTPONLY)
5859 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005860
Willy Tarreau58975672014-04-24 21:13:57 +02005861 if (s->be->ck_opts & PR_CK_SECURE)
5862 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005863
Willy Tarreau58975672014-04-24 21:13:57 +02005864 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5865 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005866
Willy Tarreau58975672014-04-24 21:13:57 +02005867 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005868 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005869 /* the server did not change, only the date was updated */
5870 txn->flags |= TX_SCK_UPDATED;
5871 else
5872 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005873
Willy Tarreau58975672014-04-24 21:13:57 +02005874 /* Here, we will tell an eventual cache on the client side that we don't
5875 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5876 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5877 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005878 */
Willy Tarreau58975672014-04-24 21:13:57 +02005879 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005880
Willy Tarreau58975672014-04-24 21:13:57 +02005881 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005882
Willy Tarreau58975672014-04-24 21:13:57 +02005883 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5884 "Cache-control: private", 22) < 0))
5885 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005886 }
Willy Tarreau58975672014-04-24 21:13:57 +02005887 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005888
Willy Tarreau58975672014-04-24 21:13:57 +02005889 /*
5890 * Check if result will be cacheable with a cookie.
5891 * We'll block the response if security checks have caught
5892 * nasty things such as a cacheable cookie.
5893 */
5894 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5895 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5896 (s->be->options & PR_O_CHK_CACHE)) {
5897 /* we're in presence of a cacheable response containing
5898 * a set-cookie header. We'll block it as requested by
5899 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005900 */
Willy Tarreau58975672014-04-24 21:13:57 +02005901 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005902 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005903
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005904 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5905 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005906 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005907 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005908
Christopher Faulet767a84b2017-11-24 16:50:31 +01005909 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5910 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005911 send_log(s->be, LOG_ALERT,
5912 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5913 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5914 goto return_srv_prx_502;
5915 }
Willy Tarreau03945942009-12-22 16:50:27 +01005916
Willy Tarreau70730dd2014-04-24 18:06:27 +02005917 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005918 /*
5919 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5920 * If an "Upgrade" token is found, the header is left untouched in order
5921 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005922 * if anything but "Upgrade" is present in the Connection header. We don't
5923 * want to touch any 101 response either since it's switching to another
5924 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005925 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005926 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005927 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005928 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005929 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5930 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005931
Willy Tarreau58975672014-04-24 21:13:57 +02005932 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5933 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5934 /* we want a keep-alive response here. Keep-alive header
5935 * required if either side is not 1.1.
5936 */
5937 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5938 want_flags |= TX_CON_KAL_SET;
5939 }
5940 else {
5941 /* we want a close response here. Close header required if
5942 * the server is 1.1, regardless of the client.
5943 */
5944 if (msg->flags & HTTP_MSGF_VER_11)
5945 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005946 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005947
Willy Tarreau58975672014-04-24 21:13:57 +02005948 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5949 http_change_connection_header(txn, msg, want_flags);
5950 }
5951
5952 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005953 /* Always enter in the body analyzer */
5954 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5955 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005956
Willy Tarreau58975672014-04-24 21:13:57 +02005957 /* if the user wants to log as soon as possible, without counting
5958 * bytes from the server, then this is the right moment. We have
5959 * to temporarily assign bytes_out to log what we currently have.
5960 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005961 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005962 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5963 s->logs.bytes_out = txn->rsp.eoh;
5964 s->do_log(s);
5965 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005966 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005967 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005968}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005969
Willy Tarreaud98cf932009-12-27 22:54:55 +01005970/* This function is an analyser which forwards response body (including chunk
5971 * sizes if any). It is called as soon as we must forward, even if we forward
5972 * zero byte. The only situation where it must not be called is when we're in
5973 * tunnel mode and we want to forward till the close. It's used both to forward
5974 * remaining data and to resync after end of body. It expects the msg_state to
5975 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005976 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005977 *
5978 * It is capable of compressing response data both in content-length mode and
5979 * in chunked mode. The state machines follows different flows depending on
5980 * whether content-length and chunked modes are used, since there are no
5981 * trailers in content-length :
5982 *
5983 * chk-mode cl-mode
5984 * ,----- BODY -----.
5985 * / \
5986 * V size > 0 V chk-mode
5987 * .--> SIZE -------------> DATA -------------> CRLF
5988 * | | size == 0 | last byte |
5989 * | v final crlf v inspected |
5990 * | TRAILERS -----------> DONE |
5991 * | |
5992 * `----------------------------------------------'
5993 *
5994 * Compression only happens in the DATA state, and must be flushed in final
5995 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5996 * is performed at once on final states for all bytes parsed, or when leaving
5997 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005998 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005999int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006000{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006001 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006002 struct http_txn *txn = s->txn;
6003 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006004 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006005
Christopher Faulet814d2702017-03-30 11:33:44 +02006006 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6007 now_ms, __FUNCTION__,
6008 s,
6009 res,
6010 res->rex, res->wex,
6011 res->flags,
6012 res->buf->i,
6013 res->analysers);
6014
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006015 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6016 return 0;
6017
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006018 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006019 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006020 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006021 /* Output closed while we were sending data. We must abort and
6022 * wake the other side up.
6023 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006024 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006025 msg->msg_state = HTTP_MSG_ERROR;
6026 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006027 return 1;
6028 }
6029
Willy Tarreau4fe41902010-06-07 22:27:41 +02006030 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006031 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006032
Christopher Fauletd7c91962015-04-30 11:48:27 +02006033 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006034 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6035 ? HTTP_MSG_CHUNK_SIZE
6036 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006037 }
6038
Willy Tarreauefdf0942014-04-24 20:08:57 +02006039 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006040 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006041 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006042 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006043 }
6044
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006045 if (msg->msg_state < HTTP_MSG_DONE) {
6046 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6047 ? http_msg_forward_chunked_body(s, msg)
6048 : http_msg_forward_body(s, msg));
6049 if (!ret)
6050 goto missing_data_or_waiting;
6051 if (ret < 0)
6052 goto return_bad_res;
6053 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006054
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006055 /* other states, DONE...TUNNEL */
6056 /* for keep-alive we don't want to forward closes on DONE */
6057 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6058 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6059 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006060
Christopher Faulet894da4c2017-07-18 11:29:07 +02006061 http_resync_states(s);
6062 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006063 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6064 if (res->flags & CF_SHUTW) {
6065 /* response errors are most likely due to the
6066 * client aborting the transfer. */
6067 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006068 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006069 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006070 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 +01006071 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006072 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006073 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006074 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006075 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006076
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006077 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006078 if (res->flags & CF_SHUTW)
6079 goto aborted_xfer;
6080
6081 /* stop waiting for data if the input is closed before the end. If the
6082 * client side was already closed, it means that the client has aborted,
6083 * so we don't want to count this as a server abort. Otherwise it's a
6084 * server abort.
6085 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006086 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006087 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006088 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006089 /* If we have some pending data, we continue the processing */
6090 if (!buffer_pending(res->buf)) {
6091 if (!(s->flags & SF_ERR_MASK))
6092 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006093 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006094 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006095 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006096 goto return_bad_res_stats_ok;
6097 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006098 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006099
Willy Tarreau40dba092010-03-04 18:14:51 +01006100 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006101 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006102 goto return_bad_res;
6103
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006104 /* When TE: chunked is used, we need to get there again to parse
6105 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006106 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6107 * or if there are filters registered on the stream, we don't want to
6108 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006109 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006110 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006111 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006112 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6113 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006114 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006115
Willy Tarreau5c620922011-05-11 19:56:11 +02006116 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006117 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006118 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006119 * modes are already handled by the stream sock layer. We must not do
6120 * this in content-length mode because it could present the MSG_MORE
6121 * flag with the last block of forwarded data, which would cause an
6122 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006123 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006124 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006125 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006126
Willy Tarreau87b09662015-04-03 00:22:06 +02006127 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006128 return 0;
6129
Willy Tarreau40dba092010-03-04 18:14:51 +01006130 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006131 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006132 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006133 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006134
6135 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006136 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006137 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006138 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006139 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006140 res->analysers &= AN_RES_FLT_END;
6141 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 +01006142 if (objt_server(s->target))
6143 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006144
Willy Tarreaue7dff022015-04-03 01:14:29 +02006145 if (!(s->flags & SF_ERR_MASK))
6146 s->flags |= SF_ERR_PRXCOND;
6147 if (!(s->flags & SF_FINST_MASK))
6148 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006149 return 0;
6150
6151 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006152 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006153 txn->rsp.msg_state = HTTP_MSG_ERROR;
6154 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006155 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006156 res->analysers &= AN_RES_FLT_END;
6157 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 +01006158
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006159 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6160 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006161 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006162 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006163
Willy Tarreaue7dff022015-04-03 01:14:29 +02006164 if (!(s->flags & SF_ERR_MASK))
6165 s->flags |= SF_ERR_CLICL;
6166 if (!(s->flags & SF_FINST_MASK))
6167 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006168 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006169}
6170
6171
6172static inline int
6173http_msg_forward_body(struct stream *s, struct http_msg *msg)
6174{
6175 struct channel *chn = msg->chn;
6176 int ret;
6177
6178 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6179
6180 if (msg->msg_state == HTTP_MSG_ENDING)
6181 goto ending;
6182
6183 /* Neither content-length, nor transfer-encoding was found, so we must
6184 * read the body until the server connection is closed. In that case, we
6185 * eat data as they come. Of course, this happens for response only. */
6186 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6187 unsigned long long len = (chn->buf->i - msg->next);
6188 msg->chunk_len += len;
6189 msg->body_len += len;
6190 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006191 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6192 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6193 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006194 msg->next += ret;
6195 msg->chunk_len -= ret;
6196 if (msg->chunk_len) {
6197 /* input empty or output full */
6198 if (chn->buf->i > msg->next)
6199 chn->flags |= CF_WAKE_WRITE;
6200 goto missing_data_or_waiting;
6201 }
6202
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006203 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6204 * always set for a request. */
6205 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6206 /* The server still sending data that should be filtered */
6207 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6208 goto missing_data_or_waiting;
6209 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006210
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006211 msg->msg_state = HTTP_MSG_ENDING;
6212
6213 ending:
6214 /* we may have some pending data starting at res->buf->p such as a last
6215 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006216 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6217 /* default_ret */ msg->next,
6218 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006219 b_adv(chn->buf, ret);
6220 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006221 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6222 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006223 if (msg->next)
6224 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006225
Christopher Fauletda02e172015-12-04 09:25:05 +01006226 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6227 /* default_ret */ 1,
6228 /* on_error */ goto error,
6229 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006230 msg->msg_state = HTTP_MSG_DONE;
6231 return 1;
6232
6233 missing_data_or_waiting:
6234 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006235 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6236 /* default_ret */ msg->next,
6237 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006238 b_adv(chn->buf, ret);
6239 msg->next -= ret;
6240 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6241 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006242 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006243 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006244 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006245 return 0;
6246 error:
6247 return -1;
6248}
6249
6250static inline int
6251http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6252{
6253 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006254 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006255 int ret;
6256
6257 /* Here we have the guarantee to be in one of the following state:
6258 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6259 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6260
6261 switch_states:
6262 switch (msg->msg_state) {
6263 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006264 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6265 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6266 /* on_error */ goto error);
6267 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006268 msg->chunk_len -= ret;
6269 if (msg->chunk_len) {
6270 /* input empty or output full */
6271 if (chn->buf->i > msg->next)
6272 chn->flags |= CF_WAKE_WRITE;
6273 goto missing_data_or_waiting;
6274 }
6275
6276 /* nothing left to forward for this chunk*/
6277 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6278 /* fall through for HTTP_MSG_CHUNK_CRLF */
6279
6280 case HTTP_MSG_CHUNK_CRLF:
6281 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006282 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006283 if (ret == 0)
6284 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006285 if (ret < 0) {
6286 msg->err_pos = chn->buf->i + ret;
6287 if (msg->err_pos < 0)
6288 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006289 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006290 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006291 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006292 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6293 /* fall through for HTTP_MSG_CHUNK_SIZE */
6294
6295 case HTTP_MSG_CHUNK_SIZE:
6296 /* read the chunk size and assign it to ->chunk_len,
6297 * then set ->next to point to the body and switch to
6298 * DATA or TRAILERS state.
6299 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006300 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006301 if (ret == 0)
6302 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006303 if (ret < 0) {
6304 msg->err_pos = chn->buf->i + ret;
6305 if (msg->err_pos < 0)
6306 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006307 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006308 }
6309
6310 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006311 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006312 msg->chunk_len = chunk;
6313 msg->body_len += chunk;
6314
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006315 if (msg->chunk_len) {
6316 msg->msg_state = HTTP_MSG_DATA;
6317 goto switch_states;
6318 }
6319 msg->msg_state = HTTP_MSG_TRAILERS;
6320 /* fall through for HTTP_MSG_TRAILERS */
6321
6322 case HTTP_MSG_TRAILERS:
6323 ret = http_forward_trailers(msg);
6324 if (ret < 0)
6325 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006326 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6327 /* default_ret */ 1,
6328 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006329 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006330 if (!ret)
6331 goto missing_data_or_waiting;
6332 break;
6333
6334 case HTTP_MSG_ENDING:
6335 goto ending;
6336
6337 default:
6338 /* This should no happen in this function */
6339 goto error;
6340 }
6341
6342 msg->msg_state = HTTP_MSG_ENDING;
6343 ending:
6344 /* we may have some pending data starting at res->buf->p such as a last
6345 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006346 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006347 /* default_ret */ msg->next,
6348 /* on_error */ goto error);
6349 b_adv(chn->buf, ret);
6350 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006351 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6352 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006353 if (msg->next)
6354 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006355
Christopher Fauletda02e172015-12-04 09:25:05 +01006356 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006357 /* default_ret */ 1,
6358 /* on_error */ goto error,
6359 /* on_wait */ goto waiting);
6360 msg->msg_state = HTTP_MSG_DONE;
6361 return 1;
6362
6363 missing_data_or_waiting:
6364 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006365 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006366 /* default_ret */ msg->next,
6367 /* on_error */ goto error);
6368 b_adv(chn->buf, ret);
6369 msg->next -= ret;
6370 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6371 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006372 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006373 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006374 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006375 return 0;
6376
6377 chunk_parsing_error:
6378 if (msg->err_pos >= 0) {
6379 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006380 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006381 msg->msg_state, strm_fe(s));
6382 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006383 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006384 msg, msg->msg_state, s->be);
6385 }
6386 error:
6387 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006388}
6389
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006391/* Iterate the same filter through all request headers.
6392 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006393 * Since it can manage the switch to another backend, it updates the per-proxy
6394 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006395 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006396int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006398 char *cur_ptr, *cur_end, *cur_next;
6399 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006400 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006401 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006402 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006404 last_hdr = 0;
6405
Willy Tarreau9b28e032012-10-12 23:49:43 +02006406 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006407 old_idx = 0;
6408
6409 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006410 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006411 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006412 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006413 (exp->action == ACT_ALLOW ||
6414 exp->action == ACT_DENY ||
6415 exp->action == ACT_TARPIT))
6416 return 0;
6417
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006418 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006419 if (!cur_idx)
6420 break;
6421
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006422 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006423 cur_ptr = cur_next;
6424 cur_end = cur_ptr + cur_hdr->len;
6425 cur_next = cur_end + cur_hdr->cr + 1;
6426
6427 /* Now we have one header between cur_ptr and cur_end,
6428 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006429 */
6430
Willy Tarreau15a53a42015-01-21 13:39:42 +01006431 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006433 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006434 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006435 last_hdr = 1;
6436 break;
6437
6438 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006439 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006440 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 break;
6442
6443 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006444 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006445 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006446 break;
6447
6448 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006449 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6450 if (trash.len < 0)
6451 return -1;
6452
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006453 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 /* FIXME: if the user adds a newline in the replacement, the
6455 * index will not be recalculated for now, and the new line
6456 * will not be counted as a new header.
6457 */
6458
6459 cur_end += delta;
6460 cur_next += delta;
6461 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006462 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 break;
6464
6465 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006466 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 cur_next += delta;
6468
Willy Tarreaufa355d42009-11-29 18:12:29 +01006469 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006470 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6471 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 cur_hdr->len = 0;
6473 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006474 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 break;
6476
6477 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006478 }
6479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 /* keep the link from this header to next one in case of later
6481 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006482 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 old_idx = cur_idx;
6484 }
6485 return 0;
6486}
6487
6488
6489/* Apply the filter to the request line.
6490 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6491 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006492 * Since it can manage the switch to another backend, it updates the per-proxy
6493 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006494 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006495int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 char *cur_ptr, *cur_end;
6498 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006499 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006500 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006501
Willy Tarreau3d300592007-03-18 18:34:41 +01006502 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006503 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006504 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 (exp->action == ACT_ALLOW ||
6506 exp->action == ACT_DENY ||
6507 exp->action == ACT_TARPIT))
6508 return 0;
6509 else if (exp->action == ACT_REMOVE)
6510 return 0;
6511
6512 done = 0;
6513
Willy Tarreau9b28e032012-10-12 23:49:43 +02006514 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006515 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006516
6517 /* Now we have the request line between cur_ptr and cur_end */
6518
Willy Tarreau15a53a42015-01-21 13:39:42 +01006519 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006520 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006521 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006522 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006523 done = 1;
6524 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006526 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006527 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528 done = 1;
6529 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006532 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 done = 1;
6534 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006536 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006537 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6538 if (trash.len < 0)
6539 return -1;
6540
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006541 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006542 /* FIXME: if the user adds a newline in the replacement, the
6543 * index will not be recalculated for now, and the new line
6544 * will not be counted as a new header.
6545 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006546
Willy Tarreaufa355d42009-11-29 18:12:29 +01006547 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006549 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 HTTP_MSG_RQMETH,
6551 cur_ptr, cur_end + 1,
6552 NULL, NULL);
6553 if (unlikely(!cur_end))
6554 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 /* we have a full request and we know that we have either a CR
6557 * or an LF at <ptr>.
6558 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006559 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6560 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561 /* there is no point trying this regex on headers */
6562 return 1;
6563 }
6564 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006565 return done;
6566}
Willy Tarreau97de6242006-12-27 17:18:38 +01006567
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568
Willy Tarreau58f10d72006-12-04 02:26:12 +01006569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006570/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006571 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006572 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006573 * unparsable request. Since it can manage the switch to another backend, it
6574 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006575 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006576int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006577{
Willy Tarreau192252e2015-04-04 01:47:55 +02006578 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006579 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006580 struct hdr_exp *exp;
6581
6582 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006583 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006585 /*
6586 * The interleaving of transformations and verdicts
6587 * makes it difficult to decide to continue or stop
6588 * the evaluation.
6589 */
6590
Willy Tarreau6c123b12010-01-28 20:22:06 +01006591 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6592 break;
6593
Willy Tarreau3d300592007-03-18 18:34:41 +01006594 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006596 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006598
6599 /* if this filter had a condition, evaluate it now and skip to
6600 * next filter if the condition does not match.
6601 */
6602 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006603 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006604 ret = acl_pass(ret);
6605 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6606 ret = !ret;
6607
6608 if (!ret)
6609 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006610 }
6611
6612 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006613 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006614 if (unlikely(ret < 0))
6615 return -1;
6616
6617 if (likely(ret == 0)) {
6618 /* The filter did not match the request, it can be
6619 * iterated through all headers.
6620 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006621 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6622 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006623 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006624 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006625 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626}
6627
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006628
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629/* Find the end of a cookie value contained between <s> and <e>. It works the
6630 * same way as with headers above except that the semi-colon also ends a token.
6631 * See RFC2965 for more information. Note that it requires a valid header to
6632 * return a valid result.
6633 */
6634char *find_cookie_value_end(char *s, const char *e)
6635{
6636 int quoted, qdpair;
6637
6638 quoted = qdpair = 0;
6639 for (; s < e; s++) {
6640 if (qdpair) qdpair = 0;
6641 else if (quoted) {
6642 if (*s == '\\') qdpair = 1;
6643 else if (*s == '"') quoted = 0;
6644 }
6645 else if (*s == '"') quoted = 1;
6646 else if (*s == ',' || *s == ';') return s;
6647 }
6648 return s;
6649}
6650
6651/* Delete a value in a header between delimiters <from> and <next> in buffer
6652 * <buf>. The number of characters displaced is returned, and the pointer to
6653 * the first delimiter is updated if required. The function tries as much as
6654 * possible to respect the following principles :
6655 * - replace <from> delimiter by the <next> one unless <from> points to a
6656 * colon, in which case <next> is simply removed
6657 * - set exactly one space character after the new first delimiter, unless
6658 * there are not enough characters in the block being moved to do so.
6659 * - remove unneeded spaces before the previous delimiter and after the new
6660 * one.
6661 *
6662 * It is the caller's responsibility to ensure that :
6663 * - <from> points to a valid delimiter or the colon ;
6664 * - <next> points to a valid delimiter or the final CR/LF ;
6665 * - there are non-space chars before <from> ;
6666 * - there is a CR/LF at or after <next>.
6667 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006668int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006669{
6670 char *prev = *from;
6671
6672 if (*prev == ':') {
6673 /* We're removing the first value, preserve the colon and add a
6674 * space if possible.
6675 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006676 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006677 next++;
6678 prev++;
6679 if (prev < next)
6680 *prev++ = ' ';
6681
Willy Tarreau2235b262016-11-05 15:50:20 +01006682 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 next++;
6684 } else {
6685 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006686 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006687 prev--;
6688 *from = prev;
6689
6690 /* copy the delimiter and if possible a space if we're
6691 * not at the end of the line.
6692 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006693 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006694 *prev++ = *next++;
6695 if (prev + 1 < next)
6696 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006697 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006698 next++;
6699 }
6700 }
6701 return buffer_replace2(buf, prev, next, NULL, 0);
6702}
6703
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006704/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006705 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006706 * desirable to call it only when needed. This code is quite complex because
6707 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6708 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006709 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006710void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006711{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006712 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006713 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006714 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006715 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006716 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6717 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006718
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006720 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006721 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006722
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006723 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006724 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006725 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006726
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006727 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 hdr_beg = hdr_next;
6729 hdr_end = hdr_beg + cur_hdr->len;
6730 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006731
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 /* We have one full header between hdr_beg and hdr_end, and the
6733 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734 * "Cookie:" headers.
6735 */
6736
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006738 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006739 old_idx = cur_idx;
6740 continue;
6741 }
6742
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 del_from = NULL; /* nothing to be deleted */
6744 preserve_hdr = 0; /* assume we may kill the whole header */
6745
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746 /* Now look for cookies. Conforming to RFC2109, we have to support
6747 * attributes whose name begin with a '$', and associate them with
6748 * the right cookie, if we want to delete this cookie.
6749 * So there are 3 cases for each cookie read :
6750 * 1) it's a special attribute, beginning with a '$' : ignore it.
6751 * 2) it's a server id cookie that we *MAY* want to delete : save
6752 * some pointers on it (last semi-colon, beginning of cookie...)
6753 * 3) it's an application cookie : we *MAY* have to delete a previous
6754 * "special" cookie.
6755 * At the end of loop, if a "special" cookie remains, we may have to
6756 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006757 * *MUST* delete it.
6758 *
6759 * Note: RFC2965 is unclear about the processing of spaces around
6760 * the equal sign in the ATTR=VALUE form. A careful inspection of
6761 * the RFC explicitly allows spaces before it, and not within the
6762 * tokens (attrs or values). An inspection of RFC2109 allows that
6763 * too but section 10.1.3 lets one think that spaces may be allowed
6764 * after the equal sign too, resulting in some (rare) buggy
6765 * implementations trying to do that. So let's do what servers do.
6766 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6767 * allowed quoted strings in values, with any possible character
6768 * after a backslash, including control chars and delimitors, which
6769 * causes parsing to become ambiguous. Browsers also allow spaces
6770 * within values even without quotes.
6771 *
6772 * We have to keep multiple pointers in order to support cookie
6773 * removal at the beginning, middle or end of header without
6774 * corrupting the header. All of these headers are valid :
6775 *
6776 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6777 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6778 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6779 * | | | | | | | | |
6780 * | | | | | | | | hdr_end <--+
6781 * | | | | | | | +--> next
6782 * | | | | | | +----> val_end
6783 * | | | | | +-----------> val_beg
6784 * | | | | +--------------> equal
6785 * | | | +----------------> att_end
6786 * | | +---------------------> att_beg
6787 * | +--------------------------> prev
6788 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006789 */
6790
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6792 /* Iterate through all cookies on this line */
6793
6794 /* find att_beg */
6795 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006796 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797 att_beg++;
6798
6799 /* find att_end : this is the first character after the last non
6800 * space before the equal. It may be equal to hdr_end.
6801 */
6802 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006803
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006804 while (equal < hdr_end) {
6805 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006806 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006807 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006808 continue;
6809 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006810 }
6811
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006812 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6813 * is between <att_beg> and <equal>, both may be identical.
6814 */
6815
6816 /* look for end of cookie if there is an equal sign */
6817 if (equal < hdr_end && *equal == '=') {
6818 /* look for the beginning of the value */
6819 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006820 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006821 val_beg++;
6822
6823 /* find the end of the value, respecting quotes */
6824 next = find_cookie_value_end(val_beg, hdr_end);
6825
6826 /* make val_end point to the first white space or delimitor after the value */
6827 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006828 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006829 val_end--;
6830 } else {
6831 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006832 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006833
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006834 /* We have nothing to do with attributes beginning with '$'. However,
6835 * they will automatically be removed if a header before them is removed,
6836 * since they're supposed to be linked together.
6837 */
6838 if (*att_beg == '$')
6839 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006840
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006841 /* Ignore cookies with no equal sign */
6842 if (equal == next) {
6843 /* This is not our cookie, so we must preserve it. But if we already
6844 * scheduled another cookie for removal, we cannot remove the
6845 * complete header, but we can remove the previous block itself.
6846 */
6847 preserve_hdr = 1;
6848 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006849 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006850 val_end += delta;
6851 next += delta;
6852 hdr_end += delta;
6853 hdr_next += delta;
6854 cur_hdr->len += delta;
6855 http_msg_move_end(&txn->req, delta);
6856 prev = del_from;
6857 del_from = NULL;
6858 }
6859 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006861
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 /* if there are spaces around the equal sign, we need to
6863 * strip them otherwise we'll get trouble for cookie captures,
6864 * or even for rewrites. Since this happens extremely rarely,
6865 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006866 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006867 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6868 int stripped_before = 0;
6869 int stripped_after = 0;
6870
6871 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006872 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 equal += stripped_before;
6874 val_beg += stripped_before;
6875 }
6876
6877 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006878 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006879 val_beg += stripped_after;
6880 stripped_before += stripped_after;
6881 }
6882
6883 val_end += stripped_before;
6884 next += stripped_before;
6885 hdr_end += stripped_before;
6886 hdr_next += stripped_before;
6887 cur_hdr->len += stripped_before;
6888 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006891
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006892 /* First, let's see if we want to capture this cookie. We check
6893 * that we don't already have a client side cookie, because we
6894 * can only capture one. Also as an optimisation, we ignore
6895 * cookies shorter than the declared name.
6896 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006897 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6898 (val_end - att_beg >= sess->fe->capture_namelen) &&
6899 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006900 int log_len = val_end - att_beg;
6901
Willy Tarreaubafbe012017-11-24 17:34:44 +01006902 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006903 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006905 if (log_len > sess->fe->capture_len)
6906 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 memcpy(txn->cli_cookie, att_beg, log_len);
6908 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006909 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006910 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006911
Willy Tarreaubca99692010-10-06 19:25:55 +02006912 /* Persistence cookies in passive, rewrite or insert mode have the
6913 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006915 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006916 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006917 * For cookies in prefix mode, the form is :
6918 *
6919 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006920 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006921 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6922 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6923 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6927 * have the server ID between val_beg and delim, and the original cookie between
6928 * delim+1 and val_end. Otherwise, delim==val_end :
6929 *
6930 * Cookie: NAME=SRV; # in all but prefix modes
6931 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6932 * | || || | |+-> next
6933 * | || || | +--> val_end
6934 * | || || +---------> delim
6935 * | || |+------------> val_beg
6936 * | || +-------------> att_end = equal
6937 * | |+-----------------> att_beg
6938 * | +------------------> prev
6939 * +-------------------------> hdr_beg
6940 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006941
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006942 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006943 for (delim = val_beg; delim < val_end; delim++)
6944 if (*delim == COOKIE_DELIM)
6945 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006946 } else {
6947 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006949 /* Now check if the cookie contains a date field, which would
6950 * appear after a vertical bar ('|') just after the server name
6951 * and before the delimiter.
6952 */
6953 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6954 if (vbar1) {
6955 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006956 * right is the last seen date. It is a base64 encoded
6957 * 30-bit value representing the UNIX date since the
6958 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006959 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006960 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006961 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006962 if (val_end - vbar1 >= 5) {
6963 val = b64tos30(vbar1);
6964 if (val > 0)
6965 txn->cookie_last_date = val << 2;
6966 }
6967 /* look for a second vertical bar */
6968 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6969 if (vbar1 && (val_end - vbar1 > 5)) {
6970 val = b64tos30(vbar1 + 1);
6971 if (val > 0)
6972 txn->cookie_first_date = val << 2;
6973 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006974 }
6975 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006976
Willy Tarreauf64d1412010-10-07 20:06:11 +02006977 /* if the cookie has an expiration date and the proxy wants to check
6978 * it, then we do that now. We first check if the cookie is too old,
6979 * then only if it has expired. We detect strict overflow because the
6980 * time resolution here is not great (4 seconds). Cookies with dates
6981 * in the future are ignored if their offset is beyond one day. This
6982 * allows an admin to fix timezone issues without expiring everyone
6983 * and at the same time avoids keeping unwanted side effects for too
6984 * long.
6985 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006986 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6987 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006988 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006989 txn->flags &= ~TX_CK_MASK;
6990 txn->flags |= TX_CK_OLD;
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 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006995 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6996 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006997 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006998 txn->flags &= ~TX_CK_MASK;
6999 txn->flags |= TX_CK_EXPIRED;
7000 delim = val_beg; // let's pretend we have not found the cookie
7001 txn->cookie_first_date = 0;
7002 txn->cookie_last_date = 0;
7003 }
7004
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005 /* Here, we'll look for the first running server which supports the cookie.
7006 * This allows to share a same cookie between several servers, for example
7007 * to dedicate backup servers to specific servers only.
7008 * However, to prevent clients from sticking to cookie-less backup server
7009 * when they have incidentely learned an empty cookie, we simply ignore
7010 * empty cookies and mark them as invalid.
7011 * The same behaviour is applied when persistence must be ignored.
7012 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007013 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007014 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007016 while (srv) {
7017 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7018 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007019 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007020 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007021 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007022 /* we found the server and we can use it */
7023 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007024 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007025 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007026 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 break;
7028 } else {
7029 /* we found a server, but it's down,
7030 * mark it as such and go on in case
7031 * another one is available.
7032 */
7033 txn->flags &= ~TX_CK_MASK;
7034 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007036 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007037 srv = srv->next;
7038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039
Willy Tarreauf64d1412010-10-07 20:06:11 +02007040 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007041 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007043 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007044 txn->flags |= TX_CK_UNUSED;
7045 else
7046 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007047 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007049 /* depending on the cookie mode, we may have to either :
7050 * - delete the complete cookie if we're in insert+indirect mode, so that
7051 * the server never sees it ;
7052 * - remove the server id from the cookie value, and tag the cookie as an
7053 * application cookie so that it does not get accidentely removed later,
7054 * if we're in cookie prefix mode
7055 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007056 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007057 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007058
Willy Tarreau9b28e032012-10-12 23:49:43 +02007059 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007060 val_end += delta;
7061 next += delta;
7062 hdr_end += delta;
7063 hdr_next += delta;
7064 cur_hdr->len += delta;
7065 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007067 del_from = NULL;
7068 preserve_hdr = 1; /* we want to keep this cookie */
7069 }
7070 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007071 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007072 del_from = prev;
7073 }
7074 } else {
7075 /* This is not our cookie, so we must preserve it. But if we already
7076 * scheduled another cookie for removal, we cannot remove the
7077 * complete header, but we can remove the previous block itself.
7078 */
7079 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007080
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007081 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007082 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007083 if (att_beg >= del_from)
7084 att_beg += delta;
7085 if (att_end >= del_from)
7086 att_end += delta;
7087 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 val_end += delta;
7089 next += delta;
7090 hdr_end += delta;
7091 hdr_next += delta;
7092 cur_hdr->len += delta;
7093 http_msg_move_end(&txn->req, delta);
7094 prev = del_from;
7095 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007098
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007099 /* continue with next cookie on this header line */
7100 att_beg = next;
7101 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 /* There are no more cookies on this line.
7104 * We may still have one (or several) marked for deletion at the
7105 * end of the line. We must do this now in two ways :
7106 * - if some cookies must be preserved, we only delete from the
7107 * mark to the end of line ;
7108 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007110 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007111 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007112 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007113 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007114 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115 cur_hdr->len += delta;
7116 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007117 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118
7119 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007120 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7121 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007123 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007125 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007126 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007127 }
7128
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007129 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007131 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132}
7133
7134
Willy Tarreaua15645d2007-03-18 16:22:39 +01007135/* Iterate the same filter through all response headers contained in <rtr>.
7136 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7137 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007138int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007139{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140 char *cur_ptr, *cur_end, *cur_next;
7141 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007142 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007144 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145
7146 last_hdr = 0;
7147
Willy Tarreau9b28e032012-10-12 23:49:43 +02007148 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 old_idx = 0;
7150
7151 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007152 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007154 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155 (exp->action == ACT_ALLOW ||
7156 exp->action == ACT_DENY))
7157 return 0;
7158
7159 cur_idx = txn->hdr_idx.v[old_idx].next;
7160 if (!cur_idx)
7161 break;
7162
7163 cur_hdr = &txn->hdr_idx.v[cur_idx];
7164 cur_ptr = cur_next;
7165 cur_end = cur_ptr + cur_hdr->len;
7166 cur_next = cur_end + cur_hdr->cr + 1;
7167
7168 /* Now we have one header between cur_ptr and cur_end,
7169 * and the next header starts at cur_next.
7170 */
7171
Willy Tarreau15a53a42015-01-21 13:39:42 +01007172 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173 switch (exp->action) {
7174 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007175 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 last_hdr = 1;
7177 break;
7178
7179 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007180 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181 last_hdr = 1;
7182 break;
7183
7184 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007185 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7186 if (trash.len < 0)
7187 return -1;
7188
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007189 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 /* FIXME: if the user adds a newline in the replacement, the
7191 * index will not be recalculated for now, and the new line
7192 * will not be counted as a new header.
7193 */
7194
7195 cur_end += delta;
7196 cur_next += delta;
7197 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007198 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007199 break;
7200
7201 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007202 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 cur_next += delta;
7204
Willy Tarreaufa355d42009-11-29 18:12:29 +01007205 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7207 txn->hdr_idx.used--;
7208 cur_hdr->len = 0;
7209 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007210 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 break;
7212
7213 }
7214 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007215
7216 /* keep the link from this header to next one in case of later
7217 * removal of next header.
7218 */
7219 old_idx = cur_idx;
7220 }
7221 return 0;
7222}
7223
7224
7225/* Apply the filter to the status line in the response buffer <rtr>.
7226 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7227 * or -1 if a replacement resulted in an invalid status line.
7228 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007229int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007230{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 char *cur_ptr, *cur_end;
7232 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007233 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007234 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235
7236
Willy Tarreau3d300592007-03-18 18:34:41 +01007237 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007239 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 (exp->action == ACT_ALLOW ||
7241 exp->action == ACT_DENY))
7242 return 0;
7243 else if (exp->action == ACT_REMOVE)
7244 return 0;
7245
7246 done = 0;
7247
Willy Tarreau9b28e032012-10-12 23:49:43 +02007248 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007249 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250
7251 /* Now we have the status line between cur_ptr and cur_end */
7252
Willy Tarreau15a53a42015-01-21 13:39:42 +01007253 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 switch (exp->action) {
7255 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007256 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007257 done = 1;
7258 break;
7259
7260 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007261 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262 done = 1;
7263 break;
7264
7265 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007266 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7267 if (trash.len < 0)
7268 return -1;
7269
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007270 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 /* FIXME: if the user adds a newline in the replacement, the
7272 * index will not be recalculated for now, and the new line
7273 * will not be counted as a new header.
7274 */
7275
Willy Tarreaufa355d42009-11-29 18:12:29 +01007276 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007277 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007278 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007279 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280 cur_ptr, cur_end + 1,
7281 NULL, NULL);
7282 if (unlikely(!cur_end))
7283 return -1;
7284
7285 /* we have a full respnse and we know that we have either a CR
7286 * or an LF at <ptr>.
7287 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007288 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007289 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007290 /* there is no point trying this regex on headers */
7291 return 1;
7292 }
7293 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 return done;
7295}
7296
7297
7298
7299/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007300 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7302 * unparsable response.
7303 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007304int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305{
Willy Tarreau192252e2015-04-04 01:47:55 +02007306 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007307 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007308 struct hdr_exp *exp;
7309
7310 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007311 int ret;
7312
7313 /*
7314 * The interleaving of transformations and verdicts
7315 * makes it difficult to decide to continue or stop
7316 * the evaluation.
7317 */
7318
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007319 if (txn->flags & TX_SVDENY)
7320 break;
7321
Willy Tarreau3d300592007-03-18 18:34:41 +01007322 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007323 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7324 exp->action == ACT_PASS)) {
7325 exp = exp->next;
7326 continue;
7327 }
7328
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007329 /* if this filter had a condition, evaluate it now and skip to
7330 * next filter if the condition does not match.
7331 */
7332 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007333 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007334 ret = acl_pass(ret);
7335 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7336 ret = !ret;
7337 if (!ret)
7338 continue;
7339 }
7340
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007342 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 if (unlikely(ret < 0))
7344 return -1;
7345
7346 if (likely(ret == 0)) {
7347 /* The filter did not match the response, it can be
7348 * iterated through all headers.
7349 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007350 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7351 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007352 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 }
7354 return 0;
7355}
7356
7357
Willy Tarreaua15645d2007-03-18 16:22:39 +01007358/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007359 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007360 * desirable to call it only when needed. This function is also used when we
7361 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007363void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007364{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007365 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007366 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007367 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007368 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007369 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007370 char *hdr_beg, *hdr_end, *hdr_next;
7371 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372
Willy Tarreaua15645d2007-03-18 16:22:39 +01007373 /* Iterate through the headers.
7374 * we start with the start line.
7375 */
7376 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007377 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007378
7379 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7380 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007381 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007382
7383 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007384 hdr_beg = hdr_next;
7385 hdr_end = hdr_beg + cur_hdr->len;
7386 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387
Willy Tarreau24581ba2010-08-31 22:39:35 +02007388 /* We have one full header between hdr_beg and hdr_end, and the
7389 * next header starts at hdr_next. We're only interested in
7390 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007391 */
7392
Willy Tarreau24581ba2010-08-31 22:39:35 +02007393 is_cookie2 = 0;
7394 prev = hdr_beg + 10;
7395 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007396 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007397 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7398 if (!val) {
7399 old_idx = cur_idx;
7400 continue;
7401 }
7402 is_cookie2 = 1;
7403 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007404 }
7405
Willy Tarreau24581ba2010-08-31 22:39:35 +02007406 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7407 * <prev> points to the colon.
7408 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007409 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007410
Willy Tarreau24581ba2010-08-31 22:39:35 +02007411 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7412 * check-cache is enabled) and we are not interested in checking
7413 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007414 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007415 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416 return;
7417
Willy Tarreau24581ba2010-08-31 22:39:35 +02007418 /* OK so now we know we have to process this response cookie.
7419 * The format of the Set-Cookie header is slightly different
7420 * from the format of the Cookie header in that it does not
7421 * support the comma as a cookie delimiter (thus the header
7422 * cannot be folded) because the Expires attribute described in
7423 * the original Netscape's spec may contain an unquoted date
7424 * with a comma inside. We have to live with this because
7425 * many browsers don't support Max-Age and some browsers don't
7426 * support quoted strings. However the Set-Cookie2 header is
7427 * clean.
7428 *
7429 * We have to keep multiple pointers in order to support cookie
7430 * removal at the beginning, middle or end of header without
7431 * corrupting the header (in case of set-cookie2). A special
7432 * pointer, <scav> points to the beginning of the set-cookie-av
7433 * fields after the first semi-colon. The <next> pointer points
7434 * either to the end of line (set-cookie) or next unquoted comma
7435 * (set-cookie2). All of these headers are valid :
7436 *
7437 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7438 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7439 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7440 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7441 * | | | | | | | | | |
7442 * | | | | | | | | +-> next hdr_end <--+
7443 * | | | | | | | +------------> scav
7444 * | | | | | | +--------------> val_end
7445 * | | | | | +--------------------> val_beg
7446 * | | | | +----------------------> equal
7447 * | | | +------------------------> att_end
7448 * | | +----------------------------> att_beg
7449 * | +------------------------------> prev
7450 * +-----------------------------------------> hdr_beg
7451 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452
Willy Tarreau24581ba2010-08-31 22:39:35 +02007453 for (; prev < hdr_end; prev = next) {
7454 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 /* find att_beg */
7457 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007458 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007459 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007460
Willy Tarreau24581ba2010-08-31 22:39:35 +02007461 /* find att_end : this is the first character after the last non
7462 * space before the equal. It may be equal to hdr_end.
7463 */
7464 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465
Willy Tarreau24581ba2010-08-31 22:39:35 +02007466 while (equal < hdr_end) {
7467 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7468 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007469 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 continue;
7471 att_end = equal;
7472 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007473
Willy Tarreau24581ba2010-08-31 22:39:35 +02007474 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7475 * is between <att_beg> and <equal>, both may be identical.
7476 */
7477
7478 /* look for end of cookie if there is an equal sign */
7479 if (equal < hdr_end && *equal == '=') {
7480 /* look for the beginning of the value */
7481 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007482 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007484
Willy Tarreau24581ba2010-08-31 22:39:35 +02007485 /* find the end of the value, respecting quotes */
7486 next = find_cookie_value_end(val_beg, hdr_end);
7487
7488 /* make val_end point to the first white space or delimitor after the value */
7489 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007490 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007491 val_end--;
7492 } else {
7493 /* <equal> points to next comma, semi-colon or EOL */
7494 val_beg = val_end = next = equal;
7495 }
7496
7497 if (next < hdr_end) {
7498 /* Set-Cookie2 supports multiple cookies, and <next> points to
7499 * a colon or semi-colon before the end. So skip all attr-value
7500 * pairs and look for the next comma. For Set-Cookie, since
7501 * commas are permitted in values, skip to the end.
7502 */
7503 if (is_cookie2)
7504 next = find_hdr_value_end(next, hdr_end);
7505 else
7506 next = hdr_end;
7507 }
7508
7509 /* Now everything is as on the diagram above */
7510
7511 /* Ignore cookies with no equal sign */
7512 if (equal == val_end)
7513 continue;
7514
7515 /* If there are spaces around the equal sign, we need to
7516 * strip them otherwise we'll get trouble for cookie captures,
7517 * or even for rewrites. Since this happens extremely rarely,
7518 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007520 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7521 int stripped_before = 0;
7522 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523
Willy Tarreau24581ba2010-08-31 22:39:35 +02007524 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007525 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007526 equal += stripped_before;
7527 val_beg += stripped_before;
7528 }
7529
7530 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007531 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007532 val_beg += stripped_after;
7533 stripped_before += stripped_after;
7534 }
7535
7536 val_end += stripped_before;
7537 next += stripped_before;
7538 hdr_end += stripped_before;
7539 hdr_next += stripped_before;
7540 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007541 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007542 }
7543
7544 /* First, let's see if we want to capture this cookie. We check
7545 * that we don't already have a server side cookie, because we
7546 * can only capture one. Also as an optimisation, we ignore
7547 * cookies shorter than the declared name.
7548 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007549 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007550 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007551 (val_end - att_beg >= sess->fe->capture_namelen) &&
7552 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007553 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007554 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007555 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007557 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007558 if (log_len > sess->fe->capture_len)
7559 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007560 memcpy(txn->srv_cookie, att_beg, log_len);
7561 txn->srv_cookie[log_len] = 0;
7562 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007563 }
7564
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007565 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007567 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007568 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7569 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007570 /* assume passive cookie by default */
7571 txn->flags &= ~TX_SCK_MASK;
7572 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007573
7574 /* If the cookie is in insert mode on a known server, we'll delete
7575 * this occurrence because we'll insert another one later.
7576 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 * a direct access.
7578 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007579 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007580 /* The "preserve" flag was set, we don't want to touch the
7581 * server's cookie.
7582 */
7583 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007584 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007585 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007586 /* this cookie must be deleted */
7587 if (*prev == ':' && next == hdr_end) {
7588 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007589 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007590 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7591 txn->hdr_idx.used--;
7592 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007593 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007594 hdr_next += delta;
7595 http_msg_move_end(&txn->rsp, delta);
7596 /* note: while both invalid now, <next> and <hdr_end>
7597 * are still equal, so the for() will stop as expected.
7598 */
7599 } else {
7600 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007601 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007602 next = prev;
7603 hdr_end += delta;
7604 hdr_next += delta;
7605 cur_hdr->len += delta;
7606 http_msg_move_end(&txn->rsp, delta);
7607 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007608 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007609 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007610 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007612 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007613 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007614 * with this server since we know it.
7615 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007616 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007617 next += delta;
7618 hdr_end += delta;
7619 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007621 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007622
Willy Tarreauf1348312010-10-07 15:54:11 +02007623 txn->flags &= ~TX_SCK_MASK;
7624 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007625 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007626 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007628 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007630 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007631 next += delta;
7632 hdr_end += delta;
7633 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007634 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007635 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007636
Willy Tarreau827aee92011-03-10 16:55:02 +01007637 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007638 txn->flags &= ~TX_SCK_MASK;
7639 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 }
7641 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 /* that's done for this cookie, check the next one on the same
7643 * line when next != hdr_end (only if is_cookie2).
7644 */
7645 }
7646 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007647 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007648 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649}
7650
7651
Willy Tarreaua15645d2007-03-18 16:22:39 +01007652/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007653 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007655void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007657 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007658 char *p1, *p2;
7659
7660 char *cur_ptr, *cur_end, *cur_next;
7661 int cur_idx;
7662
Willy Tarreau5df51872007-11-25 16:20:08 +01007663 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 return;
7665
7666 /* Iterate through the headers.
7667 * we start with the start line.
7668 */
7669 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007670 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671
7672 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7673 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007674 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007675
7676 cur_hdr = &txn->hdr_idx.v[cur_idx];
7677 cur_ptr = cur_next;
7678 cur_end = cur_ptr + cur_hdr->len;
7679 cur_next = cur_end + cur_hdr->cr + 1;
7680
7681 /* We have one full header between cur_ptr and cur_end, and the
7682 * next header starts at cur_next. We're only interested in
7683 * "Cookie:" headers.
7684 */
7685
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007686 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7687 if (val) {
7688 if ((cur_end - (cur_ptr + val) >= 8) &&
7689 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7690 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7691 return;
7692 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007693 }
7694
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007695 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7696 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007697 continue;
7698
7699 /* OK, right now we know we have a cache-control header at cur_ptr */
7700
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007701 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702
7703 if (p1 >= cur_end) /* no more info */
7704 continue;
7705
7706 /* p1 is at the beginning of the value */
7707 p2 = p1;
7708
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007709 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007710 p2++;
7711
7712 /* we have a complete value between p1 and p2 */
7713 if (p2 < cur_end && *p2 == '=') {
7714 /* we have something of the form no-cache="set-cookie" */
7715 if ((cur_end - p1 >= 21) &&
7716 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7717 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007718 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007719 continue;
7720 }
7721
7722 /* OK, so we know that either p2 points to the end of string or to a comma */
7723 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007724 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007725 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7726 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7727 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007728 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007729 return;
7730 }
7731
7732 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007733 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007734 continue;
7735 }
7736 }
7737}
7738
Willy Tarreau58f10d72006-12-04 02:26:12 +01007739
Willy Tarreaub2513902006-12-17 14:52:38 +01007740/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007741 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007742 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007743 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007744 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007745 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007746 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007747 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007748 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007749int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007750{
7751 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007752 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007753 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007754
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007755 if (!uri_auth)
7756 return 0;
7757
Cyril Bonté70be45d2010-10-12 00:14:35 +02007758 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007759 return 0;
7760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007761 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007762 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007763 return 0;
7764
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007765 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007766 return 0;
7767
Willy Tarreaub2513902006-12-17 14:52:38 +01007768 return 1;
7769}
7770
Willy Tarreau4076a152009-04-02 15:18:36 +02007771/*
7772 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007773 * By default it tries to report the error position as msg->err_pos. However if
7774 * this one is not set, it will then report msg->next, which is the last known
7775 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007776 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007777 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007778void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007779 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007780 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007781{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007782 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007783 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007784 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007785
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007786 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007787 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007788 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007789 len1 = MIN(len1, es->len);
7790 len2 = es->len - len1; /* remaining data if buffer wraps */
7791
Willy Tarreauf3764b72016-03-31 13:45:10 +02007792 if (!es->buf)
7793 es->buf = malloc(global.tune.bufsize);
7794
7795 if (es->buf) {
7796 memcpy(es->buf, chn->buf->p, len1);
7797 if (len2)
7798 memcpy(es->buf + len1, chn->buf->data, len2);
7799 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007800
Willy Tarreau4076a152009-04-02 15:18:36 +02007801 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007802 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007803 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007804 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007805
Willy Tarreau4076a152009-04-02 15:18:36 +02007806 es->when = date; // user-visible date
7807 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007808 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007809 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007810 if (objt_conn(sess->origin))
7811 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007812 else
7813 memset(&es->src, 0, sizeof(es->src));
7814
Willy Tarreau078272e2010-12-12 12:46:33 +01007815 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007816 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007817 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007818 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007819 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007820 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007821 es->b_out = chn->buf->o;
7822 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007823 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007824 es->m_clen = msg->chunk_len;
7825 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007826 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007827}
Willy Tarreaub2513902006-12-17 14:52:38 +01007828
Willy Tarreau294c4732011-12-16 21:35:50 +01007829/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7830 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7831 * performed over the whole headers. Otherwise it must contain a valid header
7832 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7833 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7834 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7835 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007836 * -1. The value fetch stops at commas, so this function is suited for use with
7837 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007838 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007839 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007840unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007841 struct hdr_idx *idx, int occ,
7842 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007843{
Willy Tarreau294c4732011-12-16 21:35:50 +01007844 struct hdr_ctx local_ctx;
7845 char *ptr_hist[MAX_HDR_HISTORY];
7846 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007847 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007848 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007849
Willy Tarreau294c4732011-12-16 21:35:50 +01007850 if (!ctx) {
7851 local_ctx.idx = 0;
7852 ctx = &local_ctx;
7853 }
7854
Willy Tarreaubce70882009-09-07 11:51:47 +02007855 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007856 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007857 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007858 occ--;
7859 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007860 *vptr = ctx->line + ctx->val;
7861 *vlen = ctx->vlen;
7862 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007863 }
7864 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007865 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007866 }
7867
7868 /* negative occurrence, we scan all the list then walk back */
7869 if (-occ > MAX_HDR_HISTORY)
7870 return 0;
7871
Willy Tarreau294c4732011-12-16 21:35:50 +01007872 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007873 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007874 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7875 len_hist[hist_ptr] = ctx->vlen;
7876 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007877 hist_ptr = 0;
7878 found++;
7879 }
7880 if (-occ > found)
7881 return 0;
7882 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007883 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7884 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7885 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007886 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007887 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007888 if (hist_ptr >= MAX_HDR_HISTORY)
7889 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007890 *vptr = ptr_hist[hist_ptr];
7891 *vlen = len_hist[hist_ptr];
7892 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007893}
7894
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007895/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7896 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7897 * performed over the whole headers. Otherwise it must contain a valid header
7898 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7899 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7900 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7901 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7902 * -1. This function differs from http_get_hdr() in that it only returns full
7903 * line header values and does not stop at commas.
7904 * The return value is 0 if nothing was found, or non-zero otherwise.
7905 */
7906unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7907 struct hdr_idx *idx, int occ,
7908 struct hdr_ctx *ctx, char **vptr, int *vlen)
7909{
7910 struct hdr_ctx local_ctx;
7911 char *ptr_hist[MAX_HDR_HISTORY];
7912 int len_hist[MAX_HDR_HISTORY];
7913 unsigned int hist_ptr;
7914 int found;
7915
7916 if (!ctx) {
7917 local_ctx.idx = 0;
7918 ctx = &local_ctx;
7919 }
7920
7921 if (occ >= 0) {
7922 /* search from the beginning */
7923 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7924 occ--;
7925 if (occ <= 0) {
7926 *vptr = ctx->line + ctx->val;
7927 *vlen = ctx->vlen;
7928 return 1;
7929 }
7930 }
7931 return 0;
7932 }
7933
7934 /* negative occurrence, we scan all the list then walk back */
7935 if (-occ > MAX_HDR_HISTORY)
7936 return 0;
7937
7938 found = hist_ptr = 0;
7939 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7940 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7941 len_hist[hist_ptr] = ctx->vlen;
7942 if (++hist_ptr >= MAX_HDR_HISTORY)
7943 hist_ptr = 0;
7944 found++;
7945 }
7946 if (-occ > found)
7947 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007948
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007949 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007950 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7951 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7952 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007953 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007954 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007955 if (hist_ptr >= MAX_HDR_HISTORY)
7956 hist_ptr -= MAX_HDR_HISTORY;
7957 *vptr = ptr_hist[hist_ptr];
7958 *vlen = len_hist[hist_ptr];
7959 return 1;
7960}
7961
Willy Tarreaubaaee002006-06-26 02:48:02 +02007962/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007963 * Print a debug line with a header. Always stop at the first CR or LF char,
7964 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7965 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007967void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007968{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007969 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007970 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007971
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007972 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007973 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007974 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007975 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 +02007976
7977 for (max = 0; start + max < end; max++)
7978 if (start[max] == '\r' || start[max] == '\n')
7979 break;
7980
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007981 UBOUND(max, trash.size - trash.len - 3);
7982 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7983 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007984 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985}
7986
Willy Tarreaueee5b512015-04-03 23:46:31 +02007987
7988/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7989 * The hdr_idx is allocated as well. In case of allocation failure, everything
7990 * allocated is freed and NULL is returned. Otherwise the new transaction is
7991 * assigned to the stream and returned.
7992 */
7993struct http_txn *http_alloc_txn(struct stream *s)
7994{
7995 struct http_txn *txn = s->txn;
7996
7997 if (txn)
7998 return txn;
7999
Willy Tarreaubafbe012017-11-24 17:34:44 +01008000 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008001 if (!txn)
8002 return txn;
8003
8004 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008005 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008006 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008007 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008008 return NULL;
8009 }
8010
8011 s->txn = txn;
8012 return txn;
8013}
8014
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008015void http_txn_reset_req(struct http_txn *txn)
8016{
8017 txn->req.flags = 0;
8018 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8019 txn->req.next = 0;
8020 txn->req.chunk_len = 0LL;
8021 txn->req.body_len = 0LL;
8022 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8023}
8024
8025void http_txn_reset_res(struct http_txn *txn)
8026{
8027 txn->rsp.flags = 0;
8028 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8029 txn->rsp.next = 0;
8030 txn->rsp.chunk_len = 0LL;
8031 txn->rsp.body_len = 0LL;
8032 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8033}
8034
Willy Tarreau0937bc42009-12-22 15:03:09 +01008035/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008036 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008037 * the required fields are properly allocated and that we only need to (re)init
8038 * them. This should be used before processing any new request.
8039 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008040void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008041{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008042 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008043 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008044
8045 txn->flags = 0;
8046 txn->status = -1;
8047
Willy Tarreauf64d1412010-10-07 20:06:11 +02008048 txn->cookie_first_date = 0;
8049 txn->cookie_last_date = 0;
8050
Willy Tarreaueee5b512015-04-03 23:46:31 +02008051 txn->srv_cookie = NULL;
8052 txn->cli_cookie = NULL;
8053 txn->uri = NULL;
8054
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008055 http_txn_reset_req(txn);
8056 http_txn_reset_res(txn);
8057
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008058 txn->req.chn = &s->req;
8059 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008060
8061 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008062
8063 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8064 if (fe->options2 & PR_O2_REQBUG_OK)
8065 txn->req.err_pos = -1; /* let buggy requests pass */
8066
Willy Tarreau0937bc42009-12-22 15:03:09 +01008067 if (txn->hdr_idx.v)
8068 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008069
8070 vars_init(&s->vars_txn, SCOPE_TXN);
8071 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008072}
8073
8074/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008075void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008076{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008077 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008078 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008079
8080 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008081 pool_free(pool_head_requri, txn->uri);
8082 pool_free(pool_head_capture, txn->cli_cookie);
8083 pool_free(pool_head_capture, txn->srv_cookie);
8084 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008085
William Lallemanda73203e2012-03-12 12:48:57 +01008086 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008087 txn->uri = NULL;
8088 txn->srv_cookie = NULL;
8089 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008090
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008091 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008092 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008093 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008094 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008095 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008096 }
8097
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008098 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008099 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008100 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008101 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008102 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008103 }
8104
Willy Tarreau6204cd92016-03-10 16:33:04 +01008105 vars_prune(&s->vars_txn, s->sess, s);
8106 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008107}
8108
8109/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008110void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008111{
8112 http_end_txn(s);
8113 http_init_txn(s);
8114
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008115 /* reinitialise the current rule list pointer to NULL. We are sure that
8116 * any rulelist match the NULL pointer.
8117 */
8118 s->current_rule_list = NULL;
8119
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008120 s->be = strm_fe(s);
8121 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008122 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008123 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008124 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008125 /* re-init store persistence */
8126 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008127 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008128
Willy Tarreau0937bc42009-12-22 15:03:09 +01008129 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008130
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008131 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008132
Willy Tarreau739cfba2010-01-25 23:11:14 +01008133 /* We must trim any excess data from the response buffer, because we
8134 * may have blocked an invalid response from a server that we don't
8135 * want to accidentely forward once we disable the analysers, nor do
8136 * we want those data to come along with next response. A typical
8137 * example of such data would be from a buggy server responding to
8138 * a HEAD with some data, or sending more than the advertised
8139 * content-length.
8140 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008141 if (unlikely(s->res.buf->i))
8142 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008143
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008144 /* Now we can realign the response buffer */
8145 buffer_realign(s->res.buf);
8146
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008147 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008148 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008149
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008150 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008151 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008152
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008153 s->req.rex = TICK_ETERNITY;
8154 s->req.wex = TICK_ETERNITY;
8155 s->req.analyse_exp = TICK_ETERNITY;
8156 s->res.rex = TICK_ETERNITY;
8157 s->res.wex = TICK_ETERNITY;
8158 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008159 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008160}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008161
Sasha Pachev218f0642014-06-16 12:05:59 -06008162void free_http_res_rules(struct list *r)
8163{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008164 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008165
8166 list_for_each_entry_safe(pr, tr, r, list) {
8167 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008168 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008169 free(pr);
8170 }
8171}
8172
8173void free_http_req_rules(struct list *r)
8174{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008175 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008176
8177 list_for_each_entry_safe(pr, tr, r, list) {
8178 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008179 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008180 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008181
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008182 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008183 free(pr);
8184 }
8185}
8186
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008187/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008188struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008189{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008190 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008191 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008192 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008193 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008194
Vincent Bernat02779b62016-04-03 13:48:43 +02008195 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008196 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008197 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008198 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008199 }
8200
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008201 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008202 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008203 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008204 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008205 int code;
8206 int hc;
8207
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008208 if (!strcmp(args[0], "tarpit")) {
8209 rule->action = ACT_HTTP_REQ_TARPIT;
8210 rule->deny_status = HTTP_ERR_500;
8211 }
8212 else {
8213 rule->action = ACT_ACTION_DENY;
8214 rule->deny_status = HTTP_ERR_403;
8215 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008216 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008217 if (strcmp(args[cur_arg], "deny_status") == 0) {
8218 cur_arg++;
8219 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008220 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8221 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008222 goto out_err;
8223 }
8224
8225 code = atol(args[cur_arg]);
8226 cur_arg++;
8227 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8228 if (http_err_codes[hc] == code) {
8229 rule->deny_status = hc;
8230 break;
8231 }
8232 }
8233
8234 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008235 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8236 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008237 }
8238 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008239 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008240 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008241 cur_arg = 1;
8242
8243 while(*args[cur_arg]) {
8244 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008245 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008246 cur_arg+=2;
8247 continue;
8248 } else
8249 break;
8250 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008251 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008252 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008253 cur_arg = 1;
8254
8255 if (!*args[cur_arg] ||
8256 (*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 +01008257 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8258 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008259 goto out_err;
8260 }
8261 rule->arg.nice = atoi(args[cur_arg]);
8262 if (rule->arg.nice < -1024)
8263 rule->arg.nice = -1024;
8264 else if (rule->arg.nice > 1024)
8265 rule->arg.nice = 1024;
8266 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008267 } else if (!strcmp(args[0], "set-tos")) {
8268#ifdef IP_TOS
8269 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008270 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008271 cur_arg = 1;
8272
8273 if (!*args[cur_arg] ||
8274 (*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 +01008275 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8276 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008277 goto out_err;
8278 }
8279
8280 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8281 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008282 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8283 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008284 goto out_err;
8285 }
8286 cur_arg++;
8287#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008288 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 +02008289 goto out_err;
8290#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008291 } else if (!strcmp(args[0], "set-mark")) {
8292#ifdef SO_MARK
8293 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008294 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008295 cur_arg = 1;
8296
8297 if (!*args[cur_arg] ||
8298 (*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 +01008299 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8300 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008301 goto out_err;
8302 }
8303
8304 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8305 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008306 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8307 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008308 goto out_err;
8309 }
8310 cur_arg++;
8311 global.last_checks |= LSTCHK_NETADM;
8312#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008313 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 +02008314 goto out_err;
8315#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008316 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008317 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008318 cur_arg = 1;
8319
8320 if (!*args[cur_arg] ||
8321 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8322 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008323 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8324 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008325 goto out_err;
8326 }
8327 if (strcmp(args[cur_arg], "silent") == 0)
8328 rule->arg.loglevel = -1;
8329 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8330 goto bad_log_level;
8331 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008332 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008333 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008334 cur_arg = 1;
8335
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008336 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8337 (*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 +01008338 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8339 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008340 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008341 }
8342
8343 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8344 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8345 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008346
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008347 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008348 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008349 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 +01008350 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008351 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8352 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008353 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008354 goto out_err;
8355 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008356 free(proxy->conf.lfs_file);
8357 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8358 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008359 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008360 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008361 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008362 cur_arg = 1;
8363
8364 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008365 (*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 +01008366 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8367 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008368 goto out_err;
8369 }
8370
8371 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8372 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8373 LIST_INIT(&rule->arg.hdr_add.fmt);
8374
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008375 error = NULL;
8376 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008377 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8378 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008379 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008380 goto out_err;
8381 }
8382
8383 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008384 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008385 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 +01008386 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008387 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8388 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008389 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008390 goto out_err;
8391 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008392
8393 free(proxy->conf.lfs_file);
8394 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8395 proxy->conf.lfs_line = proxy->conf.args.line;
8396 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008397 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008398 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008399 cur_arg = 1;
8400
8401 if (!*args[cur_arg] ||
8402 (*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 +01008403 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8404 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008405 goto out_err;
8406 }
8407
8408 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8409 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8410
8411 proxy->conf.args.ctx = ARGC_HRQ;
8412 free(proxy->conf.lfs_file);
8413 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8414 proxy->conf.lfs_line = proxy->conf.args.line;
8415 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008416 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8417 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008418 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008419 struct sample_expr *expr;
8420 unsigned int where;
8421 char *err = NULL;
8422
8423 cur_arg = 1;
8424 proxy->conf.args.ctx = ARGC_TRK;
8425
8426 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8427 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008428 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8429 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008430 free(err);
8431 goto out_err;
8432 }
8433
8434 where = 0;
8435 if (proxy->cap & PR_CAP_FE)
8436 where |= SMP_VAL_FE_HRQ_HDR;
8437 if (proxy->cap & PR_CAP_BE)
8438 where |= SMP_VAL_BE_HRQ_HDR;
8439
8440 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008441 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8442 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8443 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8444 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008445 free(expr);
8446 goto out_err;
8447 }
8448
8449 if (strcmp(args[cur_arg], "table") == 0) {
8450 cur_arg++;
8451 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008452 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8453 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008454 free(expr);
8455 goto out_err;
8456 }
8457 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008458 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008459 cur_arg++;
8460 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008461 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008462 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02008463 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008464 } else if (strcmp(args[0], "redirect") == 0) {
8465 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008466 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008467
Willy Tarreaube4653b2015-05-28 15:26:58 +02008468 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008469 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8470 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008471 goto out_err;
8472 }
8473
8474 /* this redirect rule might already contain a parsed condition which
8475 * we'll pass to the http-request rule.
8476 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008477 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008478 rule->arg.redir = redir;
8479 rule->cond = redir->cond;
8480 redir->cond = NULL;
8481 cur_arg = 2;
8482 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008483 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8484 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008485 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008486 /*
8487 * '+ 8' for 'add-acl('
8488 * '- 9' for 'add-acl(' + trailing ')'
8489 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008490 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008491
8492 cur_arg = 1;
8493
8494 if (!*args[cur_arg] ||
8495 (*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 +01008496 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8497 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008498 goto out_err;
8499 }
8500
8501 LIST_INIT(&rule->arg.map.key);
8502 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008503 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008504 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008505 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008506 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8507 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008508 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008509 goto out_err;
8510 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008511 free(proxy->conf.lfs_file);
8512 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8513 proxy->conf.lfs_line = proxy->conf.args.line;
8514 cur_arg += 1;
8515 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8516 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008517 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008518 /*
8519 * '+ 8' for 'del-acl('
8520 * '- 9' for 'del-acl(' + trailing ')'
8521 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008522 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008523
8524 cur_arg = 1;
8525
8526 if (!*args[cur_arg] ||
8527 (*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 +01008528 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8529 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008530 goto out_err;
8531 }
8532
8533 LIST_INIT(&rule->arg.map.key);
8534 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008535 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008536 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008537 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008538 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8539 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008540 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008541 goto out_err;
8542 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008543 free(proxy->conf.lfs_file);
8544 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8545 proxy->conf.lfs_line = proxy->conf.args.line;
8546 cur_arg += 1;
8547 } else if (strncmp(args[0], "del-map", 7) == 0) {
8548 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008549 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008550 /*
8551 * '+ 8' for 'del-map('
8552 * '- 9' for 'del-map(' + trailing ')'
8553 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008554 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008555
8556 cur_arg = 1;
8557
8558 if (!*args[cur_arg] ||
8559 (*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 +01008560 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8561 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008562 goto out_err;
8563 }
8564
8565 LIST_INIT(&rule->arg.map.key);
8566 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008567 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008568 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008569 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008570 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8571 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008572 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008573 goto out_err;
8574 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008575 free(proxy->conf.lfs_file);
8576 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8577 proxy->conf.lfs_line = proxy->conf.args.line;
8578 cur_arg += 1;
8579 } else if (strncmp(args[0], "set-map", 7) == 0) {
8580 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008581 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008582 /*
8583 * '+ 8' for 'set-map('
8584 * '- 9' for 'set-map(' + trailing ')'
8585 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008586 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008587
8588 cur_arg = 1;
8589
8590 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8591 (*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 +01008592 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8593 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008594 goto out_err;
8595 }
8596
8597 LIST_INIT(&rule->arg.map.key);
8598 LIST_INIT(&rule->arg.map.value);
8599 proxy->conf.args.ctx = ARGC_HRQ;
8600
8601 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008602 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008603 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008604 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008605 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8606 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008607 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008608 goto out_err;
8609 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008610
8611 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008612 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008613 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 +01008614 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008615 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8616 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008617 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008618 goto out_err;
8619 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008620 free(proxy->conf.lfs_file);
8621 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8622 proxy->conf.lfs_line = proxy->conf.args.line;
8623
8624 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008625 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8626 char *errmsg = NULL;
8627 cur_arg = 1;
8628 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008629 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008630 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008631 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008632 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8633 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008634 free(errmsg);
8635 goto out_err;
8636 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008637 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008638 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008639 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8640 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8641 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8642 "%s%s, but got '%s'%s.\n",
8643 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008644 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008645 }
8646
8647 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8648 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008649 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008650
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008651 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008652 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8653 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008654 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008655 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008656 }
8657 rule->cond = cond;
8658 }
8659 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008660 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8661 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8662 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008663 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008664 }
8665
8666 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008667 out_err:
8668 free(rule);
8669 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008670}
8671
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008672/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008673struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008674{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008675 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008676 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008677 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008678 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008679
8680 rule = calloc(1, sizeof(*rule));
8681 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008682 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008683 goto out_err;
8684 }
8685
8686 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008687 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008688 cur_arg = 1;
8689 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008690 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008691 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008692 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008693 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008694 cur_arg = 1;
8695
8696 if (!*args[cur_arg] ||
8697 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008698 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8699 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008700 goto out_err;
8701 }
8702 rule->arg.nice = atoi(args[cur_arg]);
8703 if (rule->arg.nice < -1024)
8704 rule->arg.nice = -1024;
8705 else if (rule->arg.nice > 1024)
8706 rule->arg.nice = 1024;
8707 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008708 } else if (!strcmp(args[0], "set-tos")) {
8709#ifdef IP_TOS
8710 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008711 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008712 cur_arg = 1;
8713
8714 if (!*args[cur_arg] ||
8715 (*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 +01008716 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8717 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008718 goto out_err;
8719 }
8720
8721 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8722 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008723 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8724 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008725 goto out_err;
8726 }
8727 cur_arg++;
8728#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008729 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 +02008730 goto out_err;
8731#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008732 } else if (!strcmp(args[0], "set-mark")) {
8733#ifdef SO_MARK
8734 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008735 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008736 cur_arg = 1;
8737
8738 if (!*args[cur_arg] ||
8739 (*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 +01008740 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8741 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008742 goto out_err;
8743 }
8744
8745 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8746 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008747 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8748 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008749 goto out_err;
8750 }
8751 cur_arg++;
8752 global.last_checks |= LSTCHK_NETADM;
8753#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008754 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 +02008755 goto out_err;
8756#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008757 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008758 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008759 cur_arg = 1;
8760
8761 if (!*args[cur_arg] ||
8762 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8763 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008764 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8765 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008766 goto out_err;
8767 }
8768 if (strcmp(args[cur_arg], "silent") == 0)
8769 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008770 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008771 goto bad_log_level;
8772 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008773 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008774 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008775 cur_arg = 1;
8776
8777 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8778 (*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 +01008779 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8780 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008781 goto out_err;
8782 }
8783
8784 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8785 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8786 LIST_INIT(&rule->arg.hdr_add.fmt);
8787
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008788 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008789 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008790 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008791 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008792 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8793 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008794 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008795 goto out_err;
8796 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008797 free(proxy->conf.lfs_file);
8798 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8799 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008800 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008801 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008802 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008803 cur_arg = 1;
8804
8805 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008806 (*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 +01008807 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8808 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008809 goto out_err;
8810 }
8811
8812 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8813 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8814 LIST_INIT(&rule->arg.hdr_add.fmt);
8815
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008816 error = NULL;
8817 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008818 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8819 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008820 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008821 goto out_err;
8822 }
8823
8824 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008825 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008826 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 +01008827 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008828 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8829 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008830 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008831 goto out_err;
8832 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008833
8834 free(proxy->conf.lfs_file);
8835 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8836 proxy->conf.lfs_line = proxy->conf.args.line;
8837 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008838 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008839 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008840 cur_arg = 1;
8841
8842 if (!*args[cur_arg] ||
8843 (*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 +01008844 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8845 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008846 goto out_err;
8847 }
8848
8849 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8850 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8851
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008852 proxy->conf.args.ctx = ARGC_HRS;
8853 free(proxy->conf.lfs_file);
8854 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8855 proxy->conf.lfs_line = proxy->conf.args.line;
8856 cur_arg += 1;
8857 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8858 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008859 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008860 /*
8861 * '+ 8' for 'add-acl('
8862 * '- 9' for 'add-acl(' + trailing ')'
8863 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008864 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008865
8866 cur_arg = 1;
8867
8868 if (!*args[cur_arg] ||
8869 (*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 +01008870 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8871 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008872 goto out_err;
8873 }
8874
8875 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008876 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008877 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008878 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008879 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008880 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8881 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008882 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008883 goto out_err;
8884 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008885 free(proxy->conf.lfs_file);
8886 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8887 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008888
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008889 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008890 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8891 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008892 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008893 /*
8894 * '+ 8' for 'del-acl('
8895 * '- 9' for 'del-acl(' + trailing ')'
8896 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008897 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008898
8899 cur_arg = 1;
8900
8901 if (!*args[cur_arg] ||
8902 (*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 +01008903 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8904 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008905 goto out_err;
8906 }
8907
8908 LIST_INIT(&rule->arg.map.key);
8909 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008910 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008911 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008912 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008913 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8914 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008915 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008916 goto out_err;
8917 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008918 free(proxy->conf.lfs_file);
8919 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8920 proxy->conf.lfs_line = proxy->conf.args.line;
8921 cur_arg += 1;
8922 } else if (strncmp(args[0], "del-map", 7) == 0) {
8923 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008924 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008925 /*
8926 * '+ 8' for 'del-map('
8927 * '- 9' for 'del-map(' + trailing ')'
8928 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008929 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008930
8931 cur_arg = 1;
8932
8933 if (!*args[cur_arg] ||
8934 (*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 +01008935 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8936 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008937 goto out_err;
8938 }
8939
8940 LIST_INIT(&rule->arg.map.key);
8941 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008942 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008943 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008944 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008945 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8946 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008947 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008948 goto out_err;
8949 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008950 free(proxy->conf.lfs_file);
8951 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8952 proxy->conf.lfs_line = proxy->conf.args.line;
8953 cur_arg += 1;
8954 } else if (strncmp(args[0], "set-map", 7) == 0) {
8955 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008956 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008957 /*
8958 * '+ 8' for 'set-map('
8959 * '- 9' for 'set-map(' + trailing ')'
8960 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008961 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008962
8963 cur_arg = 1;
8964
8965 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8966 (*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 +01008967 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8968 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008969 goto out_err;
8970 }
8971
8972 LIST_INIT(&rule->arg.map.key);
8973 LIST_INIT(&rule->arg.map.value);
8974
8975 proxy->conf.args.ctx = ARGC_HRS;
8976
8977 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008978 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008979 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008980 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008981 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8982 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008983 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008984 goto out_err;
8985 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008986
8987 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008988 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008989 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 +01008990 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008991 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8992 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008993 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008994 goto out_err;
8995 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008996
8997 free(proxy->conf.lfs_file);
8998 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8999 proxy->conf.lfs_line = proxy->conf.args.line;
9000
9001 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009002 } else if (strcmp(args[0], "redirect") == 0) {
9003 struct redirect_rule *redir;
9004 char *errmsg = NULL;
9005
9006 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009007 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9008 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009009 goto out_err;
9010 }
9011
9012 /* this redirect rule might already contain a parsed condition which
9013 * we'll pass to the http-request rule.
9014 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009015 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009016 rule->arg.redir = redir;
9017 rule->cond = redir->cond;
9018 redir->cond = NULL;
9019 cur_arg = 2;
9020 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009021 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9022 args[0][9] == '\0' && args[0][8] >= '0' &&
9023 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9024 struct sample_expr *expr;
9025 unsigned int where;
9026 char *err = NULL;
9027
9028 cur_arg = 1;
9029 proxy->conf.args.ctx = ARGC_TRK;
9030
9031 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9032 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009033 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9034 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009035 free(err);
9036 goto out_err;
9037 }
9038
9039 where = 0;
9040 if (proxy->cap & PR_CAP_FE)
9041 where |= SMP_VAL_FE_HRS_HDR;
9042 if (proxy->cap & PR_CAP_BE)
9043 where |= SMP_VAL_BE_HRS_HDR;
9044
9045 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009046 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9047 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9048 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9049 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009050 free(expr);
9051 goto out_err;
9052 }
9053
9054 if (strcmp(args[cur_arg], "table") == 0) {
9055 cur_arg++;
9056 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009057 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9058 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009059 free(expr);
9060 goto out_err;
9061 }
9062 /* we copy the table name for now, it will be resolved later */
9063 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9064 cur_arg++;
9065 }
9066 rule->arg.trk_ctr.expr = expr;
9067 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02009068 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009069 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9070 char *errmsg = NULL;
9071 cur_arg = 1;
9072 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009073 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009074 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009075 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009076 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9077 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009078 free(errmsg);
9079 goto out_err;
9080 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009081 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009082 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009083 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9084 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9085 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9086 "%s%s, but got '%s'%s.\n",
9087 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009088 goto out_err;
9089 }
9090
9091 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9092 struct acl_cond *cond;
9093 char *errmsg = NULL;
9094
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009095 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009096 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9097 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009098 free(errmsg);
9099 goto out_err;
9100 }
9101 rule->cond = cond;
9102 }
9103 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009104 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9105 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9106 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009107 goto out_err;
9108 }
9109
9110 return rule;
9111 out_err:
9112 free(rule);
9113 return NULL;
9114}
9115
Willy Tarreau4baae242012-12-27 12:00:31 +01009116/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009117 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009118 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9119 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009120 */
9121struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009122 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009123{
9124 struct redirect_rule *rule;
9125 int cur_arg;
9126 int type = REDIRECT_TYPE_NONE;
9127 int code = 302;
9128 const char *destination = NULL;
9129 const char *cookie = NULL;
9130 int cookie_set = 0;
9131 unsigned int flags = REDIRECT_FLAG_NONE;
9132 struct acl_cond *cond = NULL;
9133
9134 cur_arg = 0;
9135 while (*(args[cur_arg])) {
9136 if (strcmp(args[cur_arg], "location") == 0) {
9137 if (!*args[cur_arg + 1])
9138 goto missing_arg;
9139
9140 type = REDIRECT_TYPE_LOCATION;
9141 cur_arg++;
9142 destination = args[cur_arg];
9143 }
9144 else if (strcmp(args[cur_arg], "prefix") == 0) {
9145 if (!*args[cur_arg + 1])
9146 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009147 type = REDIRECT_TYPE_PREFIX;
9148 cur_arg++;
9149 destination = args[cur_arg];
9150 }
9151 else if (strcmp(args[cur_arg], "scheme") == 0) {
9152 if (!*args[cur_arg + 1])
9153 goto missing_arg;
9154
9155 type = REDIRECT_TYPE_SCHEME;
9156 cur_arg++;
9157 destination = args[cur_arg];
9158 }
9159 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9160 if (!*args[cur_arg + 1])
9161 goto missing_arg;
9162
9163 cur_arg++;
9164 cookie = args[cur_arg];
9165 cookie_set = 1;
9166 }
9167 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9168 if (!*args[cur_arg + 1])
9169 goto missing_arg;
9170
9171 cur_arg++;
9172 cookie = args[cur_arg];
9173 cookie_set = 0;
9174 }
9175 else if (strcmp(args[cur_arg], "code") == 0) {
9176 if (!*args[cur_arg + 1])
9177 goto missing_arg;
9178
9179 cur_arg++;
9180 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009181 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009182 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009183 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009184 args[cur_arg - 1], args[cur_arg]);
9185 return NULL;
9186 }
9187 }
9188 else if (!strcmp(args[cur_arg],"drop-query")) {
9189 flags |= REDIRECT_FLAG_DROP_QS;
9190 }
9191 else if (!strcmp(args[cur_arg],"append-slash")) {
9192 flags |= REDIRECT_FLAG_APPEND_SLASH;
9193 }
9194 else if (strcmp(args[cur_arg], "if") == 0 ||
9195 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009196 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009197 if (!cond) {
9198 memprintf(errmsg, "error in condition: %s", *errmsg);
9199 return NULL;
9200 }
9201 break;
9202 }
9203 else {
9204 memprintf(errmsg,
9205 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9206 args[cur_arg]);
9207 return NULL;
9208 }
9209 cur_arg++;
9210 }
9211
9212 if (type == REDIRECT_TYPE_NONE) {
9213 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9214 return NULL;
9215 }
9216
Willy Tarreaube4653b2015-05-28 15:26:58 +02009217 if (dir && type != REDIRECT_TYPE_LOCATION) {
9218 memprintf(errmsg, "response only supports redirect type 'location'");
9219 return NULL;
9220 }
9221
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009222 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009223 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009224 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009225
9226 if (!use_fmt) {
9227 /* old-style static redirect rule */
9228 rule->rdr_str = strdup(destination);
9229 rule->rdr_len = strlen(destination);
9230 }
9231 else {
9232 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009233
9234 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9235 * if prefix == "/", we don't want to add anything, otherwise it
9236 * makes it hard for the user to configure a self-redirection.
9237 */
Godbachd9722032014-12-18 15:44:58 +08009238 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009239 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009240 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9241 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 +01009242 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9243 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009244 return NULL;
9245 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009246 free(curproxy->conf.lfs_file);
9247 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9248 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009249 }
9250 }
9251
Willy Tarreau4baae242012-12-27 12:00:31 +01009252 if (cookie) {
9253 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9254 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9255 */
9256 rule->cookie_len = strlen(cookie);
9257 if (cookie_set) {
9258 rule->cookie_str = malloc(rule->cookie_len + 10);
9259 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9260 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9261 rule->cookie_len += 9;
9262 } else {
9263 rule->cookie_str = malloc(rule->cookie_len + 21);
9264 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9265 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9266 rule->cookie_len += 20;
9267 }
9268 }
9269 rule->type = type;
9270 rule->code = code;
9271 rule->flags = flags;
9272 LIST_INIT(&rule->list);
9273 return rule;
9274
9275 missing_arg:
9276 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9277 return NULL;
9278}
9279
Willy Tarreau8797c062007-05-07 00:55:35 +02009280/************************************************************************/
9281/* The code below is dedicated to ACL parsing and matching */
9282/************************************************************************/
9283
9284
Willy Tarreau14174bc2012-04-16 14:34:04 +02009285/* This function ensures that the prerequisites for an L7 fetch are ready,
9286 * which means that a request or response is ready. If some data is missing,
9287 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009288 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9289 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009290 *
9291 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009292 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9293 * decide whether or not an HTTP message is present ;
9294 * 0 if the requested data cannot be fetched or if it is certain that
9295 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009296 * 1 if an HTTP message is ready
9297 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009298int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009299 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009300{
Willy Tarreau192252e2015-04-04 01:47:55 +02009301 struct http_txn *txn;
9302 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009303
Willy Tarreaube508f12016-03-10 11:47:01 +01009304 /* Note: it is possible that <s> is NULL when called before stream
9305 * initialization (eg: tcp-request connection), so this function is the
9306 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009307 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009308 if (!s)
9309 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009310
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009311 if (!s->txn) {
9312 if (unlikely(!http_alloc_txn(s)))
9313 return 0; /* not enough memory */
9314 http_init_txn(s);
9315 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009316 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009317 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009318
9319 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009320 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009321
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009322 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009323 /* If the buffer does not leave enough free space at the end,
9324 * we must first realign it.
9325 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009326 if (s->req.buf->p > s->req.buf->data &&
9327 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9328 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009329
Willy Tarreau14174bc2012-04-16 14:34:04 +02009330 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009331 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009332 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009333
9334 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009335 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009336 http_msg_analyzer(msg, &txn->hdr_idx);
9337
9338 /* Still no valid request ? */
9339 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009340 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009341 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009342 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009343 }
9344 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009345 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009346 return 0;
9347 }
9348
9349 /* OK we just got a valid HTTP request. We have some minor
9350 * preparation to perform so that further checks can rely
9351 * on HTTP tests.
9352 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009353
9354 /* If the request was parsed but was too large, we must absolutely
9355 * return an error so that it is not processed. At the moment this
9356 * cannot happen, but if the parsers are to change in the future,
9357 * we want this check to be maintained.
9358 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009359 if (unlikely(s->req.buf->i + s->req.buf->p >
9360 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009361 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009362 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009363 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009364 return 1;
9365 }
9366
Willy Tarreau9b28e032012-10-12 23:49:43 +02009367 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009368 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009369 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009370
Willy Tarreau506d0502013-07-06 13:29:24 +02009371 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9372 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009373 }
9374
Willy Tarreau506d0502013-07-06 13:29:24 +02009375 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009376 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009377 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009378
9379 /* otherwise everything's ready for the request */
9380 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009381 else {
9382 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009383 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9384 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009385 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009386 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009387 }
9388
9389 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009390 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009391 return 1;
9392}
Willy Tarreau8797c062007-05-07 00:55:35 +02009393
Willy Tarreau8797c062007-05-07 00:55:35 +02009394/* 1. Check on METHOD
9395 * We use the pre-parsed method if it is known, and store its number as an
9396 * integer. If it is unknown, we use the pointer and the length.
9397 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009398static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009399{
9400 int len, meth;
9401
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009402 len = strlen(text);
9403 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009404
9405 pattern->val.i = meth;
9406 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009407 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009408 pattern->len = len;
9409 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009410 else {
9411 pattern->ptr.str = NULL;
9412 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009413 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009414 return 1;
9415}
9416
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009417/* This function fetches the method of current HTTP request and stores
9418 * it in the global pattern struct as a chunk. There are two possibilities :
9419 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9420 * in <len> and <ptr> is NULL ;
9421 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9422 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009423 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009424 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009425static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009426smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009427{
9428 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009429 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009430
Willy Tarreau24e32d82012-04-23 23:55:44 +02009431 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009432
Willy Tarreaube508f12016-03-10 11:47:01 +01009433 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009434 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009435 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009436 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009437 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009438 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9439 /* ensure the indexes are not affected */
9440 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009441 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009442 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9443 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009444 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009445 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009446 return 1;
9447}
9448
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009449/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009450static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009451{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009452 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009453 struct pattern_list *lst;
9454 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009455
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009456 list_for_each_entry(lst, &expr->patterns, list) {
9457 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009458
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009459 /* well-known method */
9460 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009461 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009462 return pattern;
9463 else
9464 continue;
9465 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009466
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009467 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009468 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009469 continue;
9470
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009471 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009472 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9473 (!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 +01009474 return pattern;
9475 }
9476 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009477}
9478
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009479static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009480smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009481{
Willy Tarreaube508f12016-03-10 11:47:01 +01009482 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009483 char *ptr;
9484 int len;
9485
Willy Tarreauc0239e02012-04-16 14:42:55 +02009486 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009487
Willy Tarreaube508f12016-03-10 11:47:01 +01009488 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009489 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009490 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009491
9492 while ((len-- > 0) && (*ptr++ != '/'));
9493 if (len <= 0)
9494 return 0;
9495
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009496 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009497 smp->data.u.str.str = ptr;
9498 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009499
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009500 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009501 return 1;
9502}
9503
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009504static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009505smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009506{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009507 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009508 char *ptr;
9509 int len;
9510
Willy Tarreauc0239e02012-04-16 14:42:55 +02009511 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009512
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009513 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009514 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9515 return 0;
9516
Willy Tarreau8797c062007-05-07 00:55:35 +02009517 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009518 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009519
9520 while ((len-- > 0) && (*ptr++ != '/'));
9521 if (len <= 0)
9522 return 0;
9523
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009524 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009525 smp->data.u.str.str = ptr;
9526 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009527
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009528 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009529 return 1;
9530}
9531
9532/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009533static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009534smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009535{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009536 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009537 char *ptr;
9538 int len;
9539
Willy Tarreauc0239e02012-04-16 14:42:55 +02009540 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009541
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009542 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009543 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9544 return 0;
9545
Willy Tarreau8797c062007-05-07 00:55:35 +02009546 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009547 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009548
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009549 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009550 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009551 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009552 return 1;
9553}
9554
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009555static int
9556smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9557{
9558 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9559 return 0;
9560
9561 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009562 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009563 return 0;
9564 smp->strm->unique_id[0] = '\0';
9565 }
9566 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9567 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9568
9569 smp->data.type = SMP_T_STR;
9570 smp->data.u.str.str = smp->strm->unique_id;
9571 smp->flags = SMP_F_CONST;
9572 return 1;
9573}
9574
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009575/* Returns a string block containing all headers including the
9576 * empty line wich separes headers from the body. This is useful
9577 * form some headers analysis.
9578 */
9579static int
9580smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9581{
9582 struct http_msg *msg;
9583 struct hdr_idx *idx;
9584 struct http_txn *txn;
9585
9586 CHECK_HTTP_MESSAGE_FIRST();
9587
9588 txn = smp->strm->txn;
9589 idx = &txn->hdr_idx;
9590 msg = &txn->req;
9591
9592 smp->data.type = SMP_T_STR;
9593 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9594 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9595 (msg->chn->buf->p[msg->eoh] == '\r');
9596
9597 return 1;
9598}
9599
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009600/* Returns the header request in a length/value encoded format.
9601 * This is useful for exchanges with the SPOE.
9602 *
9603 * A "length value" is a multibyte code encoding numbers. It uses the
9604 * SPOE format. The encoding is the following:
9605 *
9606 * Each couple "header name" / "header value" is composed
9607 * like this:
9608 * "length value" "header name bytes"
9609 * "length value" "header value bytes"
9610 * When the last header is reached, the header name and the header
9611 * value are empty. Their length are 0
9612 */
9613static int
9614smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9615{
9616 struct http_msg *msg;
9617 struct chunk *temp;
9618 struct hdr_idx *idx;
9619 const char *cur_ptr, *cur_next, *p;
9620 int old_idx, cur_idx;
9621 struct hdr_idx_elem *cur_hdr;
9622 const char *hn, *hv;
9623 int hnl, hvl;
9624 int ret;
9625 struct http_txn *txn;
9626 char *buf;
9627 char *end;
9628
9629 CHECK_HTTP_MESSAGE_FIRST();
9630
9631 temp = get_trash_chunk();
9632 buf = temp->str;
9633 end = temp->str + temp->size;
9634
9635 txn = smp->strm->txn;
9636 idx = &txn->hdr_idx;
9637 msg = &txn->req;
9638
9639 /* Build array of headers. */
9640 old_idx = 0;
9641 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9642 while (1) {
9643 cur_idx = idx->v[old_idx].next;
9644 if (!cur_idx)
9645 break;
9646 old_idx = cur_idx;
9647
9648 cur_hdr = &idx->v[cur_idx];
9649 cur_ptr = cur_next;
9650 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9651
9652 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9653 * and the next header starts at cur_next. We'll check
9654 * this header in the list as well as against the default
9655 * rule.
9656 */
9657
9658 /* look for ': *'. */
9659 hn = cur_ptr;
9660 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9661 if (p >= cur_ptr+cur_hdr->len)
9662 continue;
9663 hnl = p - hn;
9664 p++;
9665 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9666 p++;
9667 if (p >= cur_ptr + cur_hdr->len)
9668 continue;
9669 hv = p;
9670 hvl = cur_ptr + cur_hdr->len-p;
9671
9672 /* encode the header name. */
9673 ret = encode_varint(hnl, &buf, end);
9674 if (ret == -1)
9675 return 0;
9676 if (buf + hnl > end)
9677 return 0;
9678 memcpy(buf, hn, hnl);
9679 buf += hnl;
9680
9681 /* encode and copy the value. */
9682 ret = encode_varint(hvl, &buf, end);
9683 if (ret == -1)
9684 return 0;
9685 if (buf + hvl > end)
9686 return 0;
9687 memcpy(buf, hv, hvl);
9688 buf += hvl;
9689 }
9690
9691 /* encode the end of the header list with empty
9692 * header name and header value.
9693 */
9694 ret = encode_varint(0, &buf, end);
9695 if (ret == -1)
9696 return 0;
9697 ret = encode_varint(0, &buf, end);
9698 if (ret == -1)
9699 return 0;
9700
9701 /* Initialise sample data which will be filled. */
9702 smp->data.type = SMP_T_BIN;
9703 smp->data.u.str.str = temp->str;
9704 smp->data.u.str.len = buf - temp->str;
9705 smp->data.u.str.size = temp->size;
9706
9707 return 1;
9708}
9709
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009710/* returns the longest available part of the body. This requires that the body
9711 * has been waited for using http-buffer-request.
9712 */
9713static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009714smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009715{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009716 struct http_msg *msg;
9717 unsigned long len;
9718 unsigned long block1;
9719 char *body;
9720 struct chunk *temp;
9721
9722 CHECK_HTTP_MESSAGE_FIRST();
9723
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009724 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009725 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009726 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009727 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009728
9729 len = http_body_bytes(msg);
9730 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9731
9732 block1 = len;
9733 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9734 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9735
9736 if (block1 == len) {
9737 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009738 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009739 smp->data.u.str.str = body;
9740 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009741 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9742 }
9743 else {
9744 /* buffer is wrapped, we need to defragment it */
9745 temp = get_trash_chunk();
9746 memcpy(temp->str, body, block1);
9747 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009748 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009749 smp->data.u.str.str = temp->str;
9750 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009751 smp->flags = SMP_F_VOL_TEST;
9752 }
9753 return 1;
9754}
9755
9756
9757/* returns the available length of the body. This requires that the body
9758 * has been waited for using http-buffer-request.
9759 */
9760static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009761smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009762{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009763 struct http_msg *msg;
9764
9765 CHECK_HTTP_MESSAGE_FIRST();
9766
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009767 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009768 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009769 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009770 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009771
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009772 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009773 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009774
9775 smp->flags = SMP_F_VOL_TEST;
9776 return 1;
9777}
9778
9779
9780/* returns the advertised length of the body, or the advertised size of the
9781 * chunks available in the buffer. This requires that the body has been waited
9782 * for using http-buffer-request.
9783 */
9784static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009785smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009786{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009787 struct http_msg *msg;
9788
9789 CHECK_HTTP_MESSAGE_FIRST();
9790
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009791 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009792 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009793 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009794 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009795
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009796 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009797 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009798
9799 smp->flags = SMP_F_VOL_TEST;
9800 return 1;
9801}
9802
9803
Willy Tarreau8797c062007-05-07 00:55:35 +02009804/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009805static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009806smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009807{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009808 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009809
Willy Tarreauc0239e02012-04-16 14:42:55 +02009810 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009811
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009812 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009813 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009814 smp->data.u.str.len = txn->req.sl.rq.u_l;
9815 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009816 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009817 return 1;
9818}
9819
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009820static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009821smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009822{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009823 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009824 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009825
Willy Tarreauc0239e02012-04-16 14:42:55 +02009826 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009827
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009828 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009829 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 +02009830 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009831 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009832
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009833 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009834 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009835 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009836 return 1;
9837}
9838
9839static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009840smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009841{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009842 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009843 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009844
Willy Tarreauc0239e02012-04-16 14:42:55 +02009845 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009846
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009847 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009848 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 +02009849 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9850 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009851
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009852 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009853 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009854 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009855 return 1;
9856}
9857
Willy Tarreau185b5c42012-04-26 15:11:51 +02009858/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9859 * Accepts an optional argument of type string containing the header field name,
9860 * and an optional argument of type signed or unsigned integer to request an
9861 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009862 * headers are considered from the first one. It does not stop on commas and
9863 * returns full lines instead (useful for User-Agent or Date for example).
9864 */
9865static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009866smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009867{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009868 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009869 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009870 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009871 int occ = 0;
9872 const char *name_str = NULL;
9873 int name_len = 0;
9874
9875 if (!ctx) {
9876 /* first call */
9877 ctx = &static_hdr_ctx;
9878 ctx->idx = 0;
9879 smp->ctx.a[0] = ctx;
9880 }
9881
9882 if (args) {
9883 if (args[0].type != ARGT_STR)
9884 return 0;
9885 name_str = args[0].data.str.str;
9886 name_len = args[0].data.str.len;
9887
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009888 if (args[1].type == ARGT_SINT)
9889 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009890 }
9891
9892 CHECK_HTTP_MESSAGE_FIRST();
9893
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009894 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009895 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 +02009896
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009897 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9898 /* search for header from the beginning */
9899 ctx->idx = 0;
9900
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009901 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009902 /* no explicit occurrence and single fetch => last header by default */
9903 occ = -1;
9904
9905 if (!occ)
9906 /* prepare to report multiple occurrences for ACL fetches */
9907 smp->flags |= SMP_F_NOT_LAST;
9908
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009909 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009910 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009911 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 +02009912 return 1;
9913
9914 smp->flags &= ~SMP_F_NOT_LAST;
9915 return 0;
9916}
9917
9918/* 6. Check on HTTP header count. The number of occurrences is returned.
9919 * Accepts exactly 1 argument of type string. It does not stop on commas and
9920 * returns full lines instead (useful for User-Agent or Date for example).
9921 */
9922static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009923smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009924{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009925 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009926 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009927 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009928 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009929 const char *name = NULL;
9930 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009931
Willy Tarreau601a4d12015-04-01 19:16:09 +02009932 if (args && args->type == ARGT_STR) {
9933 name = args->data.str.str;
9934 len = args->data.str.len;
9935 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009936
9937 CHECK_HTTP_MESSAGE_FIRST();
9938
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009939 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009940 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 +02009941
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009942 ctx.idx = 0;
9943 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009944 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009945 cnt++;
9946
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009947 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009948 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009949 smp->flags = SMP_F_VOL_HDR;
9950 return 1;
9951}
9952
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009953static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009954smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009955{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009956 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009957 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009958 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009959 struct chunk *temp;
9960 char del = ',';
9961
9962 if (args && args->type == ARGT_STR)
9963 del = *args[0].data.str.str;
9964
9965 CHECK_HTTP_MESSAGE_FIRST();
9966
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009967 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009968 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 +02009969
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009970 temp = get_trash_chunk();
9971
9972 ctx.idx = 0;
9973 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9974 if (temp->len)
9975 temp->str[temp->len++] = del;
9976 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9977 temp->len += ctx.del;
9978 }
9979
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009980 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009981 smp->data.u.str.str = temp->str;
9982 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009983 smp->flags = SMP_F_VOL_HDR;
9984 return 1;
9985}
9986
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009987/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9988 * Accepts an optional argument of type string containing the header field name,
9989 * and an optional argument of type signed or unsigned integer to request an
9990 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009991 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009992 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009993static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009994smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009995{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009996 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009997 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009998 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009999 int occ = 0;
10000 const char *name_str = NULL;
10001 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010002
Willy Tarreaua890d072013-04-02 12:01:06 +020010003 if (!ctx) {
10004 /* first call */
10005 ctx = &static_hdr_ctx;
10006 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010007 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010008 }
10009
Willy Tarreau185b5c42012-04-26 15:11:51 +020010010 if (args) {
10011 if (args[0].type != ARGT_STR)
10012 return 0;
10013 name_str = args[0].data.str.str;
10014 name_len = args[0].data.str.len;
10015
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010016 if (args[1].type == ARGT_SINT)
10017 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010018 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010019
Willy Tarreaue333ec92012-04-16 16:26:40 +020010020 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010021
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010022 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010023 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 +020010024
Willy Tarreau185b5c42012-04-26 15:11:51 +020010025 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010026 /* search for header from the beginning */
10027 ctx->idx = 0;
10028
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010029 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010030 /* no explicit occurrence and single fetch => last header by default */
10031 occ = -1;
10032
10033 if (!occ)
10034 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010035 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010036
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010037 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010038 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010039 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 +020010040 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010041
Willy Tarreau37406352012-04-23 16:16:37 +020010042 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010043 return 0;
10044}
10045
Willy Tarreauc11416f2007-06-17 16:58:38 +020010046/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010047 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010048 */
10049static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010050smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010051{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010052 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010053 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010054 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010055 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010056 const char *name = NULL;
10057 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010058
Willy Tarreau601a4d12015-04-01 19:16:09 +020010059 if (args && args->type == ARGT_STR) {
10060 name = args->data.str.str;
10061 len = args->data.str.len;
10062 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010063
Willy Tarreaue333ec92012-04-16 16:26:40 +020010064 CHECK_HTTP_MESSAGE_FIRST();
10065
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010066 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010067 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 +020010068
Willy Tarreau33a7e692007-06-10 19:45:56 +020010069 ctx.idx = 0;
10070 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010071 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010072 cnt++;
10073
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010074 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010075 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010076 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010077 return 1;
10078}
10079
Willy Tarreau185b5c42012-04-26 15:11:51 +020010080/* Fetch an HTTP header's integer value. The integer value is returned. It
10081 * takes a mandatory argument of type string and an optional one of type int
10082 * to designate a specific occurrence. It returns an unsigned integer, which
10083 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010084 */
10085static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010086smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010087{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010088 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010089
Willy Tarreauf853c462012-04-23 18:53:56 +020010090 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010091 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010092 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010093 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010094
Willy Tarreaud53e2422012-04-16 17:21:11 +020010095 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010096}
10097
Cyril Bonté69fa9922012-10-25 00:01:06 +020010098/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10099 * and an optional one of type int to designate a specific occurrence.
10100 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010101 */
10102static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010103smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010104{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010105 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010106
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010107 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010108 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010109 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010110 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010111 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010112 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010113 if (smp->data.u.str.len < temp->size - 1) {
10114 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10115 temp->str[smp->data.u.str.len] = '\0';
10116 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010117 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010118 break;
10119 }
10120 }
10121 }
10122
Willy Tarreaud53e2422012-04-16 17:21:11 +020010123 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010124 if (!(smp->flags & SMP_F_NOT_LAST))
10125 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010126 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010127 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010128}
10129
Willy Tarreau737b0c12007-06-10 21:28:46 +020010130/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10131 * the first '/' after the possible hostname, and ends before the possible '?'.
10132 */
10133static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010134smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010135{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010136 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010137 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010138
Willy Tarreauc0239e02012-04-16 14:42:55 +020010139 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010140
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010141 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010142 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010143 ptr = http_get_path(txn);
10144 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010145 return 0;
10146
10147 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010148 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010149 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010150
10151 while (ptr < end && *ptr != '?')
10152 ptr++;
10153
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010154 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010155 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010156 return 1;
10157}
10158
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010159/* This produces a concatenation of the first occurrence of the Host header
10160 * followed by the path component if it begins with a slash ('/'). This means
10161 * that '*' will not be added, resulting in exactly the first Host entry.
10162 * If no Host header is found, then the path is returned as-is. The returned
10163 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010164 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010165 */
10166static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010167smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010168{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010169 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010170 char *ptr, *end, *beg;
10171 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010172 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010173
10174 CHECK_HTTP_MESSAGE_FIRST();
10175
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010176 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010177 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010178 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010179 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010180
10181 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010182 temp = get_trash_chunk();
10183 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010184 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010185 smp->data.u.str.str = temp->str;
10186 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010187
10188 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010189 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010190 beg = http_get_path(txn);
10191 if (!beg)
10192 beg = end;
10193
10194 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10195
10196 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010197 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10198 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010199 }
10200
10201 smp->flags = SMP_F_VOL_1ST;
10202 return 1;
10203}
10204
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010205/* This produces a 32-bit hash of the concatenation of the first occurrence of
10206 * the Host header followed by the path component if it begins with a slash ('/').
10207 * This means that '*' will not be added, resulting in exactly the first Host
10208 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010209 * is hashed using the path hash followed by a full avalanche hash and provides a
10210 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010211 * high-traffic sites without having to store whole paths.
10212 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010213int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010214smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010215{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010216 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010217 struct hdr_ctx ctx;
10218 unsigned int hash = 0;
10219 char *ptr, *beg, *end;
10220 int len;
10221
10222 CHECK_HTTP_MESSAGE_FIRST();
10223
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010224 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010225 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010226 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010227 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10228 ptr = ctx.line + ctx.val;
10229 len = ctx.vlen;
10230 while (len--)
10231 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10232 }
10233
10234 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010235 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010236 beg = http_get_path(txn);
10237 if (!beg)
10238 beg = end;
10239
10240 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10241
10242 if (beg < ptr && *beg == '/') {
10243 while (beg < ptr)
10244 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10245 }
10246 hash = full_hash(hash);
10247
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010248 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010249 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010250 smp->flags = SMP_F_VOL_1ST;
10251 return 1;
10252}
10253
Willy Tarreau4a550602012-12-09 14:53:32 +010010254/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010255 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10256 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10257 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010258 * that in environments where IPv6 is insignificant, truncating the output to
10259 * 8 bytes would still work.
10260 */
10261static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010262smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010263{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010264 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010265 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010266
10267 if (!cli_conn)
10268 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010269
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010270 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010271 return 0;
10272
Willy Tarreau47ca5452012-12-23 20:22:19 +010010273 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010274 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010275 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010276
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010277 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010278 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010279 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010280 temp->len += 4;
10281 break;
10282 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010283 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010284 temp->len += 16;
10285 break;
10286 default:
10287 return 0;
10288 }
10289
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010290 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010291 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010292 return 1;
10293}
10294
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010295/* Extracts the query string, which comes after the question mark '?'. If no
10296 * question mark is found, nothing is returned. Otherwise it returns a sample
10297 * of type string carrying the whole query string.
10298 */
10299static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010300smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010301{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010302 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010303 char *ptr, *end;
10304
10305 CHECK_HTTP_MESSAGE_FIRST();
10306
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010307 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010308 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10309 end = ptr + txn->req.sl.rq.u_l;
10310
10311 /* look up the '?' */
10312 do {
10313 if (ptr == end)
10314 return 0;
10315 } while (*ptr++ != '?');
10316
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010317 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010318 smp->data.u.str.str = ptr;
10319 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010320 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10321 return 1;
10322}
10323
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010325smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010326{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010327 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10328 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10329 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010330
Willy Tarreau24e32d82012-04-23 23:55:44 +020010331 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +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 = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010335 return 1;
10336}
10337
Willy Tarreau7f18e522010-10-22 20:04:13 +020010338/* return a valid test if the current request is the first one on the connection */
10339static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010340smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010341{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010342 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010343 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010344 return 1;
10345}
10346
Willy Tarreau34db1082012-04-19 17:16:54 +020010347/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010348static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010349smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010350{
10351
Willy Tarreau24e32d82012-04-23 23:55:44 +020010352 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010353 return 0;
10354
Willy Tarreauc0239e02012-04-16 14:42:55 +020010355 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010356
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010357 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010358 return 0;
10359
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010360 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010361 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010362 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010363 return 1;
10364}
Willy Tarreau8797c062007-05-07 00:55:35 +020010365
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010366/* Accepts exactly 1 argument of type userlist */
10367static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010368smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010369{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010370 if (!args || args->type != ARGT_USR)
10371 return 0;
10372
10373 CHECK_HTTP_MESSAGE_FIRST();
10374
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010375 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010376 return 0;
10377
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010378 /* if the user does not belong to the userlist or has a wrong password,
10379 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010380 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010381 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010382 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10383 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010384 return 0;
10385
10386 /* pat_match_auth() will need the user list */
10387 smp->ctx.a[0] = args->data.usr;
10388
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010389 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010390 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010391 smp->data.u.str.str = smp->strm->txn->auth.user;
10392 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010393
10394 return 1;
10395}
10396
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010397/* Try to find the next occurrence of a cookie name in a cookie header value.
10398 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10399 * the cookie value is returned into *value and *value_l, and the function
10400 * returns a pointer to the next pointer to search from if the value was found.
10401 * Otherwise if the cookie was not found, NULL is returned and neither value
10402 * nor value_l are touched. The input <hdr> string should first point to the
10403 * header's value, and the <hdr_end> pointer must point to the first character
10404 * not part of the value. <list> must be non-zero if value may represent a list
10405 * of values (cookie headers). This makes it faster to abort parsing when no
10406 * list is expected.
10407 */
David Carlier4686f792015-09-25 14:10:50 +010010408char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010409extract_cookie_value(char *hdr, const char *hdr_end,
10410 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010411 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010412{
10413 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10414 char *next;
10415
10416 /* we search at least a cookie name followed by an equal, and more
10417 * generally something like this :
10418 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10419 */
10420 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10421 /* Iterate through all cookies on this line */
10422
Willy Tarreau2235b262016-11-05 15:50:20 +010010423 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010424 att_beg++;
10425
10426 /* find att_end : this is the first character after the last non
10427 * space before the equal. It may be equal to hdr_end.
10428 */
10429 equal = att_end = att_beg;
10430
10431 while (equal < hdr_end) {
10432 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10433 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010434 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010435 continue;
10436 att_end = equal;
10437 }
10438
10439 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10440 * is between <att_beg> and <equal>, both may be identical.
10441 */
10442
10443 /* look for end of cookie if there is an equal sign */
10444 if (equal < hdr_end && *equal == '=') {
10445 /* look for the beginning of the value */
10446 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010447 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010448 val_beg++;
10449
10450 /* find the end of the value, respecting quotes */
10451 next = find_cookie_value_end(val_beg, hdr_end);
10452
10453 /* make val_end point to the first white space or delimitor after the value */
10454 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010455 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010456 val_end--;
10457 } else {
10458 val_beg = val_end = next = equal;
10459 }
10460
10461 /* We have nothing to do with attributes beginning with '$'. However,
10462 * they will automatically be removed if a header before them is removed,
10463 * since they're supposed to be linked together.
10464 */
10465 if (*att_beg == '$')
10466 continue;
10467
10468 /* Ignore cookies with no equal sign */
10469 if (equal == next)
10470 continue;
10471
10472 /* Now we have the cookie name between att_beg and att_end, and
10473 * its value between val_beg and val_end.
10474 */
10475
10476 if (att_end - att_beg == cookie_name_l &&
10477 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10478 /* let's return this value and indicate where to go on from */
10479 *value = val_beg;
10480 *value_l = val_end - val_beg;
10481 return next + 1;
10482 }
10483
10484 /* Set-Cookie headers only have the name in the first attr=value part */
10485 if (!list)
10486 break;
10487 }
10488
10489 return NULL;
10490}
10491
William Lallemanda43ba4e2014-01-28 18:14:25 +010010492/* Fetch a captured HTTP request header. The index is the position of
10493 * the "capture" option in the configuration file
10494 */
10495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010496smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010497{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010498 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010499 int idx;
10500
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010501 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010502 return 0;
10503
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010504 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010505
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010506 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 +010010507 return 0;
10508
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010509 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010510 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010511 smp->data.u.str.str = smp->strm->req_cap[idx];
10512 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010513
10514 return 1;
10515}
10516
10517/* Fetch a captured HTTP response header. The index is the position of
10518 * the "capture" option in the configuration file
10519 */
10520static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010521smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010522{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010523 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010524 int idx;
10525
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010526 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010527 return 0;
10528
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010529 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010530
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010531 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 +010010532 return 0;
10533
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010534 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010535 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010536 smp->data.u.str.str = smp->strm->res_cap[idx];
10537 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010538
10539 return 1;
10540}
10541
William Lallemand65ad6e12014-01-31 15:08:02 +010010542/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10543static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010544smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010545{
10546 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010547 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010548 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010549
Willy Tarreau15e91e12015-04-04 00:52:09 +020010550 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010551 return 0;
10552
William Lallemand96a77852014-02-05 00:30:02 +010010553 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010554
William Lallemand96a77852014-02-05 00:30:02 +010010555 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10556 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010557
William Lallemand96a77852014-02-05 00:30:02 +010010558 temp = get_trash_chunk();
10559 temp->str = txn->uri;
10560 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010561 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010562 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010563 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010564
10565 return 1;
10566
10567}
10568
10569/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10570static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010571smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010572{
10573 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010574 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010575 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010576
Willy Tarreau15e91e12015-04-04 00:52:09 +020010577 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010578 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010579
William Lallemand65ad6e12014-01-31 15:08:02 +010010580 ptr = txn->uri;
10581
10582 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10583 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010584
William Lallemand65ad6e12014-01-31 15:08:02 +010010585 if (!*ptr)
10586 return 0;
10587
10588 ptr++; /* skip the space */
10589
10590 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010591 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010592 if (!ptr)
10593 return 0;
10594 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10595 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010596
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010597 smp->data.u.str = *temp;
10598 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010599 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010600 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010601
10602 return 1;
10603}
10604
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010605/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10606 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10607 */
10608static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010609smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010610{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010611 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010612
Willy Tarreau15e91e12015-04-04 00:52:09 +020010613 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010614 return 0;
10615
10616 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010617 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010618 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010619 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010620
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010621 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010622 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010623 smp->flags = SMP_F_CONST;
10624 return 1;
10625
10626}
10627
10628/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10629 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10630 */
10631static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010632smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010633{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010634 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010635
Willy Tarreau15e91e12015-04-04 00:52:09 +020010636 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010637 return 0;
10638
10639 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010640 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010641 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010642 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010643
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010644 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010645 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010646 smp->flags = SMP_F_CONST;
10647 return 1;
10648
10649}
10650
William Lallemand65ad6e12014-01-31 15:08:02 +010010651
Willy Tarreaue333ec92012-04-16 16:26:40 +020010652/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010653 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010654 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010655 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010656 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010657 * Accepts exactly 1 argument of type string. If the input options indicate
10658 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010659 * The returned sample is of type CSTR. Can be used to parse cookies in other
10660 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010661 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010663{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010664 struct http_txn *txn;
10665 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010666 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010667 const struct http_msg *msg;
10668 const char *hdr_name;
10669 int hdr_name_len;
10670 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010671 int occ = 0;
10672 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010673
Willy Tarreau24e32d82012-04-23 23:55:44 +020010674 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010675 return 0;
10676
Willy Tarreaua890d072013-04-02 12:01:06 +020010677 if (!ctx) {
10678 /* first call */
10679 ctx = &static_hdr_ctx;
10680 ctx->idx = 0;
10681 smp->ctx.a[2] = ctx;
10682 }
10683
Willy Tarreaue333ec92012-04-16 16:26:40 +020010684 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010685
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010686 txn = smp->strm->txn;
10687 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010688
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010689 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010690 msg = &txn->req;
10691 hdr_name = "Cookie";
10692 hdr_name_len = 6;
10693 } else {
10694 msg = &txn->rsp;
10695 hdr_name = "Set-Cookie";
10696 hdr_name_len = 10;
10697 }
10698
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010699 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010700 /* no explicit occurrence and single fetch => last cookie by default */
10701 occ = -1;
10702
10703 /* OK so basically here, either we want only one value and it's the
10704 * last one, or we want to iterate over all of them and we fetch the
10705 * next one.
10706 */
10707
Willy Tarreau9b28e032012-10-12 23:49:43 +020010708 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010709 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010710 /* search for the header from the beginning, we must first initialize
10711 * the search parameters.
10712 */
Willy Tarreau37406352012-04-23 16:16:37 +020010713 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010714 ctx->idx = 0;
10715 }
10716
Willy Tarreau28376d62012-04-26 21:26:10 +020010717 smp->flags |= SMP_F_VOL_HDR;
10718
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010719 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010720 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10721 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010722 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10723 goto out;
10724
Willy Tarreau24e32d82012-04-23 23:55:44 +020010725 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010726 continue;
10727
Willy Tarreau37406352012-04-23 16:16:37 +020010728 smp->ctx.a[0] = ctx->line + ctx->val;
10729 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010730 }
10731
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010732 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010733 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010734 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010735 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010736 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010737 &smp->data.u.str.str,
10738 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010739 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010740 found = 1;
10741 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010742 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010743 smp->flags |= SMP_F_NOT_LAST;
10744 return 1;
10745 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010746 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010747 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010748 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010749 /* all cookie headers and values were scanned. If we're looking for the
10750 * last occurrence, we may return it now.
10751 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010752 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010753 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010754 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010755}
10756
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010757/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010758 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010759 * multiple cookies may be parsed on the same line. The returned sample is of
10760 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010761 */
10762static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010763smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010764{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010765 struct http_txn *txn;
10766 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010767 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010768 const struct http_msg *msg;
10769 const char *hdr_name;
10770 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010771 int cnt;
10772 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010773 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010774
Willy Tarreau24e32d82012-04-23 23:55:44 +020010775 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010776 return 0;
10777
Willy Tarreaue333ec92012-04-16 16:26:40 +020010778 CHECK_HTTP_MESSAGE_FIRST();
10779
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010780 txn = smp->strm->txn;
10781 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010782
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010783 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010784 msg = &txn->req;
10785 hdr_name = "Cookie";
10786 hdr_name_len = 6;
10787 } else {
10788 msg = &txn->rsp;
10789 hdr_name = "Set-Cookie";
10790 hdr_name_len = 10;
10791 }
10792
Willy Tarreau9b28e032012-10-12 23:49:43 +020010793 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010794 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010795 ctx.idx = 0;
10796 cnt = 0;
10797
10798 while (1) {
10799 /* Note: val_beg == NULL every time we need to fetch a new header */
10800 if (!val_beg) {
10801 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10802 break;
10803
Willy Tarreau24e32d82012-04-23 23:55:44 +020010804 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010805 continue;
10806
10807 val_beg = ctx.line + ctx.val;
10808 val_end = val_beg + ctx.vlen;
10809 }
10810
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010811 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010812 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010813 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010814 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010815 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010816 &smp->data.u.str.str,
10817 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010818 cnt++;
10819 }
10820 }
10821
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010822 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010823 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010824 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010825 return 1;
10826}
10827
Willy Tarreau51539362012-05-08 12:46:28 +020010828/* Fetch an cookie's integer value. The integer value is returned. It
10829 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10830 */
10831static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010832smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010833{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010834 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010835
10836 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010837 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010838 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010839 }
10840
10841 return ret;
10842}
10843
Willy Tarreau8797c062007-05-07 00:55:35 +020010844/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010845/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010846/************************************************************************/
10847
David Cournapeau16023ee2010-12-23 20:55:41 +090010848/*
10849 * Given a path string and its length, find the position of beginning of the
10850 * query string. Returns NULL if no query string is found in the path.
10851 *
10852 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10853 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010854 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010855 */
bedis4c75cca2012-10-05 08:38:24 +020010856static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010857{
10858 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010859
bedis4c75cca2012-10-05 08:38:24 +020010860 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010861 return p ? p + 1 : NULL;
10862}
10863
bedis4c75cca2012-10-05 08:38:24 +020010864static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010865{
bedis4c75cca2012-10-05 08:38:24 +020010866 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010867}
10868
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010869/* after increasing a pointer value, it can exceed the first buffer
10870 * size. This function transform the value of <ptr> according with
10871 * the expected position. <chunks> is an array of the one or two
10872 * avalaible chunks. The first value is the start of the first chunk,
10873 * the second value if the end+1 of the first chunks. The third value
10874 * is NULL or the start of the second chunk and the fourth value is
10875 * the end+1 of the second chunk. The function returns 1 if does a
10876 * wrap, else returns 0.
10877 */
10878static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10879{
10880 if (*ptr < chunks[1])
10881 return 0;
10882 if (!chunks[2])
10883 return 0;
10884 *ptr = chunks[2] + ( *ptr - chunks[1] );
10885 return 1;
10886}
10887
David Cournapeau16023ee2010-12-23 20:55:41 +090010888/*
10889 * Given a url parameter, find the starting position of the first occurence,
10890 * or NULL if the parameter is not found.
10891 *
10892 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10893 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010894 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010895 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010896 * or the second chunk. The caller must be check the position before using the
10897 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010898 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010899static const char *
10900find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010901 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010902 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010903{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010904 const char *pos, *last, *equal;
10905 const char **bufs = chunks;
10906 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010907
David Cournapeau16023ee2010-12-23 20:55:41 +090010908
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010909 pos = bufs[0];
10910 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010911 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010912 /* Check the equal. */
10913 equal = pos + url_param_name_l;
10914 if (fix_pointer_if_wrap(chunks, &equal)) {
10915 if (equal >= chunks[3])
10916 return NULL;
10917 } else {
10918 if (equal >= chunks[1])
10919 return NULL;
10920 }
10921 if (*equal == '=') {
10922 if (pos + url_param_name_l > last) {
10923 /* process wrap case, we detect a wrap. In this case, the
10924 * comparison is performed in two parts.
10925 */
10926
10927 /* This is the end, we dont have any other chunk. */
10928 if (bufs != chunks || !bufs[2])
10929 return NULL;
10930
10931 /* Compute the length of each part of the comparison. */
10932 l1 = last - pos;
10933 l2 = url_param_name_l - l1;
10934
10935 /* The second buffer is too short to contain the compared string. */
10936 if (bufs[2] + l2 > bufs[3])
10937 return NULL;
10938
10939 if (memcmp(pos, url_param_name, l1) == 0 &&
10940 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10941 return pos;
10942
10943 /* Perform wrapping and jump the string who fail the comparison. */
10944 bufs += 2;
10945 pos = bufs[0] + l2;
10946 last = bufs[1];
10947
10948 } else {
10949 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010950 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10951 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010952 pos += url_param_name_l + 1;
10953 if (fix_pointer_if_wrap(chunks, &pos))
10954 last = bufs[2];
10955 }
10956 }
10957
10958 while (1) {
10959 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010960 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010961 pos++;
10962 if (pos < last)
10963 break;
10964 /* process buffer wrapping. */
10965 if (bufs != chunks || !bufs[2])
10966 return NULL;
10967 bufs += 2;
10968 pos = bufs[0];
10969 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010970 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010971 pos++;
10972 }
10973 return NULL;
10974}
10975
10976/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010977 * Given a url parameter name and a query string, find the next value.
10978 * An empty url_param_name matches the first available parameter.
10979 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10980 * respectively provide a pointer to the value and its end.
10981 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010982 */
10983static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010984find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010985 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010986 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010987{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010988 const char *arg_start, *qs_end;
10989 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010990
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010991 arg_start = chunks[0];
10992 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010993 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010994 /* Looks for an argument name. */
10995 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010996 url_param_name, url_param_name_l,
10997 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010998 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010999 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011000 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011001 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011002 if (!arg_start)
11003 return 0;
11004
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011005 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011006 while (1) {
11007 /* looks for the first argument. */
11008 value_start = memchr(arg_start, '=', qs_end - arg_start);
11009 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011010 /* Check for wrapping. */
11011 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011012 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011013 chunks[2]) {
11014 arg_start = chunks[2];
11015 qs_end = chunks[3];
11016 continue;
11017 }
11018 return 0;
11019 }
11020 break;
11021 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011022 value_start++;
11023 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011024 else {
11025 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011026 value_start = arg_start + url_param_name_l + 1;
11027
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011028 /* Check for pointer wrapping. */
11029 if (fix_pointer_if_wrap(chunks, &value_start)) {
11030 /* Update the end pointer. */
11031 qs_end = chunks[3];
11032
11033 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011034 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011035 return 0;
11036 }
11037 }
11038
David Cournapeau16023ee2010-12-23 20:55:41 +090011039 value_end = value_start;
11040
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011041 while (1) {
11042 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11043 value_end++;
11044 if (value_end < qs_end)
11045 break;
11046 /* process buffer wrapping. */
11047 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011048 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011049 chunks[2]) {
11050 value_end = chunks[2];
11051 qs_end = chunks[3];
11052 continue;
11053 }
11054 break;
11055 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011056
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011057 *vstart = value_start;
11058 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011059 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011060}
11061
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011062/* This scans a URL-encoded query string. It takes an optionally wrapping
11063 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11064 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11065 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011066 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011067static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011068smp_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 +090011069{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011070 const char *vstart, *vend;
11071 struct chunk *temp;
11072 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011073
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011074 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011075 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011076 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011077 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011078 return 0;
11079
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011080 /* Create sample. If the value is contiguous, return the pointer as CONST,
11081 * if the value is wrapped, copy-it in a buffer.
11082 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011083 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011084 if (chunks[2] &&
11085 vstart >= chunks[0] && vstart <= chunks[1] &&
11086 vend >= chunks[2] && vend <= chunks[3]) {
11087 /* Wrapped case. */
11088 temp = get_trash_chunk();
11089 memcpy(temp->str, vstart, chunks[1] - vstart);
11090 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011091 smp->data.u.str.str = temp->str;
11092 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011093 } else {
11094 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011095 smp->data.u.str.str = (char *)vstart;
11096 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011097 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11098 }
11099
11100 /* Update context, check wrapping. */
11101 chunks[0] = vend;
11102 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11103 chunks[1] = chunks[3];
11104 chunks[2] = NULL;
11105 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011106
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011107 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011108 smp->flags |= SMP_F_NOT_LAST;
11109
David Cournapeau16023ee2010-12-23 20:55:41 +090011110 return 1;
11111}
11112
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011113/* This function iterates over each parameter of the query string. It uses
11114 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011115 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11116 * An optional parameter name is passed in args[0], otherwise any parameter is
11117 * considered. It supports an optional delimiter argument for the beginning of
11118 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011119 */
11120static int
11121smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11122{
11123 struct http_msg *msg;
11124 char delim = '?';
11125 const char *name;
11126 int name_len;
11127
Dragan Dosen26f77e52015-05-25 10:02:11 +020011128 if (!args ||
11129 (args[0].type && args[0].type != ARGT_STR) ||
11130 (args[1].type && args[1].type != ARGT_STR))
11131 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011132
Dragan Dosen26f77e52015-05-25 10:02:11 +020011133 name = "";
11134 name_len = 0;
11135 if (args->type == ARGT_STR) {
11136 name = args->data.str.str;
11137 name_len = args->data.str.len;
11138 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011139
Dragan Dosen26f77e52015-05-25 10:02:11 +020011140 if (args[1].type)
11141 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011142
Dragan Dosen26f77e52015-05-25 10:02:11 +020011143 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011144 CHECK_HTTP_MESSAGE_FIRST();
11145
11146 msg = &smp->strm->txn->req;
11147
11148 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11149 msg->sl.rq.u_l, delim);
11150 if (!smp->ctx.a[0])
11151 return 0;
11152
11153 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011154
11155 /* Assume that the context is filled with NULL pointer
11156 * before the first call.
11157 * smp->ctx.a[2] = NULL;
11158 * smp->ctx.a[3] = NULL;
11159 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011160 }
11161
11162 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11163}
11164
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011165/* This function iterates over each parameter of the body. This requires
11166 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011167 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11168 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11169 * optional second part if the body wraps at the end of the buffer. An optional
11170 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011171 */
11172static int
11173smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11174{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011175 struct http_msg *msg;
11176 unsigned long len;
11177 unsigned long block1;
11178 char *body;
11179 const char *name;
11180 int name_len;
11181
11182 if (!args || (args[0].type && args[0].type != ARGT_STR))
11183 return 0;
11184
11185 name = "";
11186 name_len = 0;
11187 if (args[0].type == ARGT_STR) {
11188 name = args[0].data.str.str;
11189 name_len = args[0].data.str.len;
11190 }
11191
11192 if (!smp->ctx.a[0]) { // first call, find the query string
11193 CHECK_HTTP_MESSAGE_FIRST();
11194
11195 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011196 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011197 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011198 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011199
11200 len = http_body_bytes(msg);
11201 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11202
11203 block1 = len;
11204 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11205 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11206
11207 if (block1 == len) {
11208 /* buffer is not wrapped (or empty) */
11209 smp->ctx.a[0] = body;
11210 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011211
11212 /* Assume that the context is filled with NULL pointer
11213 * before the first call.
11214 * smp->ctx.a[2] = NULL;
11215 * smp->ctx.a[3] = NULL;
11216 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011217 }
11218 else {
11219 /* buffer is wrapped, we need to defragment it */
11220 smp->ctx.a[0] = body;
11221 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011222 smp->ctx.a[2] = msg->chn->buf->data;
11223 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011224 }
11225 }
11226 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11227}
11228
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011229/* Return the signed integer value for the specified url parameter (see url_param
11230 * above).
11231 */
11232static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011233smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011234{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011235 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011236
11237 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011238 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011239 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011240 }
11241
11242 return ret;
11243}
11244
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011245/* This produces a 32-bit hash of the concatenation of the first occurrence of
11246 * the Host header followed by the path component if it begins with a slash ('/').
11247 * This means that '*' will not be added, resulting in exactly the first Host
11248 * entry. If no Host header is found, then the path is used. The resulting value
11249 * is hashed using the url hash followed by a full avalanche hash and provides a
11250 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11251 * high-traffic sites without having to store whole paths.
11252 * this differs from the base32 functions in that it includes the url parameters
11253 * as well as the path
11254 */
11255static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011256smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011257{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011258 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011259 struct hdr_ctx ctx;
11260 unsigned int hash = 0;
11261 char *ptr, *beg, *end;
11262 int len;
11263
11264 CHECK_HTTP_MESSAGE_FIRST();
11265
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011266 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011267 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011268 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011269 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11270 ptr = ctx.line + ctx.val;
11271 len = ctx.vlen;
11272 while (len--)
11273 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11274 }
11275
11276 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011277 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 +000011278 beg = http_get_path(txn);
11279 if (!beg)
11280 beg = end;
11281
11282 for (ptr = beg; ptr < end ; ptr++);
11283
11284 if (beg < ptr && *beg == '/') {
11285 while (beg < ptr)
11286 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11287 }
11288 hash = full_hash(hash);
11289
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011290 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011291 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011292 smp->flags = SMP_F_VOL_1ST;
11293 return 1;
11294}
11295
11296/* This concatenates the source address with the 32-bit hash of the Host and
11297 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11298 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11299 * on the source address length. The URL hash is stored before the address so
11300 * that in environments where IPv6 is insignificant, truncating the output to
11301 * 8 bytes would still work.
11302 */
11303static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011304smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011305{
11306 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011307 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011308
Dragan Dosendb5af612016-06-16 11:23:01 +020011309 if (!cli_conn)
11310 return 0;
11311
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011312 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011313 return 0;
11314
11315 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011316 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11317 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011318
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011319 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011320 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011321 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011322 temp->len += 4;
11323 break;
11324 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011325 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011326 temp->len += 16;
11327 break;
11328 default:
11329 return 0;
11330 }
11331
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011332 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011333 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011334 return 1;
11335}
11336
Willy Tarreau185b5c42012-04-26 15:11:51 +020011337/* This function is used to validate the arguments passed to any "hdr" fetch
11338 * keyword. These keywords support an optional positive or negative occurrence
11339 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11340 * is assumed that the types are already the correct ones. Returns 0 on error,
11341 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11342 * error message in case of error, that the caller is responsible for freeing.
11343 * The initial location must either be freeable or NULL.
11344 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011345int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011346{
11347 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011348 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011349 return 0;
11350 }
11351 return 1;
11352}
11353
Willy Tarreau276fae92013-07-25 14:36:01 +020011354/* takes an UINT value on input supposed to represent the time since EPOCH,
11355 * adds an optional offset found in args[0] and emits a string representing
11356 * the date in RFC-1123/5322 format.
11357 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011358static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011359{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011360 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011361 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11362 struct chunk *temp;
11363 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011364 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011365 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011366
11367 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011368 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011369 curr_date += args[0].data.sint;
11370
11371 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011372 if (!tm)
11373 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011374
11375 temp = get_trash_chunk();
11376 temp->len = snprintf(temp->str, temp->size - temp->len,
11377 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11378 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11379 tm->tm_hour, tm->tm_min, tm->tm_sec);
11380
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011381 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011382 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011383 return 1;
11384}
11385
Thierry FOURNIERad903512014-04-11 17:51:01 +020011386/* Match language range with language tag. RFC2616 14.4:
11387 *
11388 * A language-range matches a language-tag if it exactly equals
11389 * the tag, or if it exactly equals a prefix of the tag such
11390 * that the first tag character following the prefix is "-".
11391 *
11392 * Return 1 if the strings match, else return 0.
11393 */
11394static inline int language_range_match(const char *range, int range_len,
11395 const char *tag, int tag_len)
11396{
11397 const char *end = range + range_len;
11398 const char *tend = tag + tag_len;
11399 while (range < end) {
11400 if (*range == '-' && tag == tend)
11401 return 1;
11402 if (*range != *tag || tag == tend)
11403 return 0;
11404 range++;
11405 tag++;
11406 }
11407 /* Return true only if the last char of the tag is matched. */
11408 return tag == tend;
11409}
11410
11411/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011412static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011413{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011414 const char *al = smp->data.u.str.str;
11415 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011416 const char *token;
11417 int toklen;
11418 int qvalue;
11419 const char *str;
11420 const char *w;
11421 int best_q = 0;
11422
11423 /* Set the constant to the sample, because the output of the
11424 * function will be peek in the constant configuration string.
11425 */
11426 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011427 smp->data.u.str.size = 0;
11428 smp->data.u.str.str = "";
11429 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011430
11431 /* Parse the accept language */
11432 while (1) {
11433
11434 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011435 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011436 al++;
11437 if (al >= end)
11438 break;
11439
11440 /* Start of the fisrt word. */
11441 token = al;
11442
11443 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011444 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011445 al++;
11446 if (al == token)
11447 goto expect_comma;
11448
11449 /* Length of the token. */
11450 toklen = al - token;
11451 qvalue = 1000;
11452
11453 /* Check if the token exists in the list. If the token not exists,
11454 * jump to the next token.
11455 */
11456 str = args[0].data.str.str;
11457 w = str;
11458 while (1) {
11459 if (*str == ';' || *str == '\0') {
11460 if (language_range_match(token, toklen, w, str-w))
11461 goto look_for_q;
11462 if (*str == '\0')
11463 goto expect_comma;
11464 w = str + 1;
11465 }
11466 str++;
11467 }
11468 goto expect_comma;
11469
11470look_for_q:
11471
11472 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011473 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011474 al++;
11475 if (al >= end)
11476 goto process_value;
11477
11478 /* If ',' is found, process the result */
11479 if (*al == ',')
11480 goto process_value;
11481
11482 /* If the character is different from ';', look
11483 * for the end of the header part in best effort.
11484 */
11485 if (*al != ';')
11486 goto expect_comma;
11487
11488 /* Assumes that the char is ';', now expect "q=". */
11489 al++;
11490
11491 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011492 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011493 al++;
11494 if (al >= end)
11495 goto process_value;
11496
11497 /* Expect 'q'. If no 'q', continue in best effort */
11498 if (*al != 'q')
11499 goto process_value;
11500 al++;
11501
11502 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011503 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011504 al++;
11505 if (al >= end)
11506 goto process_value;
11507
11508 /* Expect '='. If no '=', continue in best effort */
11509 if (*al != '=')
11510 goto process_value;
11511 al++;
11512
11513 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011514 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011515 al++;
11516 if (al >= end)
11517 goto process_value;
11518
11519 /* Parse the q value. */
11520 qvalue = parse_qvalue(al, &al);
11521
11522process_value:
11523
11524 /* If the new q value is the best q value, then store the associated
11525 * language in the response. If qvalue is the biggest value (1000),
11526 * break the process.
11527 */
11528 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011529 smp->data.u.str.str = (char *)w;
11530 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011531 if (qvalue >= 1000)
11532 break;
11533 best_q = qvalue;
11534 }
11535
11536expect_comma:
11537
11538 /* Expect comma or end. If the end is detected, quit the loop. */
11539 while (al < end && *al != ',')
11540 al++;
11541 if (al >= end)
11542 break;
11543
11544 /* Comma is found, jump it and restart the analyzer. */
11545 al++;
11546 }
11547
11548 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011549 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11550 smp->data.u.str.str = args[1].data.str.str;
11551 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011552 }
11553
11554 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011555 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011556}
11557
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011558/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011559static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011560{
11561 /* If the constant flag is set or if not size is avalaible at
11562 * the end of the buffer, copy the string in other buffer
11563 * before decoding.
11564 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011565 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011566 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011567 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11568 smp->data.u.str.str = str->str;
11569 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011570 smp->flags &= ~SMP_F_CONST;
11571 }
11572
11573 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011574 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11575 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011576 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011577}
11578
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011579static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11580{
11581 struct proxy *fe = strm_fe(smp->strm);
11582 int idx, i;
11583 struct cap_hdr *hdr;
11584 int len;
11585
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011586 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011587 return 0;
11588
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011589 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011590
11591 /* Check the availibity of the capture id. */
11592 if (idx > fe->nb_req_cap - 1)
11593 return 0;
11594
11595 /* Look for the original configuration. */
11596 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11597 hdr != NULL && i != idx ;
11598 i--, hdr = hdr->next);
11599 if (!hdr)
11600 return 0;
11601
11602 /* check for the memory allocation */
11603 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011604 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011605 if (smp->strm->req_cap[hdr->index] == NULL)
11606 return 0;
11607
11608 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011609 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011610 if (len > hdr->len)
11611 len = hdr->len;
11612
11613 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011614 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011615 smp->strm->req_cap[idx][len] = '\0';
11616
11617 return 1;
11618}
11619
11620static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11621{
11622 struct proxy *fe = strm_fe(smp->strm);
11623 int idx, i;
11624 struct cap_hdr *hdr;
11625 int len;
11626
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011627 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011628 return 0;
11629
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011630 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011631
11632 /* Check the availibity of the capture id. */
11633 if (idx > fe->nb_rsp_cap - 1)
11634 return 0;
11635
11636 /* Look for the original configuration. */
11637 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11638 hdr != NULL && i != idx ;
11639 i--, hdr = hdr->next);
11640 if (!hdr)
11641 return 0;
11642
11643 /* check for the memory allocation */
11644 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011645 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011646 if (smp->strm->res_cap[hdr->index] == NULL)
11647 return 0;
11648
11649 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011650 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011651 if (len > hdr->len)
11652 len = hdr->len;
11653
11654 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011655 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011656 smp->strm->res_cap[idx][len] = '\0';
11657
11658 return 1;
11659}
11660
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011661/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011662 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011663 * the relevant part of the request line accordingly. Then it updates various
11664 * pointers to the next elements which were moved, and the total buffer length.
11665 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011666 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11667 * error, though this can be revisited when this code is finally exploited.
11668 *
11669 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11670 * query string and 3 to replace uri.
11671 *
11672 * In query string case, the mark question '?' must be set at the start of the
11673 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011674 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011675int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011676 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011677{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011678 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011679 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011680 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011681 int delta;
11682
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011683 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011684 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011685 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011686 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11687
11688 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011689 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011690 txn->req.sl.rq.m_l += delta;
11691 txn->req.sl.rq.u += delta;
11692 txn->req.sl.rq.v += delta;
11693 break;
11694
11695 case 1: // path
11696 cur_ptr = http_get_path(txn);
11697 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011698 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011699
11700 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011701 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 +010011702 cur_end++;
11703
11704 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011705 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011706 txn->req.sl.rq.u_l += delta;
11707 txn->req.sl.rq.v += delta;
11708 break;
11709
11710 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011711 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011712 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011713 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11714 while (cur_ptr < cur_end && *cur_ptr != '?')
11715 cur_ptr++;
11716
11717 /* skip the question mark or indicate that we must insert it
11718 * (but only if the format string is not empty then).
11719 */
11720 if (cur_ptr < cur_end)
11721 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011722 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011723 offset = 0;
11724
11725 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011726 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011727 txn->req.sl.rq.u_l += delta;
11728 txn->req.sl.rq.v += delta;
11729 break;
11730
11731 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011732 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011733 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11734
11735 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011736 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011737 txn->req.sl.rq.u_l += delta;
11738 txn->req.sl.rq.v += delta;
11739 break;
11740
11741 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011742 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011743 }
11744
11745 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011746 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011747 txn->req.sl.rq.l += delta;
11748 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011749 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011750 return 0;
11751}
11752
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011753/* This function replace the HTTP status code and the associated message. The
11754 * variable <status> contains the new status code. This function never fails.
11755 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011756void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011757{
11758 struct http_txn *txn = s->txn;
11759 char *cur_ptr, *cur_end;
11760 int delta;
11761 char *res;
11762 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011763 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011764 int msg_len;
11765
11766 chunk_reset(&trash);
11767
11768 res = ultoa_o(status, trash.str, trash.size);
11769 c_l = res - trash.str;
11770
11771 trash.str[c_l] = ' ';
11772 trash.len = c_l + 1;
11773
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011774 /* Do we have a custom reason format string? */
11775 if (msg == NULL)
11776 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011777 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011778 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11779 trash.len += msg_len;
11780
11781 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11782 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11783
11784 /* commit changes and adjust message */
11785 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11786
11787 /* adjust res line offsets and lengths */
11788 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11789 txn->rsp.sl.st.c_l = c_l;
11790 txn->rsp.sl.st.r_l = msg_len;
11791
11792 delta = trash.len - (cur_end - cur_ptr);
11793 txn->rsp.sl.st.l += delta;
11794 txn->hdr_idx.v[0].len += delta;
11795 http_msg_move_end(&txn->rsp, delta);
11796}
11797
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011798/* This function executes one of the set-{method,path,query,uri} actions. It
11799 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011800 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011801 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011802 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11803 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011804 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011805enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011806 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011807{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011808 struct chunk *replace;
11809 enum act_return ret = ACT_RET_ERR;
11810
11811 replace = alloc_trash_chunk();
11812 if (!replace)
11813 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011814
11815 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011816 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011817 replace->str[replace->len++] = '?';
11818 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11819 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011820
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011821 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11822
11823 ret = ACT_RET_CONT;
11824
11825leave:
11826 free_trash_chunk(replace);
11827 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011828}
11829
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011830/* This function is just a compliant action wrapper for "set-status". */
11831enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011832 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011833{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011834 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011835 return ACT_RET_CONT;
11836}
11837
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011838/* parse an http-request action among :
11839 * set-method
11840 * set-path
11841 * set-query
11842 * set-uri
11843 *
11844 * All of them accept a single argument of type string representing a log-format.
11845 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11846 * 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 +020011847 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011848 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011849enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11850 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011851{
11852 int cur_arg = *orig_arg;
11853
Thierry FOURNIER42148732015-09-02 17:17:33 +020011854 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011855
11856 switch (args[0][4]) {
11857 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011858 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011859 rule->action_ptr = http_action_set_req_line;
11860 break;
11861 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011862 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011863 rule->action_ptr = http_action_set_req_line;
11864 break;
11865 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011866 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011867 rule->action_ptr = http_action_set_req_line;
11868 break;
11869 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011870 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011871 rule->action_ptr = http_action_set_req_line;
11872 break;
11873 default:
11874 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011875 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011876 }
11877
11878 if (!*args[cur_arg] ||
11879 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11880 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011881 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011882 }
11883
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011884 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011885 px->conf.args.ctx = ARGC_HRQ;
11886 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
11887 (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 +010011888 return ACT_RET_PRS_ERR;
11889 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011890
11891 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011892 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011893}
11894
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011895/* parse set-status action:
11896 * This action accepts a single argument of type int representing
11897 * an http status code. It returns ACT_RET_PRS_OK on success,
11898 * ACT_RET_PRS_ERR on error.
11899 */
11900enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11901 struct act_rule *rule, char **err)
11902{
11903 char *error;
11904
Thierry FOURNIER42148732015-09-02 17:17:33 +020011905 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011906 rule->action_ptr = action_http_set_status;
11907
11908 /* Check if an argument is available */
11909 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011910 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011911 return ACT_RET_PRS_ERR;
11912 }
11913
11914 /* convert status code as integer */
11915 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11916 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11917 memprintf(err, "expects an integer status code between 100 and 999");
11918 return ACT_RET_PRS_ERR;
11919 }
11920
11921 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011922
11923 /* set custom reason string */
11924 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11925 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11926 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11927 (*orig_arg)++;
11928 rule->arg.status.reason = strdup(args[*orig_arg]);
11929 (*orig_arg)++;
11930 }
11931
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011932 return ACT_RET_PRS_OK;
11933}
11934
Willy Tarreau53275e82017-11-24 07:52:01 +010011935/* This function executes the "reject" HTTP action. It clears the request and
11936 * response buffer without sending any response. It can be useful as an HTTP
11937 * alternative to the silent-drop action to defend against DoS attacks, and may
11938 * also be used with HTTP/2 to close a connection instead of just a stream.
11939 * The txn status is unchanged, indicating no response was sent. The termination
11940 * flags will indicate "PR". It always returns ACT_RET_STOP.
11941 */
11942enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
11943 struct session *sess, struct stream *s, int flags)
11944{
11945 channel_abort(&s->req);
11946 channel_abort(&s->res);
11947 s->req.analysers = 0;
11948 s->res.analysers = 0;
11949
11950 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
11951 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
11952 if (sess->listener && sess->listener->counters)
11953 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
11954
11955 if (!(s->flags & SF_ERR_MASK))
11956 s->flags |= SF_ERR_PRXCOND;
11957 if (!(s->flags & SF_FINST_MASK))
11958 s->flags |= SF_FINST_R;
11959
11960 return ACT_RET_CONT;
11961}
11962
11963/* parse the "reject" action:
11964 * This action takes no argument and returns ACT_RET_PRS_OK on success,
11965 * ACT_RET_PRS_ERR on error.
11966 */
11967enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
11968 struct act_rule *rule, char **err)
11969{
11970 rule->action = ACT_CUSTOM;
11971 rule->action_ptr = http_action_reject;
11972 return ACT_RET_PRS_OK;
11973}
11974
Willy Tarreaua9083d02015-05-08 15:27:59 +020011975/* This function executes the "capture" action. It executes a fetch expression,
11976 * turns the result into a string and puts it in a capture slot. It always
11977 * returns 1. If an error occurs the action is cancelled, but the rule
11978 * processing continues.
11979 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011980enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011981 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011982{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011983 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011984 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011985 char **cap = s->req_cap;
11986 int len;
11987
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011988 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 +020011989 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011990 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011991
11992 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011993 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011994
11995 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011996 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011997
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011998 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011999 if (len > h->len)
12000 len = h->len;
12001
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012002 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012003 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012004 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012005}
12006
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012007/* This function executes the "capture" action and store the result in a
12008 * capture slot if exists. It executes a fetch expression, turns the result
12009 * into a string and puts it in a capture slot. It always returns 1. If an
12010 * error occurs the action is cancelled, but the rule processing continues.
12011 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012012enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012013 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012014{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012015 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012016 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012017 char **cap = s->req_cap;
12018 struct proxy *fe = strm_fe(s);
12019 int len;
12020 int i;
12021
12022 /* Look for the original configuration. */
12023 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012024 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012025 i--, h = h->next);
12026 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012027 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012028
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012029 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 +020012030 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012031 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012032
12033 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012034 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012035
12036 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012037 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012038
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012039 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012040 if (len > h->len)
12041 len = h->len;
12042
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012043 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012044 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012045 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012046}
12047
Christopher Faulet29730ba2017-09-18 15:26:32 +020012048/* Check an "http-request capture" action.
12049 *
12050 * The function returns 1 in success case, otherwise, it returns 0 and err is
12051 * filled.
12052 */
12053int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12054{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012055 if (rule->action_ptr != http_action_req_capture_by_id)
12056 return 1;
12057
Christopher Faulet29730ba2017-09-18 15:26:32 +020012058 if (rule->arg.capid.idx >= px->nb_req_cap) {
12059 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12060 rule->arg.capid.idx);
12061 return 0;
12062 }
12063
12064 return 1;
12065}
12066
Willy Tarreaua9083d02015-05-08 15:27:59 +020012067/* parse an "http-request capture" action. It takes a single argument which is
12068 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012069 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012070 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012071 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012072enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12073 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012074{
12075 struct sample_expr *expr;
12076 struct cap_hdr *hdr;
12077 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012078 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012079
12080 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12081 if (strcmp(args[cur_arg], "if") == 0 ||
12082 strcmp(args[cur_arg], "unless") == 0)
12083 break;
12084
12085 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012086 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012087 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012088 }
12089
Willy Tarreaua9083d02015-05-08 15:27:59 +020012090 cur_arg = *orig_arg;
12091 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12092 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012093 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012094
12095 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12096 memprintf(err,
12097 "fetch method '%s' extracts information from '%s', none of which is available here",
12098 args[cur_arg-1], sample_src_names(expr->fetch->use));
12099 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012100 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012101 }
12102
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012103 if (!args[cur_arg] || !*args[cur_arg]) {
12104 memprintf(err, "expects 'len or 'id'");
12105 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012106 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012107 }
12108
Willy Tarreaua9083d02015-05-08 15:27:59 +020012109 if (strcmp(args[cur_arg], "len") == 0) {
12110 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012111
12112 if (!(px->cap & PR_CAP_FE)) {
12113 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012114 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012115 }
12116
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012117 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012118
Willy Tarreaua9083d02015-05-08 15:27:59 +020012119 if (!args[cur_arg]) {
12120 memprintf(err, "missing length value");
12121 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012122 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012123 }
12124 /* we copy the table name for now, it will be resolved later */
12125 len = atoi(args[cur_arg]);
12126 if (len <= 0) {
12127 memprintf(err, "length must be > 0");
12128 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012129 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012130 }
12131 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012132
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012133 if (!len) {
12134 memprintf(err, "a positive 'len' argument is mandatory");
12135 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012136 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012137 }
12138
Vincent Bernat02779b62016-04-03 13:48:43 +020012139 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012140 hdr->next = px->req_cap;
12141 hdr->name = NULL; /* not a header capture */
12142 hdr->namelen = 0;
12143 hdr->len = len;
12144 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12145 hdr->index = px->nb_req_cap++;
12146
12147 px->req_cap = hdr;
12148 px->to_log |= LW_REQHDR;
12149
Thierry FOURNIER42148732015-09-02 17:17:33 +020012150 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012151 rule->action_ptr = http_action_req_capture;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012152 rule->check_ptr = check_http_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012153 rule->arg.cap.expr = expr;
12154 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012155 }
12156
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012157 else if (strcmp(args[cur_arg], "id") == 0) {
12158 int id;
12159 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012160
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012161 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012162
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012163 if (!args[cur_arg]) {
12164 memprintf(err, "missing id value");
12165 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012166 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012167 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012168
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012169 id = strtol(args[cur_arg], &error, 10);
12170 if (*error != '\0') {
12171 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12172 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012173 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012174 }
12175 cur_arg++;
12176
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012177 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012178
Thierry FOURNIER42148732015-09-02 17:17:33 +020012179 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012180 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012181 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012182 rule->arg.capid.expr = expr;
12183 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012184 }
12185
12186 else {
12187 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12188 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012189 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012190 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012191
12192 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012193 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012194}
12195
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012196/* This function executes the "capture" action and store the result in a
12197 * capture slot if exists. It executes a fetch expression, turns the result
12198 * into a string and puts it in a capture slot. It always returns 1. If an
12199 * error occurs the action is cancelled, but the rule processing continues.
12200 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012201enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012202 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012203{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012204 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012205 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012206 char **cap = s->res_cap;
12207 struct proxy *fe = strm_fe(s);
12208 int len;
12209 int i;
12210
12211 /* Look for the original configuration. */
12212 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012213 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012214 i--, h = h->next);
12215 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012216 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012217
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012218 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 +020012219 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012220 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012221
12222 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012223 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012224
12225 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012226 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012227
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012228 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012229 if (len > h->len)
12230 len = h->len;
12231
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012232 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012233 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012234 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012235}
12236
Christopher Faulet29730ba2017-09-18 15:26:32 +020012237/* Check an "http-response capture" action.
12238 *
12239 * The function returns 1 in success case, otherwise, it returns 0 and err is
12240 * filled.
12241 */
12242int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12243{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012244 if (rule->action_ptr != http_action_res_capture_by_id)
12245 return 1;
12246
Christopher Faulet29730ba2017-09-18 15:26:32 +020012247 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12248 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12249 rule->arg.capid.idx);
12250 return 0;
12251 }
12252
12253 return 1;
12254}
12255
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012256/* parse an "http-response capture" action. It takes a single argument which is
12257 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12258 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012259 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012260 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012261enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12262 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012263{
12264 struct sample_expr *expr;
12265 int cur_arg;
12266 int id;
12267 char *error;
12268
12269 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12270 if (strcmp(args[cur_arg], "if") == 0 ||
12271 strcmp(args[cur_arg], "unless") == 0)
12272 break;
12273
12274 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012275 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012276 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012277 }
12278
12279 cur_arg = *orig_arg;
12280 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12281 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012282 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012283
12284 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12285 memprintf(err,
12286 "fetch method '%s' extracts information from '%s', none of which is available here",
12287 args[cur_arg-1], sample_src_names(expr->fetch->use));
12288 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012289 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012290 }
12291
12292 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012293 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012294 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012295 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012296 }
12297
12298 if (strcmp(args[cur_arg], "id") != 0) {
12299 memprintf(err, "expects 'id', found '%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
12304 cur_arg++;
12305
12306 if (!args[cur_arg]) {
12307 memprintf(err, "missing id value");
12308 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012309 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012310 }
12311
12312 id = strtol(args[cur_arg], &error, 10);
12313 if (*error != '\0') {
12314 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12315 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012316 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012317 }
12318 cur_arg++;
12319
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012320 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012321
Thierry FOURNIER42148732015-09-02 17:17:33 +020012322 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012323 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012324 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012325 rule->arg.capid.expr = expr;
12326 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012327
12328 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012329 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012330}
12331
William Lallemand73025dd2014-04-24 14:38:37 +020012332/*
12333 * Return the struct http_req_action_kw associated to a keyword.
12334 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012335struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012336{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012337 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012338}
12339
12340/*
12341 * Return the struct http_res_action_kw associated to a keyword.
12342 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012343struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012344{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012345 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012346}
12347
Willy Tarreau12207b32016-11-22 19:48:51 +010012348
12349/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12350 * now.
12351 */
12352static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12353{
12354 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12355 return 1;
12356
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012357 if (*args[2]) {
12358 struct proxy *px;
12359
12360 px = proxy_find_by_name(args[2], 0, 0);
12361 if (px)
12362 appctx->ctx.errors.iid = px->uuid;
12363 else
12364 appctx->ctx.errors.iid = atoi(args[2]);
12365
12366 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012367 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012368 appctx->ctx.cli.msg = "No such proxy.\n";
12369 appctx->st0 = CLI_ST_PRINT;
12370 return 1;
12371 }
12372 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012373 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012374 appctx->ctx.errors.iid = -1; // dump all proxies
12375
Willy Tarreau35069f82016-11-25 09:16:37 +010012376 appctx->ctx.errors.flag = 0;
12377 if (strcmp(args[3], "request") == 0)
12378 appctx->ctx.errors.flag |= 4; // ignore response
12379 else if (strcmp(args[3], "response") == 0)
12380 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012381 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012382 return 0;
12383}
12384
12385/* This function dumps all captured errors onto the stream interface's
12386 * read buffer. It returns 0 if the output buffer is full and it needs
12387 * to be called again, otherwise non-zero.
12388 */
12389static int cli_io_handler_show_errors(struct appctx *appctx)
12390{
12391 struct stream_interface *si = appctx->owner;
12392 extern const char *monthname[12];
12393
12394 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12395 return 1;
12396
12397 chunk_reset(&trash);
12398
12399 if (!appctx->ctx.errors.px) {
12400 /* the function had not been called yet, let's prepare the
12401 * buffer for a response.
12402 */
12403 struct tm tm;
12404
12405 get_localtime(date.tv_sec, &tm);
12406 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12407 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12408 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12409 error_snapshot_id);
12410
Willy Tarreau06d80a92017-10-19 14:32:15 +020012411 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012412 /* Socket buffer full. Let's try again later from the same point */
12413 si_applet_cant_put(si);
12414 return 0;
12415 }
12416
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012417 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012418 appctx->ctx.errors.bol = 0;
12419 appctx->ctx.errors.ptr = -1;
12420 }
12421
12422 /* we have two inner loops here, one for the proxy, the other one for
12423 * the buffer.
12424 */
12425 while (appctx->ctx.errors.px) {
12426 struct error_snapshot *es;
12427
Willy Tarreau35069f82016-11-25 09:16:37 +010012428 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012429 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012430 if (appctx->ctx.errors.flag & 2) // skip req
12431 goto next;
12432 }
12433 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012434 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012435 if (appctx->ctx.errors.flag & 4) // skip resp
12436 goto next;
12437 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012438
12439 if (!es->when.tv_sec)
12440 goto next;
12441
12442 if (appctx->ctx.errors.iid >= 0 &&
12443 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12444 es->oe->uuid != appctx->ctx.errors.iid)
12445 goto next;
12446
12447 if (appctx->ctx.errors.ptr < 0) {
12448 /* just print headers now */
12449
12450 char pn[INET6_ADDRSTRLEN];
12451 struct tm tm;
12452 int port;
12453
12454 get_localtime(es->when.tv_sec, &tm);
12455 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12456 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12457 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12458
12459 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12460 case AF_INET:
12461 case AF_INET6:
12462 port = get_host_port(&es->src);
12463 break;
12464 default:
12465 port = 0;
12466 }
12467
Willy Tarreau35069f82016-11-25 09:16:37 +010012468 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012469 case 0:
12470 chunk_appendf(&trash,
12471 " frontend %s (#%d): invalid request\n"
12472 " backend %s (#%d)",
12473 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12474 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12475 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12476 break;
12477 case 1:
12478 chunk_appendf(&trash,
12479 " backend %s (#%d): invalid response\n"
12480 " frontend %s (#%d)",
12481 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12482 es->oe->id, es->oe->uuid);
12483 break;
12484 }
12485
12486 chunk_appendf(&trash,
12487 ", server %s (#%d), event #%u\n"
12488 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012489 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012490 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12491 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12492 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12493 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12494 es->ev_id,
12495 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012496 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012497 es->m_clen, es->m_blen,
12498 es->b_flags, es->b_out, es->b_tot,
12499 es->len, es->b_wrap, es->pos);
12500
Willy Tarreau06d80a92017-10-19 14:32:15 +020012501 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012502 /* Socket buffer full. Let's try again later from the same point */
12503 si_applet_cant_put(si);
12504 return 0;
12505 }
12506 appctx->ctx.errors.ptr = 0;
12507 appctx->ctx.errors.sid = es->sid;
12508 }
12509
12510 if (appctx->ctx.errors.sid != es->sid) {
12511 /* the snapshot changed while we were dumping it */
12512 chunk_appendf(&trash,
12513 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012514 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012515 si_applet_cant_put(si);
12516 return 0;
12517 }
12518 goto next;
12519 }
12520
12521 /* OK, ptr >= 0, so we have to dump the current line */
12522 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12523 int newptr;
12524 int newline;
12525
12526 newline = appctx->ctx.errors.bol;
12527 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12528 if (newptr == appctx->ctx.errors.ptr)
12529 return 0;
12530
Willy Tarreau06d80a92017-10-19 14:32:15 +020012531 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012532 /* Socket buffer full. Let's try again later from the same point */
12533 si_applet_cant_put(si);
12534 return 0;
12535 }
12536 appctx->ctx.errors.ptr = newptr;
12537 appctx->ctx.errors.bol = newline;
12538 };
12539 next:
12540 appctx->ctx.errors.bol = 0;
12541 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012542 appctx->ctx.errors.flag ^= 1;
12543 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012544 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012545 }
12546
12547 /* dump complete */
12548 return 1;
12549}
12550
12551/* register cli keywords */
12552static struct cli_kw_list cli_kws = {{ },{
12553 { { "show", "errors", NULL },
12554 "show errors : report last request and response errors for each proxy",
12555 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12556 },
12557 {{},}
12558}};
12559
Willy Tarreau4a568972010-05-12 08:08:50 +020012560/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012561/* All supported ACL keywords must be declared here. */
12562/************************************************************************/
12563
12564/* Note: must not be declared <const> as its list will be overwritten.
12565 * Please take care of keeping this list alphabetically sorted.
12566 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012567static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012568 { "base", "base", PAT_MATCH_STR },
12569 { "base_beg", "base", PAT_MATCH_BEG },
12570 { "base_dir", "base", PAT_MATCH_DIR },
12571 { "base_dom", "base", PAT_MATCH_DOM },
12572 { "base_end", "base", PAT_MATCH_END },
12573 { "base_len", "base", PAT_MATCH_LEN },
12574 { "base_reg", "base", PAT_MATCH_REG },
12575 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012576
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012577 { "cook", "req.cook", PAT_MATCH_STR },
12578 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12579 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12580 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12581 { "cook_end", "req.cook", PAT_MATCH_END },
12582 { "cook_len", "req.cook", PAT_MATCH_LEN },
12583 { "cook_reg", "req.cook", PAT_MATCH_REG },
12584 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012585
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012586 { "hdr", "req.hdr", PAT_MATCH_STR },
12587 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12588 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12589 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12590 { "hdr_end", "req.hdr", PAT_MATCH_END },
12591 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12592 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12593 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012594
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012595 /* these two declarations uses strings with list storage (in place
12596 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12597 * and delete functions are relative to the list management. The parse
12598 * and match method are related to the corresponding fetch methods. This
12599 * is very particular ACL declaration mode.
12600 */
12601 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12602 { "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 +020012603
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012604 { "path", "path", PAT_MATCH_STR },
12605 { "path_beg", "path", PAT_MATCH_BEG },
12606 { "path_dir", "path", PAT_MATCH_DIR },
12607 { "path_dom", "path", PAT_MATCH_DOM },
12608 { "path_end", "path", PAT_MATCH_END },
12609 { "path_len", "path", PAT_MATCH_LEN },
12610 { "path_reg", "path", PAT_MATCH_REG },
12611 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012612
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012613 { "req_ver", "req.ver", PAT_MATCH_STR },
12614 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012615
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012616 { "scook", "res.cook", PAT_MATCH_STR },
12617 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12618 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12619 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12620 { "scook_end", "res.cook", PAT_MATCH_END },
12621 { "scook_len", "res.cook", PAT_MATCH_LEN },
12622 { "scook_reg", "res.cook", PAT_MATCH_REG },
12623 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012624
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012625 { "shdr", "res.hdr", PAT_MATCH_STR },
12626 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12627 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12628 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12629 { "shdr_end", "res.hdr", PAT_MATCH_END },
12630 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12631 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12632 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012633
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012634 { "url", "url", PAT_MATCH_STR },
12635 { "url_beg", "url", PAT_MATCH_BEG },
12636 { "url_dir", "url", PAT_MATCH_DIR },
12637 { "url_dom", "url", PAT_MATCH_DOM },
12638 { "url_end", "url", PAT_MATCH_END },
12639 { "url_len", "url", PAT_MATCH_LEN },
12640 { "url_reg", "url", PAT_MATCH_REG },
12641 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012642
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012643 { "urlp", "urlp", PAT_MATCH_STR },
12644 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12645 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12646 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12647 { "urlp_end", "urlp", PAT_MATCH_END },
12648 { "urlp_len", "urlp", PAT_MATCH_LEN },
12649 { "urlp_reg", "urlp", PAT_MATCH_REG },
12650 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012651
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012652 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012653}};
12654
12655/************************************************************************/
12656/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012657/************************************************************************/
12658/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012659static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012660 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012661 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012662 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12663
Willy Tarreau87b09662015-04-03 00:22:06 +020012664 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012665 { "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 +020012666
12667 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012668 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12669 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12670 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012671
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012672 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12673 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012674
Willy Tarreau409bcde2013-01-08 00:31:00 +010012675 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12676 * are only here to match the ACL's name, are request-only and are used
12677 * for ACL compatibility only.
12678 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012679 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12680 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012681 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12682 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012683
12684 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12685 * only here to match the ACL's name, are request-only and are used for
12686 * ACL compatibility only.
12687 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012688 { "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 +020012689 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012690 { "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 +020012691 { "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 +010012692
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012693 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012694 { "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 +010012695 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012696 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012697 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012698 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012699
12700 /* HTTP protocol on the request path */
12701 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012702 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012703
12704 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012705 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12706 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012707
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012708 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012709 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12710 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012711 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012712
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012713 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012714 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12715
Willy Tarreau18ed2562013-01-14 15:56:36 +010012716 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012717 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12718 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012719
Willy Tarreau18ed2562013-01-14 15:56:36 +010012720 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012721 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012722 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12723 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012724
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012725 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012726 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012727 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012728 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012729 { "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 +010012730 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012731 { "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 +010012732
12733 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012734 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012735 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12736 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012737
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012738 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012739 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012740 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012741 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012742 { "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 +010012743 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012744 { "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 +010012745
Willy Tarreau409bcde2013-01-08 00:31:00 +010012746 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012747 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012748 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12749 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012750 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012751
12752 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012753 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012754 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012755 { "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 +020012756 { "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 +010012757
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012758 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012759 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012760 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012761 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012762 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012763 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012764 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012765 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12766 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012767 { "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 +010012768 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012769}};
12770
Willy Tarreau8797c062007-05-07 00:55:35 +020012771
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012772/************************************************************************/
12773/* All supported converter keywords must be declared here. */
12774/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012775/* Note: must not be declared <const> as its list will be overwritten */
12776static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012777 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012778 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012779 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12780 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012781 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012782 { NULL, NULL, 0, 0, 0 },
12783}};
12784
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012785
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012786/************************************************************************/
12787/* All supported http-request action keywords must be declared here. */
12788/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012789struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012790 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012791 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012792 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012793 { "set-method", parse_set_req_line },
12794 { "set-path", parse_set_req_line },
12795 { "set-query", parse_set_req_line },
12796 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012797 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012798 }
12799};
12800
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012801struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012802 .kw = {
12803 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012804 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012805 { NULL, NULL }
12806 }
12807};
12808
Willy Tarreau8797c062007-05-07 00:55:35 +020012809__attribute__((constructor))
12810static void __http_protocol_init(void)
12811{
12812 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012813 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012814 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012815 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012816 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012817 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012818}
12819
12820
Willy Tarreau58f10d72006-12-04 02:26:12 +010012821/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012822 * Local variables:
12823 * c-indent-level: 8
12824 * c-basic-offset: 8
12825 * End:
12826 */