blob: 8370889b417c798496c8f48fa5ab5a5aaf73ee0d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010065#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020066#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020070#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010071#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020072#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010073#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020075#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau522d6c02009-12-06 18:49:18 +010078const char HTTP_100[] =
79 "HTTP/1.1 100 Continue\r\n\r\n";
80
81const struct chunk http_100_chunk = {
82 .str = (char *)&HTTP_100,
83 .len = sizeof(HTTP_100)-1
84};
85
Willy Tarreaua9679ac2010-01-03 17:32:57 +010086/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400105
106/* same as 302 except that the browser MUST retry with the same method */
107const char *HTTP_307 =
108 "HTTP/1.1 307 Temporary Redirect\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Content-length: 0\r\n"
111 "Location: "; /* not terminated since it will be concatenated with the URL */
112
113/* same as 301 except that the browser MUST retry with the same method */
114const char *HTTP_308 =
115 "HTTP/1.1 308 Permanent Redirect\r\n"
116 "Content-length: 0\r\n"
117 "Location: "; /* not terminated since it will be concatenated with the URL */
118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
120const char *HTTP_401_fmt =
121 "HTTP/1.0 401 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200124 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
Willy Tarreau844a7e72010-01-31 21:46:18 +0100129const char *HTTP_407_fmt =
130 "HTTP/1.0 407 Unauthorized\r\n"
131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
135 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800136 "<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 +0100137
Willy Tarreau0f772532006-12-23 20:51:41 +0100138
139const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_400] = 400,
142 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400143 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100144 [HTTP_ERR_408] = 408,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200145 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400146 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100147 [HTTP_ERR_500] = 500,
148 [HTTP_ERR_502] = 502,
149 [HTTP_ERR_503] = 503,
150 [HTTP_ERR_504] = 504,
151};
152
Willy Tarreau80587432006-12-24 17:47:20 +0100153static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200154 [HTTP_ERR_200] =
155 "HTTP/1.0 200 OK\r\n"
156 "Cache-Control: no-cache\r\n"
157 "Connection: close\r\n"
158 "Content-Type: text/html\r\n"
159 "\r\n"
160 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
161
Willy Tarreau0f772532006-12-23 20:51:41 +0100162 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100163 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100164 "Cache-Control: no-cache\r\n"
165 "Connection: close\r\n"
166 "Content-Type: text/html\r\n"
167 "\r\n"
168 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
169
170 [HTTP_ERR_403] =
171 "HTTP/1.0 403 Forbidden\r\n"
172 "Cache-Control: no-cache\r\n"
173 "Connection: close\r\n"
174 "Content-Type: text/html\r\n"
175 "\r\n"
176 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
177
CJ Ess108b1dd2015-04-07 12:03:37 -0400178 [HTTP_ERR_405] =
179 "HTTP/1.0 405 Method Not Allowed\r\n"
180 "Cache-Control: no-cache\r\n"
181 "Connection: close\r\n"
182 "Content-Type: text/html\r\n"
183 "\r\n"
184 "<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",
185
Willy Tarreau0f772532006-12-23 20:51:41 +0100186 [HTTP_ERR_408] =
187 "HTTP/1.0 408 Request Time-out\r\n"
188 "Cache-Control: no-cache\r\n"
189 "Connection: close\r\n"
190 "Content-Type: text/html\r\n"
191 "\r\n"
192 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
193
Olivier Houchard51a76d82017-10-02 16:12:07 +0200194 [HTTP_ERR_425] =
195 "HTTP/1.0 425 Too Early\r\n"
196 "Cache-Control: no-cache\r\n"
197 "Connection: close\r\n"
198 "Content-Type: text/html\r\n"
199 "\r\n"
200 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
201
CJ Ess108b1dd2015-04-07 12:03:37 -0400202 [HTTP_ERR_429] =
203 "HTTP/1.0 429 Too Many Requests\r\n"
204 "Cache-Control: no-cache\r\n"
205 "Connection: close\r\n"
206 "Content-Type: text/html\r\n"
207 "\r\n"
208 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
209
Willy Tarreau0f772532006-12-23 20:51:41 +0100210 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200211 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100212 "Cache-Control: no-cache\r\n"
213 "Connection: close\r\n"
214 "Content-Type: text/html\r\n"
215 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200216 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100217
218 [HTTP_ERR_502] =
219 "HTTP/1.0 502 Bad Gateway\r\n"
220 "Cache-Control: no-cache\r\n"
221 "Connection: close\r\n"
222 "Content-Type: text/html\r\n"
223 "\r\n"
224 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
225
226 [HTTP_ERR_503] =
227 "HTTP/1.0 503 Service Unavailable\r\n"
228 "Cache-Control: no-cache\r\n"
229 "Connection: close\r\n"
230 "Content-Type: text/html\r\n"
231 "\r\n"
232 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
233
234 [HTTP_ERR_504] =
235 "HTTP/1.0 504 Gateway Time-out\r\n"
236 "Cache-Control: no-cache\r\n"
237 "Connection: close\r\n"
238 "Content-Type: text/html\r\n"
239 "\r\n"
240 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
241
242};
243
Cyril Bonté19979e12012-04-04 12:57:21 +0200244/* status codes available for the stats admin page (strictly 4 chars length) */
245const char *stat_status_codes[STAT_STATUS_SIZE] = {
246 [STAT_STATUS_DENY] = "DENY",
247 [STAT_STATUS_DONE] = "DONE",
248 [STAT_STATUS_ERRP] = "ERRP",
249 [STAT_STATUS_EXCD] = "EXCD",
250 [STAT_STATUS_NONE] = "NONE",
251 [STAT_STATUS_PART] = "PART",
252 [STAT_STATUS_UNKN] = "UNKN",
253};
254
255
William Lallemand73025dd2014-04-24 14:38:37 +0200256/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200257struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200258 .list = LIST_HEAD_INIT(http_req_keywords.list)
259};
260
261/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200262struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200263 .list = LIST_HEAD_INIT(http_res_keywords.list)
264};
265
Willy Tarreau80587432006-12-24 17:47:20 +0100266/* We must put the messages here since GCC cannot initialize consts depending
267 * on strlen().
268 */
269struct chunk http_err_chunks[HTTP_ERR_SIZE];
270
Willy Tarreaua890d072013-04-02 12:01:06 +0200271/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100272static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200273
Willy Tarreau42250582007-04-01 01:30:43 +0200274#define FD_SETS_ARE_BITFIELDS
275#ifdef FD_SETS_ARE_BITFIELDS
276/*
277 * This map is used with all the FD_* macros to check whether a particular bit
278 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
279 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
280 * byte should be encoded. Be careful to always pass bytes from 0 to 255
281 * exclusively to the macros.
282 */
283fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
284fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100285fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200286
287#else
288#error "Check if your OS uses bitfields for fd_sets"
289#endif
290
Willy Tarreau87b09662015-04-03 00:22:06 +0200291static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200292
David Carlier7365f7d2016-04-04 11:54:42 +0100293static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
294static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100295
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200296/* This function returns a reason associated with the HTTP status.
297 * This function never fails, a message is always returned.
298 */
299const char *get_reason(unsigned int status)
300{
301 switch (status) {
302 case 100: return "Continue";
303 case 101: return "Switching Protocols";
304 case 102: return "Processing";
305 case 200: return "OK";
306 case 201: return "Created";
307 case 202: return "Accepted";
308 case 203: return "Non-Authoritative Information";
309 case 204: return "No Content";
310 case 205: return "Reset Content";
311 case 206: return "Partial Content";
312 case 207: return "Multi-Status";
313 case 210: return "Content Different";
314 case 226: return "IM Used";
315 case 300: return "Multiple Choices";
316 case 301: return "Moved Permanently";
317 case 302: return "Moved Temporarily";
318 case 303: return "See Other";
319 case 304: return "Not Modified";
320 case 305: return "Use Proxy";
321 case 307: return "Temporary Redirect";
322 case 308: return "Permanent Redirect";
323 case 310: return "Too many Redirects";
324 case 400: return "Bad Request";
325 case 401: return "Unauthorized";
326 case 402: return "Payment Required";
327 case 403: return "Forbidden";
328 case 404: return "Not Found";
329 case 405: return "Method Not Allowed";
330 case 406: return "Not Acceptable";
331 case 407: return "Proxy Authentication Required";
332 case 408: return "Request Time-out";
333 case 409: return "Conflict";
334 case 410: return "Gone";
335 case 411: return "Length Required";
336 case 412: return "Precondition Failed";
337 case 413: return "Request Entity Too Large";
338 case 414: return "Request-URI Too Long";
339 case 415: return "Unsupported Media Type";
340 case 416: return "Requested range unsatisfiable";
341 case 417: return "Expectation failed";
342 case 418: return "I'm a teapot";
343 case 422: return "Unprocessable entity";
344 case 423: return "Locked";
345 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200346 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200347 case 426: return "Upgrade Required";
348 case 428: return "Precondition Required";
349 case 429: return "Too Many Requests";
350 case 431: return "Request Header Fields Too Large";
351 case 449: return "Retry With";
352 case 450: return "Blocked by Windows Parental Controls";
353 case 451: return "Unavailable For Legal Reasons";
354 case 456: return "Unrecoverable Error";
355 case 499: return "client has closed connection";
356 case 500: return "Internal Server Error";
357 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200358 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200359 case 503: return "Service Unavailable";
360 case 504: return "Gateway Time-out";
361 case 505: return "HTTP Version not supported";
362 case 506: return "Variant also negociate";
363 case 507: return "Insufficient storage";
364 case 508: return "Loop detected";
365 case 509: return "Bandwidth Limit Exceeded";
366 case 510: return "Not extended";
367 case 511: return "Network authentication required";
368 case 520: return "Web server is returning an unknown error";
369 default:
370 switch (status) {
371 case 100 ... 199: return "Informational";
372 case 200 ... 299: return "Success";
373 case 300 ... 399: return "Redirection";
374 case 400 ... 499: return "Client Error";
375 case 500 ... 599: return "Server Error";
376 default: return "Other";
377 }
378 }
379}
380
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200381/* This function returns HTTP_ERR_<num> (enum) matching http status code.
382 * Returned value should match codes from http_err_codes.
383 */
384static const int http_get_status_idx(unsigned int status)
385{
386 switch (status) {
387 case 200: return HTTP_ERR_200;
388 case 400: return HTTP_ERR_400;
389 case 403: return HTTP_ERR_403;
390 case 405: return HTTP_ERR_405;
391 case 408: return HTTP_ERR_408;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200392 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200393 case 429: return HTTP_ERR_429;
394 case 500: return HTTP_ERR_500;
395 case 502: return HTTP_ERR_502;
396 case 503: return HTTP_ERR_503;
397 case 504: return HTTP_ERR_504;
398 default: return HTTP_ERR_500;
399 }
400}
401
Willy Tarreau80587432006-12-24 17:47:20 +0100402void init_proto_http()
403{
Willy Tarreau42250582007-04-01 01:30:43 +0200404 int i;
405 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100406 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200407
Willy Tarreau80587432006-12-24 17:47:20 +0100408 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
409 if (!http_err_msgs[msg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100410 ha_alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
Willy Tarreau80587432006-12-24 17:47:20 +0100411 abort();
412 }
413
414 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
415 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
416 }
Willy Tarreau42250582007-04-01 01:30:43 +0200417
418 /* initialize the log header encoding map : '{|}"#' should be encoded with
419 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
420 * URL encoding only requires '"', '#' to be encoded as well as non-
421 * printable characters above.
422 */
423 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
424 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100425 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200426 for (i = 0; i < 32; i++) {
427 FD_SET(i, hdr_encode_map);
428 FD_SET(i, url_encode_map);
429 }
430 for (i = 127; i < 256; i++) {
431 FD_SET(i, hdr_encode_map);
432 FD_SET(i, url_encode_map);
433 }
434
435 tmp = "\"#{|}";
436 while (*tmp) {
437 FD_SET(*tmp, hdr_encode_map);
438 tmp++;
439 }
440
441 tmp = "\"#";
442 while (*tmp) {
443 FD_SET(*tmp, url_encode_map);
444 tmp++;
445 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200446
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100447 /* initialize the http header encoding map. The draft httpbis define the
448 * header content as:
449 *
450 * HTTP-message = start-line
451 * *( header-field CRLF )
452 * CRLF
453 * [ message-body ]
454 * header-field = field-name ":" OWS field-value OWS
455 * field-value = *( field-content / obs-fold )
456 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
457 * obs-fold = CRLF 1*( SP / HTAB )
458 * field-vchar = VCHAR / obs-text
459 * VCHAR = %x21-7E
460 * obs-text = %x80-FF
461 *
462 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
463 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
464 * "obs-fold" is volontary forgotten because haproxy remove this.
465 */
466 memset(http_encode_map, 0, sizeof(http_encode_map));
467 for (i = 0x00; i <= 0x08; i++)
468 FD_SET(i, http_encode_map);
469 for (i = 0x0a; i <= 0x1f; i++)
470 FD_SET(i, http_encode_map);
471 FD_SET(0x7f, http_encode_map);
472
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200473 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100474 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
475 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100476}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477
Willy Tarreau53b6c742006-12-17 13:37:46 +0100478/*
479 * We have 26 list of methods (1 per first letter), each of which can have
480 * up to 3 entries (2 valid, 1 null).
481 */
482struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100483 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100484 int len;
485 const char text[8];
486};
487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100488const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100489 ['C' - 'A'] = {
490 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
491 },
492 ['D' - 'A'] = {
493 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
494 },
495 ['G' - 'A'] = {
496 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
497 },
498 ['H' - 'A'] = {
499 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
500 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200501 ['O' - 'A'] = {
502 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
503 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100504 ['P' - 'A'] = {
505 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
506 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
507 },
508 ['T' - 'A'] = {
509 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
510 },
511 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200512 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100513 */
514};
515
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100516const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100517 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
518 [HTTP_METH_GET] = { "GET", 3 },
519 [HTTP_METH_HEAD] = { "HEAD", 4 },
520 [HTTP_METH_POST] = { "POST", 4 },
521 [HTTP_METH_PUT] = { "PUT", 3 },
522 [HTTP_METH_DELETE] = { "DELETE", 6 },
523 [HTTP_METH_TRACE] = { "TRACE", 5 },
524 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
525};
526
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100527/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100528 * Adds a header and its CRLF at the tail of the message's buffer, just before
529 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100530 * The header is also automatically added to the index <hdr_idx>, and the end
531 * of headers is automatically adjusted. The number of bytes added is returned
532 * on success, otherwise <0 is returned indicating an error.
533 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100534int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100535{
536 int bytes, len;
537
538 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200539 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100540 if (!bytes)
541 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100542 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100543 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
544}
545
546/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100547 * Adds a header and its CRLF at the tail of the message's buffer, just before
548 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100549 * the buffer is only opened and the space reserved, but nothing is copied.
550 * The header is also automatically added to the index <hdr_idx>, and the end
551 * of headers is automatically adjusted. The number of bytes added is returned
552 * on success, otherwise <0 is returned indicating an error.
553 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100554int http_header_add_tail2(struct http_msg *msg,
555 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100556{
557 int bytes;
558
Willy Tarreau9b28e032012-10-12 23:49:43 +0200559 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100560 if (!bytes)
561 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100562 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
564}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565
566/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100567 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
568 * If so, returns the position of the first non-space character relative to
569 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
570 * to return a pointer to the place after the first space. Returns 0 if the
571 * header name does not match. Checks are case-insensitive.
572 */
573int http_header_match2(const char *hdr, const char *end,
574 const char *name, int len)
575{
576 const char *val;
577
578 if (hdr + len >= end)
579 return 0;
580 if (hdr[len] != ':')
581 return 0;
582 if (strncasecmp(hdr, name, len) != 0)
583 return 0;
584 val = hdr + len + 1;
585 while (val < end && HTTP_IS_SPHT(*val))
586 val++;
587 if ((val >= end) && (len + 2 <= end - hdr))
588 return len + 2; /* we may replace starting from second space */
589 return val - hdr;
590}
591
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200592/* Find the first or next occurrence of header <name> in message buffer <sol>
593 * using headers index <idx>, and return it in the <ctx> structure. This
594 * structure holds everything necessary to use the header and find next
595 * occurrence. If its <idx> member is 0, the header is searched from the
596 * beginning. Otherwise, the next occurrence is returned. The function returns
597 * 1 when it finds a value, and 0 when there is no more. It is very similar to
598 * http_find_header2() except that it is designed to work with full-line headers
599 * whose comma is not a delimiter but is part of the syntax. As a special case,
600 * if ctx->val is NULL when searching for a new values of a header, the current
601 * header is rescanned. This allows rescanning after a header deletion.
602 */
603int http_find_full_header2(const char *name, int len,
604 char *sol, struct hdr_idx *idx,
605 struct hdr_ctx *ctx)
606{
607 char *eol, *sov;
608 int cur_idx, old_idx;
609
610 cur_idx = ctx->idx;
611 if (cur_idx) {
612 /* We have previously returned a header, let's search another one */
613 sol = ctx->line;
614 eol = sol + idx->v[cur_idx].len;
615 goto next_hdr;
616 }
617
618 /* first request for this header */
619 sol += hdr_idx_first_pos(idx);
620 old_idx = 0;
621 cur_idx = hdr_idx_first_idx(idx);
622 while (cur_idx) {
623 eol = sol + idx->v[cur_idx].len;
624
625 if (len == 0) {
626 /* No argument was passed, we want any header.
627 * To achieve this, we simply build a fake request. */
628 while (sol + len < eol && sol[len] != ':')
629 len++;
630 name = sol;
631 }
632
633 if ((len < eol - sol) &&
634 (sol[len] == ':') &&
635 (strncasecmp(sol, name, len) == 0)) {
636 ctx->del = len;
637 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100638 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200639 sov++;
640
641 ctx->line = sol;
642 ctx->prev = old_idx;
643 ctx->idx = cur_idx;
644 ctx->val = sov - sol;
645 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100646 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200647 eol--;
648 ctx->tws++;
649 }
650 ctx->vlen = eol - sov;
651 return 1;
652 }
653 next_hdr:
654 sol = eol + idx->v[cur_idx].cr + 1;
655 old_idx = cur_idx;
656 cur_idx = idx->v[cur_idx].next;
657 }
658 return 0;
659}
660
Willy Tarreauc90dc232015-02-20 13:51:36 +0100661/* Find the first or next header field in message buffer <sol> using headers
662 * index <idx>, and return it in the <ctx> structure. This structure holds
663 * everything necessary to use the header and find next occurrence. If its
664 * <idx> member is 0, the first header is retrieved. Otherwise, the next
665 * occurrence is returned. The function returns 1 when it finds a value, and
666 * 0 when there is no more. It is equivalent to http_find_full_header2() with
667 * no header name.
668 */
669int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
670{
671 char *eol, *sov;
672 int cur_idx, old_idx;
673 int len;
674
675 cur_idx = ctx->idx;
676 if (cur_idx) {
677 /* We have previously returned a header, let's search another one */
678 sol = ctx->line;
679 eol = sol + idx->v[cur_idx].len;
680 goto next_hdr;
681 }
682
683 /* first request for this header */
684 sol += hdr_idx_first_pos(idx);
685 old_idx = 0;
686 cur_idx = hdr_idx_first_idx(idx);
687 while (cur_idx) {
688 eol = sol + idx->v[cur_idx].len;
689
690 len = 0;
691 while (1) {
692 if (len >= eol - sol)
693 goto next_hdr;
694 if (sol[len] == ':')
695 break;
696 len++;
697 }
698
699 ctx->del = len;
700 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100701 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100702 sov++;
703
704 ctx->line = sol;
705 ctx->prev = old_idx;
706 ctx->idx = cur_idx;
707 ctx->val = sov - sol;
708 ctx->tws = 0;
709
Willy Tarreau2235b262016-11-05 15:50:20 +0100710 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100711 eol--;
712 ctx->tws++;
713 }
714 ctx->vlen = eol - sov;
715 return 1;
716
717 next_hdr:
718 sol = eol + idx->v[cur_idx].cr + 1;
719 old_idx = cur_idx;
720 cur_idx = idx->v[cur_idx].next;
721 }
722 return 0;
723}
724
Lukas Tribus23953682017-04-28 13:24:30 +0000725/* Find the end of the header value contained between <s> and <e>. See RFC7230,
726 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100729char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200730{
731 int quoted, qdpair;
732
733 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100734
735#if defined(__x86_64__) || \
736 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
737 defined(__ARM_ARCH_7A__)
738 /* speedup: skip everything not a comma nor a double quote */
739 for (; s <= e - sizeof(int); s += sizeof(int)) {
740 unsigned int c = *(int *)s; // comma
741 unsigned int q = c; // quote
742
743 c ^= 0x2c2c2c2c; // contains one zero on a comma
744 q ^= 0x22222222; // contains one zero on a quote
745
746 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
747 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
748
749 if ((c | q) & 0x80808080)
750 break; // found a comma or a quote
751 }
752#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200753 for (; s < e; s++) {
754 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200755 else if (quoted) {
756 if (*s == '\\') qdpair = 1;
757 else if (*s == '"') quoted = 0;
758 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200759 else if (*s == '"') quoted = 1;
760 else if (*s == ',') return s;
761 }
762 return s;
763}
764
765/* Find the first or next occurrence of header <name> in message buffer <sol>
766 * using headers index <idx>, and return it in the <ctx> structure. This
767 * structure holds everything necessary to use the header and find next
768 * occurrence. If its <idx> member is 0, the header is searched from the
769 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100770 * 1 when it finds a value, and 0 when there is no more. It is designed to work
771 * with headers defined as comma-separated lists. As a special case, if ctx->val
772 * is NULL when searching for a new values of a header, the current header is
773 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200774 */
775int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100776 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200777 struct hdr_ctx *ctx)
778{
Willy Tarreau68085d82010-01-18 14:54:04 +0100779 char *eol, *sov;
780 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200781
Willy Tarreau68085d82010-01-18 14:54:04 +0100782 cur_idx = ctx->idx;
783 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200784 /* We have previously returned a value, let's search
785 * another one on the same line.
786 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200787 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200788 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100789 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200790 eol = sol + idx->v[cur_idx].len;
791
792 if (sov >= eol)
793 /* no more values in this header */
794 goto next_hdr;
795
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 /* values remaining for this header, skip the comma but save it
797 * for later use (eg: for header deletion).
798 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200799 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100800 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 sov++;
802
803 goto return_hdr;
804 }
805
806 /* first request for this header */
807 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200809 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200810 while (cur_idx) {
811 eol = sol + idx->v[cur_idx].len;
812
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200813 if (len == 0) {
814 /* No argument was passed, we want any header.
815 * To achieve this, we simply build a fake request. */
816 while (sol + len < eol && sol[len] != ':')
817 len++;
818 name = sol;
819 }
820
Willy Tarreau33a7e692007-06-10 19:45:56 +0200821 if ((len < eol - sol) &&
822 (sol[len] == ':') &&
823 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100824 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200825 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100826 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100828
Willy Tarreau33a7e692007-06-10 19:45:56 +0200829 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100830 ctx->prev = old_idx;
831 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200832 ctx->idx = cur_idx;
833 ctx->val = sov - sol;
834
835 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200836 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100837 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200838 eol--;
839 ctx->tws++;
840 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200841 ctx->vlen = eol - sov;
842 return 1;
843 }
844 next_hdr:
845 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100846 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200847 cur_idx = idx->v[cur_idx].next;
848 }
849 return 0;
850}
851
852int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100853 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200854 struct hdr_ctx *ctx)
855{
856 return http_find_header2(name, strlen(name), sol, idx, ctx);
857}
858
Willy Tarreau68085d82010-01-18 14:54:04 +0100859/* Remove one value of a header. This only works on a <ctx> returned by one of
860 * the http_find_header functions. The value is removed, as well as surrounding
861 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100862 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100863 * message <msg>. The new index is returned. If it is zero, it means there is
864 * no more header, so any processing may stop. The ctx is always left in a form
865 * that can be handled by http_find_header2() to find next occurrence.
866 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100867int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100868{
869 int cur_idx = ctx->idx;
870 char *sol = ctx->line;
871 struct hdr_idx_elem *hdr;
872 int delta, skip_comma;
873
874 if (!cur_idx)
875 return 0;
876
877 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200878 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100879 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200880 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100881 http_msg_move_end(msg, delta);
882 idx->used--;
883 hdr->len = 0; /* unused entry */
884 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100885 if (idx->tail == ctx->idx)
886 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100887 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100888 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100889 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200890 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100891 return ctx->idx;
892 }
893
894 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200895 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
896 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100897 */
898
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200899 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200900 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200901 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100902 NULL, 0);
903 hdr->len += delta;
904 http_msg_move_end(msg, delta);
905 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200906 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100907 return ctx->idx;
908}
909
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100910/* This function handles a server error at the stream interface level. The
911 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100912 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100913 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100914 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200916static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100917 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918{
Willy Tarreau2019f952017-03-14 11:07:31 +0100919 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100920 channel_auto_read(si_oc(si));
921 channel_abort(si_oc(si));
922 channel_auto_close(si_oc(si));
923 channel_erase(si_oc(si));
924 channel_auto_close(si_ic(si));
925 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200926 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200927 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200928 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200929 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200930 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200931 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932}
933
Willy Tarreau87b09662015-04-03 00:22:06 +0200934/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100935 * and message.
936 */
937
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200938struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100939{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200940 const int msgnum = http_get_status_idx(s->txn->status);
941
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200942 if (s->be->errmsg[msgnum].str)
943 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200944 else if (strm_fe(s)->errmsg[msgnum].str)
945 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100946 else
947 return &http_err_chunks[msgnum];
948}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100950void
951http_reply_and_close(struct stream *s, short status, struct chunk *msg)
952{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200953 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100954 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100955 stream_int_retnclose(&s->si[0], msg);
956}
957
Willy Tarreau53b6c742006-12-17 13:37:46 +0100958/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200959 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
960 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100961 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100962enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100963{
964 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100965 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966
967 m = ((unsigned)*str - 'A');
968
969 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100970 for (h = http_methods[m]; h->len > 0; h++) {
971 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100975 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100976 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200977 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100978}
979
Willy Tarreau21d2af32008-02-14 20:25:24 +0100980/* Parse the URI from the given transaction (which is assumed to be in request
981 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
982 * It is returned otherwise.
983 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200984char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100985{
986 char *ptr, *end;
987
Willy Tarreau9b28e032012-10-12 23:49:43 +0200988 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100989 end = ptr + txn->req.sl.rq.u_l;
990
991 if (ptr >= end)
992 return NULL;
993
Lukas Tribus23953682017-04-28 13:24:30 +0000994 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100995 * Request-URI = "*" | absuri | abspath | authority
996 */
997
998 if (*ptr == '*')
999 return NULL;
1000
1001 if (isalpha((unsigned char)*ptr)) {
1002 /* this is a scheme as described by RFC3986, par. 3.1 */
1003 ptr++;
1004 while (ptr < end &&
1005 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1006 ptr++;
1007 /* skip '://' */
1008 if (ptr == end || *ptr++ != ':')
1009 return NULL;
1010 if (ptr == end || *ptr++ != '/')
1011 return NULL;
1012 if (ptr == end || *ptr++ != '/')
1013 return NULL;
1014 }
1015 /* skip [user[:passwd]@]host[:[port]] */
1016
1017 while (ptr < end && *ptr != '/')
1018 ptr++;
1019
1020 if (ptr == end)
1021 return NULL;
1022
1023 /* OK, we got the '/' ! */
1024 return ptr;
1025}
1026
William Lallemand65ad6e12014-01-31 15:08:02 +01001027/* Parse the URI from the given string and look for the "/" beginning the PATH.
1028 * If not found, return NULL. It is returned otherwise.
1029 */
1030static char *
1031http_get_path_from_string(char *str)
1032{
1033 char *ptr = str;
1034
1035 /* RFC2616, par. 5.1.2 :
1036 * Request-URI = "*" | absuri | abspath | authority
1037 */
1038
1039 if (*ptr == '*')
1040 return NULL;
1041
1042 if (isalpha((unsigned char)*ptr)) {
1043 /* this is a scheme as described by RFC3986, par. 3.1 */
1044 ptr++;
1045 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1046 ptr++;
1047 /* skip '://' */
1048 if (*ptr == '\0' || *ptr++ != ':')
1049 return NULL;
1050 if (*ptr == '\0' || *ptr++ != '/')
1051 return NULL;
1052 if (*ptr == '\0' || *ptr++ != '/')
1053 return NULL;
1054 }
1055 /* skip [user[:passwd]@]host[:[port]] */
1056
1057 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1058 ptr++;
1059
1060 if (*ptr == '\0' || *ptr == ' ')
1061 return NULL;
1062
1063 /* OK, we got the '/' ! */
1064 return ptr;
1065}
1066
Willy Tarreau71241ab2012-12-27 11:30:54 +01001067/* Returns a 302 for a redirectable request that reaches a server working in
1068 * in redirect mode. This may only be called just after the stream interface
1069 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1070 * follow normal proxy processing. NOTE: this function is designed to support
1071 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001072 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001073void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074{
1075 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001076 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001077 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001078 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079
1080 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001081 trash.len = strlen(HTTP_302);
1082 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001084 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001085
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001087 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088 return;
1089
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001090 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001091 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001092 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1093 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001094 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001095
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001096 /* 3: add the request URI. Since it was already forwarded, we need
1097 * to temporarily rewind the buffer.
1098 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001099 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001100 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001101
Willy Tarreauefb453c2008-10-26 20:49:47 +01001102 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001103 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 +02001104
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001105 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001106
Willy Tarreauefb453c2008-10-26 20:49:47 +01001107 if (!path)
1108 return;
1109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001110 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001111 return;
1112
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001113 memcpy(trash.str + trash.len, path, len);
1114 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001115
1116 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001117 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1118 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001119 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001120 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1121 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001122 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001123
1124 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001125 si_shutr(si);
1126 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001127 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001128 si->state = SI_ST_CLO;
1129
1130 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001131 txn->status = 302;
1132 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133
1134 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001135 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001136 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137}
1138
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001139/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001140 * that the server side is closed. Note that err_type is actually a
1141 * bitmask, where almost only aborts may be cumulated with other
1142 * values. We consider that aborted operations are more important
1143 * than timeouts or errors due to the fact that nobody else in the
1144 * logs might explain incomplete retries. All others should avoid
1145 * being cumulated. It should normally not be possible to have multiple
1146 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001147 * Note that connection errors appearing on the second request of a keep-alive
1148 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001149 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001150void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001151{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001152 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001153
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001154 /* set s->txn->status for http_error_message(s) */
1155 s->txn->status = 503;
1156
Willy Tarreauefb453c2008-10-26 20:49:47 +01001157 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001159 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001160 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001161 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001162 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001163 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001164 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001165 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001166 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001167 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001169 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001170 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001171 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001172 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001173 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001174 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001176 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001177 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001178 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001180 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001181 http_error_message(s));
1182 else { /* SI_ET_CONN_OTHER and others */
1183 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001185 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001186 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001187}
1188
Willy Tarreau42250582007-04-01 01:30:43 +02001189extern const char sess_term_cond[8];
1190extern const char sess_fin_state[8];
1191extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +01001192struct pool_head *pool_head_http_txn;
1193struct pool_head *pool_head_requri;
1194struct pool_head *pool_head_capture = NULL;
1195struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196
Willy Tarreau117f59e2007-03-04 18:17:17 +01001197/*
1198 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001199 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001200 */
1201void capture_headers(char *som, struct hdr_idx *idx,
1202 char **cap, struct cap_hdr *cap_hdr)
1203{
1204 char *eol, *sol, *col, *sov;
1205 int cur_idx;
1206 struct cap_hdr *h;
1207 int len;
1208
1209 sol = som + hdr_idx_first_pos(idx);
1210 cur_idx = hdr_idx_first_idx(idx);
1211
1212 while (cur_idx) {
1213 eol = sol + idx->v[cur_idx].len;
1214
1215 col = sol;
1216 while (col < eol && *col != ':')
1217 col++;
1218
1219 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001220 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001221 sov++;
1222
1223 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001224 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001225 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1226 if (cap[h->index] == NULL)
1227 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +01001228 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001229
1230 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001231 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +01001232 continue;
1233 }
1234
1235 len = eol - sov;
1236 if (len > h->len)
1237 len = h->len;
1238
1239 memcpy(cap[h->index], sov, len);
1240 cap[h->index][len]=0;
1241 }
1242 }
1243 sol = eol + idx->v[cur_idx].cr + 1;
1244 cur_idx = idx->v[cur_idx].next;
1245 }
1246}
1247
Willy Tarreaubaaee002006-06-26 02:48:02 +02001248/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001249 * Returns the data from Authorization header. Function may be called more
1250 * than once so data is stored in txn->auth_data. When no header is found
1251 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001252 * searching again for something we are unable to find anyway. However, if
1253 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001254 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001255 */
1256
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001257int
Willy Tarreau87b09662015-04-03 00:22:06 +02001258get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001259{
1260
Willy Tarreaueee5b512015-04-03 23:46:31 +02001261 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001262 struct chunk auth_method;
1263 struct hdr_ctx ctx;
1264 char *h, *p;
1265 int len;
1266
1267#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001268 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001269#endif
1270
1271 if (txn->auth.method == HTTP_AUTH_WRONG)
1272 return 0;
1273
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001274 txn->auth.method = HTTP_AUTH_WRONG;
1275
1276 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001277
1278 if (txn->flags & TX_USE_PX_CONN) {
1279 h = "Proxy-Authorization";
1280 len = strlen(h);
1281 } else {
1282 h = "Authorization";
1283 len = strlen(h);
1284 }
1285
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001286 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001287 return 0;
1288
1289 h = ctx.line + ctx.val;
1290
1291 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001292 len = p - h;
1293 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001294 return 0;
1295
David Carlier7365f7d2016-04-04 11:54:42 +01001296 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1297 return 0;
1298
Willy Tarreau5c557d12016-03-13 08:17:02 +01001299 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001300
1301 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001302 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001303
1304 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001305 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001306
1307 if (len < 0)
1308 return 0;
1309
1310
Christopher Faulet6988f672017-07-27 15:18:52 +02001311 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001312
Christopher Faulet6988f672017-07-27 15:18:52 +02001313 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001314
1315 if (!p)
1316 return 0;
1317
Christopher Faulet6988f672017-07-27 15:18:52 +02001318 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001319 *p = '\0';
1320 txn->auth.pass = p+1;
1321
1322 txn->auth.method = HTTP_AUTH_BASIC;
1323 return 1;
1324 }
1325
1326 return 0;
1327}
1328
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001329
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001330/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1331 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1332 * nothing is done and 1 is returned.
1333 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001334static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001335{
1336 int delta;
1337 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001338 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001339
1340 if (msg->sl.rq.v_l != 0)
1341 return 1;
1342
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001343 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1344 if (txn->meth != HTTP_METH_GET)
1345 return 0;
1346
Willy Tarreau9b28e032012-10-12 23:49:43 +02001347 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001348
1349 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001350 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1351 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001352 }
1353 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001354 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001355 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001356 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001357 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001358 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001359 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001360 NULL, NULL);
1361 if (unlikely(!cur_end))
1362 return 0;
1363
1364 /* we have a full HTTP/1.0 request now and we know that
1365 * we have either a CR or an LF at <ptr>.
1366 */
1367 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1368 return 1;
1369}
1370
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001371/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001372 * and "keep-alive" values. If we already know that some headers may safely
1373 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001374 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1375 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001376 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001377 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1378 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1379 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001380 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001381 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001382void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001383{
Willy Tarreau5b154472009-12-21 20:11:07 +01001384 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001385 const char *hdr_val = "Connection";
1386 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001387
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001388 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001389 return;
1390
Willy Tarreau88d349d2010-01-25 12:15:43 +01001391 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1392 hdr_val = "Proxy-Connection";
1393 hdr_len = 16;
1394 }
1395
Willy Tarreau5b154472009-12-21 20:11:07 +01001396 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001397 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001398 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001399 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1400 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001401 if (to_del & 2)
1402 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001403 else
1404 txn->flags |= TX_CON_KAL_SET;
1405 }
1406 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1407 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001408 if (to_del & 1)
1409 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001410 else
1411 txn->flags |= TX_CON_CLO_SET;
1412 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001413 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1414 txn->flags |= TX_HDR_CONN_UPG;
1415 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001416 }
1417
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001418 txn->flags |= TX_HDR_CONN_PRS;
1419 return;
1420}
Willy Tarreau5b154472009-12-21 20:11:07 +01001421
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001422/* Apply desired changes on the Connection: header. Values may be removed and/or
1423 * added depending on the <wanted> flags, which are exclusively composed of
1424 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1425 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1426 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001427void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001428{
1429 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001430 const char *hdr_val = "Connection";
1431 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001432
1433 ctx.idx = 0;
1434
Willy Tarreau88d349d2010-01-25 12:15:43 +01001435
1436 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1437 hdr_val = "Proxy-Connection";
1438 hdr_len = 16;
1439 }
1440
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001441 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001442 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001443 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1444 if (wanted & TX_CON_KAL_SET)
1445 txn->flags |= TX_CON_KAL_SET;
1446 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001447 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001448 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001449 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1450 if (wanted & TX_CON_CLO_SET)
1451 txn->flags |= TX_CON_CLO_SET;
1452 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001453 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001454 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001455 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001456
1457 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1458 return;
1459
1460 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1461 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001462 hdr_val = "Connection: close";
1463 hdr_len = 17;
1464 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1465 hdr_val = "Proxy-Connection: close";
1466 hdr_len = 23;
1467 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001468 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001469 }
1470
1471 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1472 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001473 hdr_val = "Connection: keep-alive";
1474 hdr_len = 22;
1475 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1476 hdr_val = "Proxy-Connection: keep-alive";
1477 hdr_len = 28;
1478 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001479 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001480 }
1481 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001482}
1483
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001484/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1485 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1486 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1487 * Unparsable qvalues return 1000 as "q=1.000".
1488 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001489int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001490{
1491 int q = 1000;
1492
Willy Tarreau506c69a2014-07-08 00:59:48 +02001493 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001494 goto out;
1495 q = (*qvalue++ - '0') * 1000;
1496
1497 if (*qvalue++ != '.')
1498 goto out;
1499
Willy Tarreau506c69a2014-07-08 00:59:48 +02001500 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001501 goto out;
1502 q += (*qvalue++ - '0') * 100;
1503
Willy Tarreau506c69a2014-07-08 00:59:48 +02001504 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001505 goto out;
1506 q += (*qvalue++ - '0') * 10;
1507
Willy Tarreau506c69a2014-07-08 00:59:48 +02001508 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001509 goto out;
1510 q += (*qvalue++ - '0') * 1;
1511 out:
1512 if (q > 1000)
1513 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001514 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001515 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001516 return q;
1517}
William Lallemand82fe75c2012-10-23 10:25:10 +02001518
Willy Tarreau87b09662015-04-03 00:22:06 +02001519void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001520{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001521 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001522 int tmp = TX_CON_WANT_KAL;
1523
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001524 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1525 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001526 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1527 tmp = TX_CON_WANT_TUN;
1528
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001529 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001530 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1531 tmp = TX_CON_WANT_TUN;
1532 }
1533
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001534 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001535 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1536 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001537 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001538 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1539 tmp = TX_CON_WANT_CLO;
1540 else
1541 tmp = TX_CON_WANT_SCL;
1542 }
1543
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001544 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001545 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1546 tmp = TX_CON_WANT_CLO;
1547
1548 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1549 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1550
1551 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1552 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1553 /* parse the Connection header and possibly clean it */
1554 int to_del = 0;
1555 if ((msg->flags & HTTP_MSGF_VER_11) ||
1556 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001557 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001558 to_del |= 2; /* remove "keep-alive" */
1559 if (!(msg->flags & HTTP_MSGF_VER_11))
1560 to_del |= 1; /* remove "close" */
1561 http_parse_connection_header(txn, msg, to_del);
1562 }
1563
1564 /* check if client or config asks for explicit close in KAL/SCL */
1565 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1566 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1567 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1568 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1569 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001570 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001571 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1572}
William Lallemand82fe75c2012-10-23 10:25:10 +02001573
Willy Tarreaud787e662009-07-07 10:14:51 +02001574/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1575 * processing can continue on next analysers, or zero if it either needs more
1576 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001577 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001578 * when it has nothing left to do, and may remove any analyser when it wants to
1579 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001581int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582{
Willy Tarreau59234e92008-11-30 23:51:27 +01001583 /*
1584 * We will parse the partial (or complete) lines.
1585 * We will check the request syntax, and also join multi-line
1586 * headers. An index of all the lines will be elaborated while
1587 * parsing.
1588 *
1589 * For the parsing, we use a 28 states FSM.
1590 *
1591 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001592 * req->buf->p = beginning of request
1593 * req->buf->p + msg->eoh = end of processed headers / start of current one
1594 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001595 * msg->eol = end of current header or line (LF or CRLF)
1596 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001597 *
1598 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001599 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001600 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1601 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001602 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau59234e92008-11-30 23:51:27 +01001604 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001605 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001606 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001607 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001608 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau87b09662015-04-03 00:22:06 +02001610 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 +01001611 now_ms, __FUNCTION__,
1612 s,
1613 req,
1614 req->rex, req->wex,
1615 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001617 req->analysers);
1618
Willy Tarreau52a0c602009-08-16 22:45:38 +02001619 /* we're speaking HTTP here, so let's speak HTTP to the client */
1620 s->srv_error = http_return_srv_error;
1621
Willy Tarreau83e3af02009-12-28 17:39:57 +01001622 /* There's a protected area at the end of the buffer for rewriting
1623 * purposes. We don't want to start to parse the request if the
1624 * protected area is affected, because we may have to move processed
1625 * data later, which is much more complicated.
1626 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001627 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001628
1629 /* This point is executed when some data is avalaible for analysis,
1630 * so we log the end of the idle time. */
1631 if (s->logs.t_idle == -1)
1632 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1633
Willy Tarreau379357a2013-06-08 12:55:46 +02001634 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001635 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001636 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001637 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001638 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001639 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001640 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001641 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001642 return 0;
1643 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001644 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1645 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1646 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001647 }
1648
Willy Tarreau9b28e032012-10-12 23:49:43 +02001649 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001650 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001651 }
1652
Willy Tarreau59234e92008-11-30 23:51:27 +01001653 /* 1: we might have to print this header in debug mode */
1654 if (unlikely((global.mode & MODE_DEBUG) &&
1655 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001656 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001657 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001658
Willy Tarreau9b28e032012-10-12 23:49:43 +02001659 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001660 /* this is a bit complex : in case of error on the request line,
1661 * we know that rq.l is still zero, so we display only the part
1662 * up to the end of the line (truncated by debug_hdr).
1663 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001664 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001665 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001666
Willy Tarreau59234e92008-11-30 23:51:27 +01001667 sol += hdr_idx_first_pos(&txn->hdr_idx);
1668 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001669
Willy Tarreau59234e92008-11-30 23:51:27 +01001670 while (cur_idx) {
1671 eol = sol + txn->hdr_idx.v[cur_idx].len;
1672 debug_hdr("clihdr", s, sol, eol);
1673 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1674 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001676 }
1677
Willy Tarreau58f10d72006-12-04 02:26:12 +01001678
Willy Tarreau59234e92008-11-30 23:51:27 +01001679 /*
1680 * Now we quickly check if we have found a full valid request.
1681 * If not so, we check the FD and buffer states before leaving.
1682 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001683 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001684 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001685 * on a keep-alive stream, if we encounter and error, close, t/o,
1686 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001687 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001688 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001689 * Last, we may increase some tracked counters' http request errors on
1690 * the cases that are deliberately the client's fault. For instance,
1691 * a timeout or connection reset is not counted as an error. However
1692 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001693 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001694
Willy Tarreau655dce92009-11-08 13:10:58 +01001695 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001697 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001698 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001699 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001700 stream_inc_http_req_ctr(s);
1701 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001702 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001703 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001704 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001705
Willy Tarreau59234e92008-11-30 23:51:27 +01001706 /* 1: Since we are in header mode, if there's no space
1707 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001708 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001709 * must terminate it now.
1710 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001711 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001712 /* FIXME: check if URI is set and return Status
1713 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001714 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001715 stream_inc_http_req_ctr(s);
1716 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001717 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001718 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001719 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001720 goto return_bad_req;
1721 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001722
Willy Tarreau59234e92008-11-30 23:51:27 +01001723 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001724 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001725 if (!(s->flags & SF_ERR_MASK))
1726 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001727
Willy Tarreaufcffa692010-01-10 14:21:19 +01001728 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001729 goto failed_keep_alive;
1730
Willy Tarreau0f228a02015-05-01 15:37:53 +02001731 if (sess->fe->options & PR_O_IGNORE_PRB)
1732 goto failed_keep_alive;
1733
Willy Tarreau59234e92008-11-30 23:51:27 +01001734 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001735 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001736 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001737 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001738 }
1739
Willy Tarreaudc979f22012-12-04 10:39:01 +01001740 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001741 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001742 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001743 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001744 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001745 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001746 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001747 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001748 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001749 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001750
Willy Tarreaue7dff022015-04-03 01:14:29 +02001751 if (!(s->flags & SF_FINST_MASK))
1752 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001753 return 0;
1754 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001755
Willy Tarreau59234e92008-11-30 23:51:27 +01001756 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001757 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001758 if (!(s->flags & SF_ERR_MASK))
1759 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001760
Willy Tarreaufcffa692010-01-10 14:21:19 +01001761 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001762 goto failed_keep_alive;
1763
Willy Tarreau0f228a02015-05-01 15:37:53 +02001764 if (sess->fe->options & PR_O_IGNORE_PRB)
1765 goto failed_keep_alive;
1766
Willy Tarreau59234e92008-11-30 23:51:27 +01001767 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001768 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001769 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001770 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001771 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001772 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001773 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001775 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001776 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001777
Willy Tarreau87b09662015-04-03 00:22:06 +02001778 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001779 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001780 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001781 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001782 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001783
Willy Tarreaue7dff022015-04-03 01:14:29 +02001784 if (!(s->flags & SF_FINST_MASK))
1785 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001786 return 0;
1787 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001788
Willy Tarreau59234e92008-11-30 23:51:27 +01001789 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001790 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001791 if (!(s->flags & SF_ERR_MASK))
1792 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001793
Willy Tarreaufcffa692010-01-10 14:21:19 +01001794 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001795 goto failed_keep_alive;
1796
Willy Tarreau0f228a02015-05-01 15:37:53 +02001797 if (sess->fe->options & PR_O_IGNORE_PRB)
1798 goto failed_keep_alive;
1799
Willy Tarreau4076a152009-04-02 15:18:36 +02001800 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001801 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001803 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001804 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001805 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001806 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001807 stream_inc_http_err_ctr(s);
1808 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001809 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001810 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001811 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001812 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001813
Willy Tarreaue7dff022015-04-03 01:14:29 +02001814 if (!(s->flags & SF_FINST_MASK))
1815 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001816 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001817 }
1818
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001819 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001820 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001821 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001822#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001823 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1824 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001825 /* We need more data, we have to re-enable quick-ack in case we
1826 * previously disabled it, otherwise we might cause the client
1827 * to delay next data.
1828 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001829 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001830 }
1831#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001832
Willy Tarreaufcffa692010-01-10 14:21:19 +01001833 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1834 /* If the client starts to talk, let's fall back to
1835 * request timeout processing.
1836 */
1837 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001838 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001839 }
1840
Willy Tarreau59234e92008-11-30 23:51:27 +01001841 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001842 if (!tick_isset(req->analyse_exp)) {
1843 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1844 (txn->flags & TX_WAIT_NEXT_RQ) &&
1845 tick_isset(s->be->timeout.httpka))
1846 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1847 else
1848 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1849 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001850
Willy Tarreau59234e92008-11-30 23:51:27 +01001851 /* we're not ready yet */
1852 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001853
1854 failed_keep_alive:
1855 /* Here we process low-level errors for keep-alive requests. In
1856 * short, if the request is not the first one and it experiences
1857 * a timeout, read error or shutdown, we just silently close so
1858 * that the client can try again.
1859 */
1860 txn->status = 0;
1861 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001862 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001863 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001864 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001865 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001866 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001867 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001868 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001869
Willy Tarreaud787e662009-07-07 10:14:51 +02001870 /* OK now we have a complete HTTP request with indexed headers. Let's
1871 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001872 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001873 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001874 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001875 * byte after the last LF. msg->sov points to the first byte of data.
1876 * msg->eol cannot be trusted because it may have been left uninitialized
1877 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001878 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001879
Willy Tarreau87b09662015-04-03 00:22:06 +02001880 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001881 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001882
Willy Tarreaub16a5742010-01-10 14:46:16 +01001883 if (txn->flags & TX_WAIT_NEXT_RQ) {
1884 /* kill the pending keep-alive timeout */
1885 txn->flags &= ~TX_WAIT_NEXT_RQ;
1886 req->analyse_exp = TICK_ETERNITY;
1887 }
1888
1889
Willy Tarreaud787e662009-07-07 10:14:51 +02001890 /* Maybe we found in invalid header name while we were configured not
1891 * to block on that, so we have to capture it now.
1892 */
1893 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001894 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001895
Willy Tarreau59234e92008-11-30 23:51:27 +01001896 /*
1897 * 1: identify the method
1898 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001899 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001900
1901 /* we can make use of server redirect on GET and HEAD */
1902 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001903 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001904 else if (txn->meth == HTTP_METH_OTHER &&
1905 msg->sl.rq.m_l == 3 && memcmp(req->buf->p, "PRI", 3) == 0) {
1906 /* PRI is reserved for the HTTP/2 preface */
1907 msg->err_pos = 0;
1908 goto return_bad_req;
1909 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001910
Willy Tarreau59234e92008-11-30 23:51:27 +01001911 /*
1912 * 2: check if the URI matches the monitor_uri.
1913 * We have to do this for every request which gets in, because
1914 * the monitor-uri is defined by the frontend.
1915 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001916 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1917 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001918 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001919 sess->fe->monitor_uri,
1920 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001922 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001923 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001924 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001925
Willy Tarreaue7dff022015-04-03 01:14:29 +02001926 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001927 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001928
Willy Tarreau59234e92008-11-30 23:51:27 +01001929 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001930 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001931 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001932
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 ret = acl_pass(ret);
1934 if (cond->pol == ACL_COND_UNLESS)
1935 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001936
Willy Tarreau59234e92008-11-30 23:51:27 +01001937 if (ret) {
1938 /* we fail this request, let's return 503 service unavail */
1939 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001940 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001941 if (!(s->flags & SF_ERR_MASK))
1942 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001943 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001944 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001945 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001946
Willy Tarreau59234e92008-11-30 23:51:27 +01001947 /* nothing to fail, let's reply normaly */
1948 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001949 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001950 if (!(s->flags & SF_ERR_MASK))
1951 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001952 goto return_prx_cond;
1953 }
1954
1955 /*
1956 * 3: Maybe we have to copy the original REQURI for the logs ?
1957 * Note: we cannot log anymore if the request has been
1958 * classified as invalid.
1959 */
1960 if (unlikely(s->logs.logwait & LW_REQ)) {
1961 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001962 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001963 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001965 if (urilen >= global.tune.requri_len )
1966 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001967 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001968 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001970 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001971 s->do_log(s);
1972 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001973 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001975 }
Willy Tarreau06619262006-12-17 08:37:22 +01001976
Willy Tarreau91852eb2015-05-01 13:26:00 +02001977 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1978 * exactly one digit "." one digit. This check may be disabled using
1979 * option accept-invalid-http-request.
1980 */
1981 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1982 if (msg->sl.rq.v_l != 8) {
1983 msg->err_pos = msg->sl.rq.v;
1984 goto return_bad_req;
1985 }
1986
1987 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1988 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1989 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1990 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1991 msg->err_pos = msg->sl.rq.v + 4;
1992 goto return_bad_req;
1993 }
1994 }
Willy Tarreau13317662015-05-01 13:47:08 +02001995 else {
1996 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1997 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1998 goto return_bad_req;
1999 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002000
Willy Tarreau5b154472009-12-21 20:11:07 +01002001 /* ... and check if the request is HTTP/1.1 or above */
2002 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002003 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2004 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2005 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002006 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002007
2008 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002009 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 +01002010
Willy Tarreau88d349d2010-01-25 12:15:43 +01002011 /* if the frontend has "option http-use-proxy-header", we'll check if
2012 * we have what looks like a proxied connection instead of a connection,
2013 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2014 * Note that this is *not* RFC-compliant, however browsers and proxies
2015 * happen to do that despite being non-standard :-(
2016 * We consider that a request not beginning with either '/' or '*' is
2017 * a proxied connection, which covers both "scheme://location" and
2018 * CONNECT ip:port.
2019 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002020 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002021 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002022 txn->flags |= TX_USE_PX_CONN;
2023
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002024 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002025 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002026
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002028 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002029 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002030 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002031
Willy Tarreau557f1992015-05-01 10:05:17 +02002032 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2033 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002034 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002035 * The length of a message body is determined by one of the following
2036 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002037 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002038 * 1. Any response to a HEAD request and any response with a 1xx
2039 * (Informational), 204 (No Content), or 304 (Not Modified) status
2040 * code is always terminated by the first empty line after the
2041 * header fields, regardless of the header fields present in the
2042 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002043 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002044 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2045 * the connection will become a tunnel immediately after the empty
2046 * line that concludes the header fields. A client MUST ignore any
2047 * Content-Length or Transfer-Encoding header fields received in
2048 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002049 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002050 * 3. If a Transfer-Encoding header field is present and the chunked
2051 * transfer coding (Section 4.1) is the final encoding, the message
2052 * body length is determined by reading and decoding the chunked
2053 * data until the transfer coding indicates the data is complete.
2054 *
2055 * If a Transfer-Encoding header field is present in a response and
2056 * the chunked transfer coding is not the final encoding, the
2057 * message body length is determined by reading the connection until
2058 * it is closed by the server. If a Transfer-Encoding header field
2059 * is present in a request and the chunked transfer coding is not
2060 * the final encoding, the message body length cannot be determined
2061 * reliably; the server MUST respond with the 400 (Bad Request)
2062 * status code and then close the connection.
2063 *
2064 * If a message is received with both a Transfer-Encoding and a
2065 * Content-Length header field, the Transfer-Encoding overrides the
2066 * Content-Length. Such a message might indicate an attempt to
2067 * perform request smuggling (Section 9.5) or response splitting
2068 * (Section 9.4) and ought to be handled as an error. A sender MUST
2069 * remove the received Content-Length field prior to forwarding such
2070 * a message downstream.
2071 *
2072 * 4. If a message is received without Transfer-Encoding and with
2073 * either multiple Content-Length header fields having differing
2074 * field-values or a single Content-Length header field having an
2075 * invalid value, then the message framing is invalid and the
2076 * recipient MUST treat it as an unrecoverable error. If this is a
2077 * request message, the server MUST respond with a 400 (Bad Request)
2078 * status code and then close the connection. If this is a response
2079 * message received by a proxy, the proxy MUST close the connection
2080 * to the server, discard the received response, and send a 502 (Bad
2081 * Gateway) response to the client. If this is a response message
2082 * received by a user agent, the user agent MUST close the
2083 * connection to the server and discard the received response.
2084 *
2085 * 5. If a valid Content-Length header field is present without
2086 * Transfer-Encoding, its decimal value defines the expected message
2087 * body length in octets. If the sender closes the connection or
2088 * the recipient times out before the indicated number of octets are
2089 * received, the recipient MUST consider the message to be
2090 * incomplete and close the connection.
2091 *
2092 * 6. If this is a request message and none of the above are true, then
2093 * the message body length is zero (no message body is present).
2094 *
2095 * 7. Otherwise, this is a response message without a declared message
2096 * body length, so the message body length is determined by the
2097 * number of octets received prior to the server closing the
2098 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002099 */
2100
Willy Tarreau32b47f42009-10-18 20:55:02 +02002101 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002102 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002103 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002104 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002105 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002106 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002107 /* chunked not last, return badreq */
2108 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002109 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002110 }
2111
Willy Tarreau1c913912015-04-30 10:57:51 +02002112 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002113 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002114 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2115 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2116 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2117 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002118 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002119 signed long long cl;
2120
Willy Tarreauad14f752011-09-02 20:33:27 +02002121 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002122 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002123 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002124 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002125
Willy Tarreauad14f752011-09-02 20:33:27 +02002126 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002127 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002128 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002129 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002130
Willy Tarreauad14f752011-09-02 20:33:27 +02002131 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002132 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002133 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002134 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002135
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002136 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002137 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002138 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002139 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002140
Christopher Fauletbe821b92017-03-30 11:21:53 +02002141 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002142 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002143 }
2144
Willy Tarreau34dfc602015-05-01 10:09:49 +02002145 /* even bodyless requests have a known length */
2146 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002147
Willy Tarreau179085c2014-04-28 16:48:56 +02002148 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2149 * only change if both the request and the config reference something else.
2150 * Option httpclose by itself sets tunnel mode where headers are mangled.
2151 * However, if another mode is set, it will affect it (eg: server-close/
2152 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2153 * if FE and BE have the same settings (common). The method consists in
2154 * checking if options changed between the two calls (implying that either
2155 * one is non-null, or one of them is non-null and we are there for the first
2156 * time.
2157 */
2158 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002159 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002160 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002161
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002162 /* we may have to wait for the request's body */
2163 if ((s->be->options & PR_O_WREQ_BODY) &&
2164 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2165 req->analysers |= AN_REQ_HTTP_BODY;
2166
Willy Tarreau83ece462017-12-21 15:13:09 +01002167 /*
2168 * RFC7234#4:
2169 * A cache MUST write through requests with methods
2170 * that are unsafe (Section 4.2.1 of [RFC7231]) to
2171 * the origin server; i.e., a cache is not allowed
2172 * to generate a reply to such a request before
2173 * having forwarded the request and having received
2174 * a corresponding response.
2175 *
2176 * RFC7231#4.2.1:
2177 * Of the request methods defined by this
2178 * specification, the GET, HEAD, OPTIONS, and TRACE
2179 * methods are defined to be safe.
2180 */
2181 if (likely(txn->meth == HTTP_METH_GET ||
2182 txn->meth == HTTP_METH_HEAD ||
2183 txn->meth == HTTP_METH_OPTIONS ||
2184 txn->meth == HTTP_METH_TRACE))
2185 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2186
Willy Tarreaud787e662009-07-07 10:14:51 +02002187 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002188 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002189 req->analyse_exp = TICK_ETERNITY;
2190 return 1;
2191
2192 return_bad_req:
2193 /* We centralize bad requests processing here */
2194 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2195 /* we detected a parsing error. We want to archive this request
2196 * in the dedicated proxy area for later troubleshooting.
2197 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002198 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002199 }
2200
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002201 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002202 txn->req.msg_state = HTTP_MSG_ERROR;
2203 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002204 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002205
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002206 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002207 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002208 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002209
2210 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002211 if (!(s->flags & SF_ERR_MASK))
2212 s->flags |= SF_ERR_PRXCOND;
2213 if (!(s->flags & SF_FINST_MASK))
2214 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002215
Christopher Faulet0184ea72017-01-05 14:06:34 +01002216 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002217 req->analyse_exp = TICK_ETERNITY;
2218 return 0;
2219}
2220
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002221
Willy Tarreau347a35d2013-11-22 17:51:09 +01002222/* This function prepares an applet to handle the stats. It can deal with the
2223 * "100-continue" expectation, check that admin rules are met for POST requests,
2224 * and program a response message if something was unexpected. It cannot fail
2225 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002226 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002227 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002228 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002229 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002230int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002231{
2232 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002233 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002234 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002235 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002236 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002237 struct uri_auth *uri_auth = s->be->uri_auth;
2238 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002239 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002240
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002241 appctx = si_appctx(si);
2242 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2243 appctx->st1 = appctx->st2 = 0;
2244 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2245 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002246 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002247 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002248
2249 uri = msg->chn->buf->p + msg->sl.rq.u;
2250 lookup = uri + uri_auth->uri_len;
2251
2252 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2253 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002254 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002255 break;
2256 }
2257 }
2258
2259 if (uri_auth->refresh) {
2260 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2261 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002262 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002263 break;
2264 }
2265 }
2266 }
2267
2268 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2269 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002270 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002271 break;
2272 }
2273 }
2274
Willy Tarreau1e62df92016-01-11 18:57:53 +01002275 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2276 if (memcmp(h, ";typed", 6) == 0) {
2277 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2278 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2279 break;
2280 }
2281 }
2282
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002283 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2284 if (memcmp(h, ";st=", 4) == 0) {
2285 int i;
2286 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002287 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002288 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2289 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002290 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002291 break;
2292 }
2293 }
2294 break;
2295 }
2296 }
2297
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002298 appctx->ctx.stats.scope_str = 0;
2299 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002300 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2301 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2302 int itx = 0;
2303 const char *h2;
2304 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2305 const char *err;
2306
2307 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2308 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002309 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002310 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2311 itx++;
2312 h++;
2313 }
2314
2315 if (itx > STAT_SCOPE_TXT_MAXLEN)
2316 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002318
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002319 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002320 memcpy(scope_txt, h2, itx);
2321 scope_txt[itx] = '\0';
2322 err = invalid_char(scope_txt);
2323 if (err) {
2324 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 appctx->ctx.stats.scope_str = 0;
2326 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002327 }
2328 break;
2329 }
2330 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002331
2332 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002333 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002334 int ret = 1;
2335
2336 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002337 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 +01002338 ret = acl_pass(ret);
2339 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2340 ret = !ret;
2341 }
2342
2343 if (ret) {
2344 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002345 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002346 break;
2347 }
2348 }
2349
2350 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002351 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002352 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002353 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002354 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2355 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002356 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002357 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002358 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002359 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2360 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002361 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002362 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002363 else {
2364 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002366 }
2367
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002368 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002369 return 1;
2370}
2371
Lukas Tribus67db8df2013-06-23 17:37:13 +02002372/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2373 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2374 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002375void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002376{
2377#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002378 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002379 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2380#endif
2381#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002382 if (from->ss_family == AF_INET6) {
2383 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002384 /* v4-mapped addresses need IP_TOS */
2385 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2386 else
2387 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2388 }
2389#endif
2390}
2391
Willy Tarreau87b09662015-04-03 00:22:06 +02002392int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002393 const char* name, unsigned int name_len,
2394 const char *str, struct my_regex *re,
2395 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002396{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002397 struct hdr_ctx ctx;
2398 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002399 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002400 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2401 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002402 struct chunk *output = get_trash_chunk();
2403
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002404 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002405
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002406 /* Choose the header browsing function. */
2407 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002408 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002409 http_find_hdr_func = http_find_header2;
2410 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002411 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002412 http_find_hdr_func = http_find_full_header2;
2413 break;
2414 default: /* impossible */
2415 return -1;
2416 }
2417
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002418 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2419 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002420 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002421 char *val = ctx.line + ctx.val;
2422 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002423
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002424 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2425 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002426
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002427 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002428 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002429 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002430
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002431 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002432
2433 hdr->len += delta;
2434 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002435
2436 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002437 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002438 }
2439
2440 return 0;
2441}
2442
Willy Tarreau87b09662015-04-03 00:22:06 +02002443static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002444 const char* name, unsigned int name_len,
2445 struct list *fmt, struct my_regex *re,
2446 int action)
2447{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002448 struct chunk *replace;
2449 int ret = -1;
2450
2451 replace = alloc_trash_chunk();
2452 if (!replace)
2453 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002454
2455 replace->len = build_logline(s, replace->str, replace->size, fmt);
2456 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002457 goto leave;
2458
2459 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002460
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002461 leave:
2462 free_trash_chunk(replace);
2463 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002464}
2465
Willy Tarreau87b09662015-04-03 00:22:06 +02002466/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002467 * transaction <txn>. Returns the verdict of the first rule that prevents
2468 * further processing of the request (auth, deny, ...), and defaults to
2469 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2470 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002471 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2472 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2473 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002474 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002475enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002476http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002477{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002478 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002479 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002480 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002481 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002482 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002483 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002484 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002485 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002486
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002487 /* If "the current_rule_list" match the executed rule list, we are in
2488 * resume condition. If a resume is needed it is always in the action
2489 * and never in the ACL or converters. In this case, we initialise the
2490 * current rule, and go to the action execution point.
2491 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002492 if (s->current_rule) {
2493 rule = s->current_rule;
2494 s->current_rule = NULL;
2495 if (s->current_rule_list == rules)
2496 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002497 }
2498 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002499
Willy Tarreauff011f22011-01-06 17:51:27 +01002500 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002501
Willy Tarreau96257ec2012-12-27 10:46:37 +01002502 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002503 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002504 int ret;
2505
Willy Tarreau192252e2015-04-04 01:47:55 +02002506 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002507 ret = acl_pass(ret);
2508
Willy Tarreauff011f22011-01-06 17:51:27 +01002509 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002510 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002511
2512 if (!ret) /* condition not matched */
2513 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002514 }
2515
Willy Tarreauacc98002015-09-27 23:34:39 +02002516 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002517resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002518 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002519 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002520 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002521
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002522 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002523 if (deny_status)
2524 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002525 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002526
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002527 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002528 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002529 if (deny_status)
2530 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002531 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002532
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002533 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002534 /* Auth might be performed on regular http-req rules as well as on stats */
2535 auth_realm = rule->arg.auth.realm;
2536 if (!auth_realm) {
2537 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2538 auth_realm = STATS_DEFAULT_REALM;
2539 else
2540 auth_realm = px->id;
2541 }
2542 /* send 401/407 depending on whether we use a proxy or not. We still
2543 * count one error, because normal browsing won't significantly
2544 * increase the counter but brute force attempts will.
2545 */
2546 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2547 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002548 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002549 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002550 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002551
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002552 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002553 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2554 return HTTP_RULE_RES_BADREQ;
2555 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002556
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002557 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002558 s->task->nice = rule->arg.nice;
2559 break;
2560
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002561 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002562 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002563 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002564 break;
2565
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002566 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002567#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002568 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002569 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002570#endif
2571 break;
2572
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002573 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002574 s->logs.level = rule->arg.loglevel;
2575 break;
2576
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002577 case ACT_HTTP_REPLACE_HDR:
2578 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002579 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2580 rule->arg.hdr_add.name_len,
2581 &rule->arg.hdr_add.fmt,
2582 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002583 return HTTP_RULE_RES_BADREQ;
2584 break;
2585
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002586 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002587 ctx.idx = 0;
2588 /* remove all occurrences of the header */
2589 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2590 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2591 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002592 }
Willy Tarreau85603282015-01-21 20:39:27 +01002593 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002595 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002596 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002597 /* The scope of the trash buffer must be limited to this function. The
2598 * build_logline() function can execute a lot of other function which
2599 * can use the trash buffer. So for limiting the scope of this global
2600 * buffer, we build first the header value using build_logline, and
2601 * after we store the header name.
2602 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002603 struct chunk *replace;
2604
2605 replace = alloc_trash_chunk();
2606 if (!replace)
2607 return HTTP_RULE_RES_BADREQ;
2608
Thierry Fournier4b788f72016-06-01 13:35:36 +02002609 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002610 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2611 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2612 replace->str[rule->arg.hdr_add.name_len] = ':';
2613 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2614 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002615
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002616 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002617 /* remove all occurrences of the header */
2618 ctx.idx = 0;
2619 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2620 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2621 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2622 }
2623 }
2624
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002625 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2626
2627 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002628 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002629 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002630
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002631 case ACT_HTTP_DEL_ACL:
2632 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002633 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002634 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002635
2636 /* collect reference */
2637 ref = pat_ref_lookup(rule->arg.map.ref);
2638 if (!ref)
2639 continue;
2640
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002641 /* allocate key */
2642 key = alloc_trash_chunk();
2643 if (!key)
2644 return HTTP_RULE_RES_BADREQ;
2645
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002646 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002647 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2648 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002649
2650 /* perform update */
2651 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002652 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002653 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002654 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002655
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002656 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002657 break;
2658 }
2659
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002660 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002661 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002662 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002663
2664 /* collect reference */
2665 ref = pat_ref_lookup(rule->arg.map.ref);
2666 if (!ref)
2667 continue;
2668
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002669 /* allocate key */
2670 key = alloc_trash_chunk();
2671 if (!key)
2672 return HTTP_RULE_RES_BADREQ;
2673
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002674 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002675 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2676 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002677
2678 /* perform update */
2679 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002680 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002681 if (pat_ref_find_elt(ref, key->str) == NULL)
2682 pat_ref_add(ref, key->str, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002683 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002684
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002685 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002686 break;
2687 }
2688
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002689 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002690 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002691 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002692
2693 /* collect reference */
2694 ref = pat_ref_lookup(rule->arg.map.ref);
2695 if (!ref)
2696 continue;
2697
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002698 /* allocate key */
2699 key = alloc_trash_chunk();
2700 if (!key)
2701 return HTTP_RULE_RES_BADREQ;
2702
2703 /* allocate value */
2704 value = alloc_trash_chunk();
2705 if (!value) {
2706 free_trash_chunk(key);
2707 return HTTP_RULE_RES_BADREQ;
2708 }
2709
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002710 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002711 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2712 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002713
2714 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002715 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2716 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002717
2718 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002719 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002720 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002721 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002722 else
2723 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002724 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002725
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002726 free_trash_chunk(key);
2727 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002728 break;
2729 }
William Lallemand73025dd2014-04-24 14:38:37 +02002730
Thierry FOURNIER42148732015-09-02 17:17:33 +02002731 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002732 if ((s->req.flags & CF_READ_ERROR) ||
2733 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2734 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2735 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002736 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002737
Willy Tarreauacc98002015-09-27 23:34:39 +02002738 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002739 case ACT_RET_ERR:
2740 case ACT_RET_CONT:
2741 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002742 case ACT_RET_STOP:
2743 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002744 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002745 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002746 return HTTP_RULE_RES_YIELD;
2747 }
William Lallemand73025dd2014-04-24 14:38:37 +02002748 break;
2749
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002750 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002751 /* Note: only the first valid tracking parameter of each
2752 * applies.
2753 */
2754
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002755 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002756 struct stktable *t;
2757 struct stksess *ts;
2758 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002759 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002760
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002761 t = rule->arg.trk_ctr.table.t;
2762 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 +02002763
2764 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002765 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002766
2767 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002768 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2769 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2770 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002771 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002772
Emeric Brun819fc6f2017-06-13 19:37:32 +02002773 if (ptr1)
2774 stktable_data_cast(ptr1, http_req_cnt)++;
2775
2776 if (ptr2)
2777 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2778 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2779
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002780 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002781
2782 /* If data was modified, we need to touch to re-schedule sync */
2783 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002784 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002785
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002786 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002787 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002788 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002789 }
2790 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002791 break;
2792
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002793 /* other flags exists, but normaly, they never be matched. */
2794 default:
2795 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002796 }
2797 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002798
2799 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002800 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002801}
2802
Willy Tarreau71241ab2012-12-27 11:30:54 +01002803
Willy Tarreau51d861a2015-05-22 17:30:48 +02002804/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2805 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2806 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2807 * is returned, the process can continue the evaluation of next rule list. If
2808 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2809 * is returned, it means the operation could not be processed and a server error
2810 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2811 * deny rule. If *YIELD is returned, the caller must call again the function
2812 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002813 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002814static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002815http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002816{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002817 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002818 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002819 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002820 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002821 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002822 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002823
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002824 /* If "the current_rule_list" match the executed rule list, we are in
2825 * resume condition. If a resume is needed it is always in the action
2826 * and never in the ACL or converters. In this case, we initialise the
2827 * current rule, and go to the action execution point.
2828 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002829 if (s->current_rule) {
2830 rule = s->current_rule;
2831 s->current_rule = NULL;
2832 if (s->current_rule_list == rules)
2833 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002834 }
2835 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002836
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002837 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002838
2839 /* check optional condition */
2840 if (rule->cond) {
2841 int ret;
2842
Willy Tarreau192252e2015-04-04 01:47:55 +02002843 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002844 ret = acl_pass(ret);
2845
2846 if (rule->cond->pol == ACL_COND_UNLESS)
2847 ret = !ret;
2848
2849 if (!ret) /* condition not matched */
2850 continue;
2851 }
2852
Willy Tarreauacc98002015-09-27 23:34:39 +02002853 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002854resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002855 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002856 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002857 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002858
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002859 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002860 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002861 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002862
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002863 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002864 s->task->nice = rule->arg.nice;
2865 break;
2866
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002867 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002868 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002869 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002870 break;
2871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002872 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002873#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002874 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002875 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002876#endif
2877 break;
2878
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002879 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002880 s->logs.level = rule->arg.loglevel;
2881 break;
2882
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002883 case ACT_HTTP_REPLACE_HDR:
2884 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002885 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2886 rule->arg.hdr_add.name_len,
2887 &rule->arg.hdr_add.fmt,
2888 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002889 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002890 break;
2891
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002892 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002893 ctx.idx = 0;
2894 /* remove all occurrences of the header */
2895 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2896 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2897 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2898 }
Willy Tarreau85603282015-01-21 20:39:27 +01002899 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002900
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002901 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002902 case ACT_HTTP_ADD_HDR: {
2903 struct chunk *replace;
2904
2905 replace = alloc_trash_chunk();
2906 if (!replace)
2907 return HTTP_RULE_RES_BADREQ;
2908
2909 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2910 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2911 replace->len = rule->arg.hdr_add.name_len;
2912 replace->str[replace->len++] = ':';
2913 replace->str[replace->len++] = ' ';
2914 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2915 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002916
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002917 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002918 /* remove all occurrences of the header */
2919 ctx.idx = 0;
2920 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2921 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2922 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2923 }
2924 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002925 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2926
2927 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002928 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002929 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002930
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002931 case ACT_HTTP_DEL_ACL:
2932 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002933 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002934 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002935
2936 /* collect reference */
2937 ref = pat_ref_lookup(rule->arg.map.ref);
2938 if (!ref)
2939 continue;
2940
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002941 /* allocate key */
2942 key = alloc_trash_chunk();
2943 if (!key)
2944 return HTTP_RULE_RES_BADREQ;
2945
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002946 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002947 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2948 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002949
2950 /* perform update */
2951 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002952 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002953 pat_ref_delete(ref, key->str);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002954 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002955
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002956 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002957 break;
2958 }
2959
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002960 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002961 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002962 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002963
2964 /* collect reference */
2965 ref = pat_ref_lookup(rule->arg.map.ref);
2966 if (!ref)
2967 continue;
2968
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002969 /* allocate key */
2970 key = alloc_trash_chunk();
2971 if (!key)
2972 return HTTP_RULE_RES_BADREQ;
2973
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002974 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002975 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2976 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002977
2978 /* perform update */
2979 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002980 if (pat_ref_find_elt(ref, key->str) == NULL)
2981 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002982
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002983 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002984 break;
2985 }
2986
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002987 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002988 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002989 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002990
2991 /* collect reference */
2992 ref = pat_ref_lookup(rule->arg.map.ref);
2993 if (!ref)
2994 continue;
2995
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002996 /* allocate key */
2997 key = alloc_trash_chunk();
2998 if (!key)
2999 return HTTP_RULE_RES_BADREQ;
3000
3001 /* allocate value */
3002 value = alloc_trash_chunk();
3003 if (!value) {
3004 free_trash_chunk(key);
3005 return HTTP_RULE_RES_BADREQ;
3006 }
3007
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003008 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003009 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
3010 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003011
3012 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003013 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
3014 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003015
3016 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003017 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003018 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003019 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003020 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003021 else
3022 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003023 pat_ref_add(ref, key->str, value->str, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003024 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02003025 free_trash_chunk(key);
3026 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003027 break;
3028 }
William Lallemand73025dd2014-04-24 14:38:37 +02003029
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003030 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003031 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3032 return HTTP_RULE_RES_BADREQ;
3033 return HTTP_RULE_RES_DONE;
3034
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003035 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3036 /* Note: only the first valid tracking parameter of each
3037 * applies.
3038 */
3039
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003040 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003041 struct stktable *t;
3042 struct stksess *ts;
3043 struct stktable_key *key;
3044 void *ptr;
3045
3046 t = rule->arg.trk_ctr.table.t;
3047 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3048
3049 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003050 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003051
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003052 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003053
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003054 /* let's count a new HTTP request as it's the first time we do it */
3055 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3056 if (ptr)
3057 stktable_data_cast(ptr, http_req_cnt)++;
3058
3059 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3060 if (ptr)
3061 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3062 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3063
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003064 /* When the client triggers a 4xx from the server, it's most often due
3065 * to a missing object or permission. These events should be tracked
3066 * because if they happen often, it may indicate a brute force or a
3067 * vulnerability scan. Normally this is done when receiving the response
3068 * but here we're tracking after this ought to have been done so we have
3069 * to do it on purpose.
3070 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003071 if ((unsigned)(txn->status - 400) < 100) {
3072 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3073 if (ptr)
3074 stktable_data_cast(ptr, http_err_cnt)++;
3075
3076 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3077 if (ptr)
3078 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3079 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3080 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003081
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003082 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003083
Emeric Brun0fed0b02017-11-29 16:15:07 +01003084 /* If data was modified, we need to touch to re-schedule sync */
3085 stktable_touch_local(t, ts, 0);
3086
Emeric Brun819fc6f2017-06-13 19:37:32 +02003087 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3088 if (sess->fe != s->be)
3089 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3090
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003091 }
3092 }
3093 break;
3094
Thierry FOURNIER42148732015-09-02 17:17:33 +02003095 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003096 if ((s->req.flags & CF_READ_ERROR) ||
3097 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3098 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3099 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02003100 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003101
Willy Tarreauacc98002015-09-27 23:34:39 +02003102 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003103 case ACT_RET_ERR:
3104 case ACT_RET_CONT:
3105 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003106 case ACT_RET_STOP:
3107 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003108 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003109 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003110 return HTTP_RULE_RES_YIELD;
3111 }
William Lallemand73025dd2014-04-24 14:38:37 +02003112 break;
3113
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003114 /* other flags exists, but normaly, they never be matched. */
3115 default:
3116 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003117 }
3118 }
3119
3120 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003121 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003122}
3123
3124
Willy Tarreau71241ab2012-12-27 11:30:54 +01003125/* Perform an HTTP redirect based on the information in <rule>. The function
3126 * returns non-zero on success, or zero in case of a, irrecoverable error such
3127 * as too large a request to build a valid response.
3128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003129static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003130{
Willy Tarreaub329a312015-05-22 16:27:37 +02003131 struct http_msg *req = &txn->req;
3132 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003133 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003134 struct chunk *chunk;
3135 int ret = 0;
3136
3137 chunk = alloc_trash_chunk();
3138 if (!chunk)
3139 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003140
3141 /* build redirect message */
3142 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003143 case 308:
3144 msg_fmt = HTTP_308;
3145 break;
3146 case 307:
3147 msg_fmt = HTTP_307;
3148 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003149 case 303:
3150 msg_fmt = HTTP_303;
3151 break;
3152 case 301:
3153 msg_fmt = HTTP_301;
3154 break;
3155 case 302:
3156 default:
3157 msg_fmt = HTTP_302;
3158 break;
3159 }
3160
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003161 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3162 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003163
3164 switch(rule->type) {
3165 case REDIRECT_TYPE_SCHEME: {
3166 const char *path;
3167 const char *host;
3168 struct hdr_ctx ctx;
3169 int pathlen;
3170 int hostlen;
3171
3172 host = "";
3173 hostlen = 0;
3174 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003175 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003176 host = ctx.line + ctx.val;
3177 hostlen = ctx.vlen;
3178 }
3179
3180 path = http_get_path(txn);
3181 /* build message using path */
3182 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003183 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003184 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3185 int qs = 0;
3186 while (qs < pathlen) {
3187 if (path[qs] == '?') {
3188 pathlen = qs;
3189 break;
3190 }
3191 qs++;
3192 }
3193 }
3194 } else {
3195 path = "/";
3196 pathlen = 1;
3197 }
3198
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003199 if (rule->rdr_str) { /* this is an old "redirect" rule */
3200 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003201 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3202 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003203
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003204 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003205 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3206 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003207 }
3208 else {
3209 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003210 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003211
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003212 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003213 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3214 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003215 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003216 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003217 memcpy(chunk->str + chunk->len, "://", 3);
3218 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003219
3220 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003221 memcpy(chunk->str + chunk->len, host, hostlen);
3222 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003223
3224 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003225 memcpy(chunk->str + chunk->len, path, pathlen);
3226 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003227
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003228 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003229 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003230 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003231 if (chunk->len > chunk->size - 5)
3232 goto leave;
3233 chunk->str[chunk->len] = '/';
3234 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235 }
3236
3237 break;
3238 }
3239 case REDIRECT_TYPE_PREFIX: {
3240 const char *path;
3241 int pathlen;
3242
3243 path = http_get_path(txn);
3244 /* build message using path */
3245 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003246 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003247 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3248 int qs = 0;
3249 while (qs < pathlen) {
3250 if (path[qs] == '?') {
3251 pathlen = qs;
3252 break;
3253 }
3254 qs++;
3255 }
3256 }
3257 } else {
3258 path = "/";
3259 pathlen = 1;
3260 }
3261
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003262 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003263 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3264 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003265
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003266 /* add prefix. Note that if prefix == "/", we don't want to
3267 * add anything, otherwise it makes it hard for the user to
3268 * configure a self-redirection.
3269 */
3270 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003271 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3272 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003273 }
3274 }
3275 else {
3276 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003277 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003278
3279 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003280 if (chunk->len + pathlen > chunk->size - 4)
3281 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003282 }
3283
3284 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003285 memcpy(chunk->str + chunk->len, path, pathlen);
3286 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003287
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003288 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003289 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003290 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003291 if (chunk->len > chunk->size - 5)
3292 goto leave;
3293 chunk->str[chunk->len] = '/';
3294 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003295 }
3296
3297 break;
3298 }
3299 case REDIRECT_TYPE_LOCATION:
3300 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003301 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003302 if (chunk->len + rule->rdr_len > chunk->size - 4)
3303 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003304
3305 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003306 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3307 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003308 }
3309 else {
3310 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003311 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003312
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003313 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003314 if (chunk->len > chunk->size - 4)
3315 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003316 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003317 break;
3318 }
3319
3320 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003321 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3322 chunk->len += 14;
3323 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3324 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003325 }
3326
Willy Tarreau19b14122017-02-28 09:48:11 +01003327 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003328 txn->status = rule->code;
3329 /* let's log the request time */
3330 s->logs.tv_request = now;
3331
Christopher Fauletbe821b92017-03-30 11:21:53 +02003332 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003333 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3334 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3335 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003336 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003337 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003338 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3339 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003340 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003341 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3342 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003343 }
3344 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003345 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3346 chunk->len += 4;
3347 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003348 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003349 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003350 bi_fast_delete(req->chn->buf, req->sov);
3351 req->next -= req->sov;
3352 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003353 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003354 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003355 req->msg_state = HTTP_MSG_CLOSED;
3356 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003357 /* Trim any possible response */
3358 res->chn->buf->i = 0;
3359 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003360 /* let the server side turn to SI_ST_CLO */
3361 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003362 } else {
3363 /* keep-alive not possible */
3364 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003365 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3366 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003367 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003368 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3369 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003370 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003371 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003372 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003373 }
3374
Willy Tarreaue7dff022015-04-03 01:14:29 +02003375 if (!(s->flags & SF_ERR_MASK))
3376 s->flags |= SF_ERR_LOCAL;
3377 if (!(s->flags & SF_FINST_MASK))
3378 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003379
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003380 ret = 1;
3381 leave:
3382 free_trash_chunk(chunk);
3383 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003384}
3385
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003386/* This stream analyser runs all HTTP request processing which is common to
3387 * frontends and backends, which means blocking ACLs, filters, connection-close,
3388 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003389 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003390 * either needs more data or wants to immediately abort the request (eg: deny,
3391 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003392 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003393int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003394{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003395 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003396 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003397 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003398 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003399 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003400 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003401 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003402 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003403
Willy Tarreau655dce92009-11-08 13:10:58 +01003404 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003405 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003406 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003407 }
3408
Willy Tarreau87b09662015-04-03 00:22:06 +02003409 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 +02003410 now_ms, __FUNCTION__,
3411 s,
3412 req,
3413 req->rex, req->wex,
3414 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003415 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003416 req->analysers);
3417
Willy Tarreau65410832014-04-28 21:25:43 +02003418 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003419 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003420
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003421 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003422 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003423 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003424
Willy Tarreau0b748332014-04-29 00:13:29 +02003425 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003426 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3427 goto return_prx_yield;
3428
Willy Tarreau0b748332014-04-29 00:13:29 +02003429 case HTTP_RULE_RES_CONT:
3430 case HTTP_RULE_RES_STOP: /* nothing to do */
3431 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003432
Willy Tarreau0b748332014-04-29 00:13:29 +02003433 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3434 if (txn->flags & TX_CLTARPIT)
3435 goto tarpit;
3436 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003437
Willy Tarreau0b748332014-04-29 00:13:29 +02003438 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3439 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003440
Willy Tarreau0b748332014-04-29 00:13:29 +02003441 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003442 goto done;
3443
Willy Tarreau0b748332014-04-29 00:13:29 +02003444 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3445 goto return_bad_req;
3446 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003447 }
3448
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003449 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3450 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003451 struct hdr_ctx ctx;
3452
3453 ctx.idx = 0;
3454 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3455 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3456 if (unlikely(http_header_add_tail2(&txn->req,
3457 &txn->hdr_idx, "Early-Data: 1",
3458 strlen("Early-Data: 1"))) < 0) {
3459 goto return_bad_req;
3460 }
3461 }
3462
3463 }
3464
Willy Tarreau52542592014-04-28 18:33:26 +02003465 /* OK at this stage, we know that the request was accepted according to
3466 * the http-request rules, we can check for the stats. Note that the
3467 * URI is detected *before* the req* rules in order not to be affected
3468 * by a possible reqrep, while they are processed *after* so that a
3469 * reqdeny can still block them. This clearly needs to change in 1.6!
3470 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003471 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003472 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003473 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003474 txn->status = 500;
3475 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003476 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003477
Willy Tarreaue7dff022015-04-03 01:14:29 +02003478 if (!(s->flags & SF_ERR_MASK))
3479 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003480 goto return_prx_cond;
3481 }
3482
3483 /* parse the whole stats request and extract the relevant information */
3484 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003485 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003486 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003487
Willy Tarreau0b748332014-04-29 00:13:29 +02003488 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3489 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003490
Willy Tarreau0b748332014-04-29 00:13:29 +02003491 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3492 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003493 }
3494
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003495 /* evaluate the req* rules except reqadd */
3496 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003497 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003498 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003499
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003500 if (txn->flags & TX_CLDENY)
3501 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003502
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003503 if (txn->flags & TX_CLTARPIT) {
3504 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003505 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003506 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003507 }
Willy Tarreau06619262006-12-17 08:37:22 +01003508
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003509 /* add request headers from the rule sets in the same order */
3510 list_for_each_entry(wl, &px->req_add, list) {
3511 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003512 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003513 ret = acl_pass(ret);
3514 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3515 ret = !ret;
3516 if (!ret)
3517 continue;
3518 }
3519
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003520 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003521 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003522 }
3523
Willy Tarreau52542592014-04-28 18:33:26 +02003524
3525 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003526 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3527 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003528 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003529 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003530 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003531
Willy Tarreaue7dff022015-04-03 01:14:29 +02003532 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3533 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3534 if (!(s->flags & SF_FINST_MASK))
3535 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003536
Willy Tarreau70730dd2014-04-24 18:06:27 +02003537 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003538 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3539 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003540 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003541 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003542 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003543
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003544 /* check whether we have some ACLs set to redirect this request */
3545 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003546 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003547 int ret;
3548
Willy Tarreau192252e2015-04-04 01:47:55 +02003549 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003550 ret = acl_pass(ret);
3551 if (rule->cond->pol == ACL_COND_UNLESS)
3552 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003553 if (!ret)
3554 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003555 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003556 if (!http_apply_redirect_rule(rule, s, txn))
3557 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003558 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003559 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003560
Willy Tarreau2be39392010-01-03 17:24:51 +01003561 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3562 * If this happens, then the data will not come immediately, so we must
3563 * send all what we have without waiting. Note that due to the small gain
3564 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003565 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003566 * itself once used.
3567 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003568 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003569
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003570 done: /* done with this analyser, continue with next ones that the calling
3571 * points will have set, if any.
3572 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003573 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003574 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3575 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003576 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003577
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003578 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003579 /* Allow cookie logging
3580 */
3581 if (s->be->cookie_name || sess->fe->capture_name)
3582 manage_client_side_cookies(s, req);
3583
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003584 /* When a connection is tarpitted, we use the tarpit timeout,
3585 * which may be the same as the connect timeout if unspecified.
3586 * If unset, then set it to zero because we really want it to
3587 * eventually expire. We build the tarpit as an analyser.
3588 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003589 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003590
3591 /* wipe the request out so that we can drop the connection early
3592 * if the client closes first.
3593 */
3594 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003595
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003596 txn->status = http_err_codes[deny_status];
3597
Christopher Faulet0184ea72017-01-05 14:06:34 +01003598 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003599 req->analysers |= AN_REQ_HTTP_TARPIT;
3600 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3601 if (!req->analyse_exp)
3602 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003603 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003604 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003605 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003606 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003607 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003608 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003609 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003610
3611 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003612
3613 /* Allow cookie logging
3614 */
3615 if (s->be->cookie_name || sess->fe->capture_name)
3616 manage_client_side_cookies(s, req);
3617
Willy Tarreau0b748332014-04-29 00:13:29 +02003618 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003619 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003620 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003621 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003622 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003623 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003624 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003625 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003626 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003627 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003628 goto return_prx_cond;
3629
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003630 return_bad_req:
3631 /* We centralize bad requests processing here */
3632 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3633 /* we detected a parsing error. We want to archive this request
3634 * in the dedicated proxy area for later troubleshooting.
3635 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003636 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003637 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003638
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003639 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003640 txn->req.msg_state = HTTP_MSG_ERROR;
3641 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003642 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003643
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003644 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003645 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003646 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003647
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003648 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003649 if (!(s->flags & SF_ERR_MASK))
3650 s->flags |= SF_ERR_PRXCOND;
3651 if (!(s->flags & SF_FINST_MASK))
3652 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003653
Christopher Faulet0184ea72017-01-05 14:06:34 +01003654 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003655 req->analyse_exp = TICK_ETERNITY;
3656 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003657
3658 return_prx_yield:
3659 channel_dont_connect(req);
3660 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003661}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003662
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003663/* This function performs all the processing enabled for the current request.
3664 * It returns 1 if the processing can continue on next analysers, or zero if it
3665 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003666 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003667 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003668int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003669{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003670 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003671 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003672 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003673 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003674
Willy Tarreau655dce92009-11-08 13:10:58 +01003675 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003676 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003677 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003678 return 0;
3679 }
3680
Willy Tarreau87b09662015-04-03 00:22:06 +02003681 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 +02003682 now_ms, __FUNCTION__,
3683 s,
3684 req,
3685 req->rex, req->wex,
3686 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003687 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003688 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003689
Willy Tarreau59234e92008-11-30 23:51:27 +01003690 /*
3691 * Right now, we know that we have processed the entire headers
3692 * and that unwanted requests have been filtered out. We can do
3693 * whatever we want with the remaining request. Also, now we
3694 * may have separate values for ->fe, ->be.
3695 */
Willy Tarreau06619262006-12-17 08:37:22 +01003696
Willy Tarreau59234e92008-11-30 23:51:27 +01003697 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003698 * If HTTP PROXY is set we simply get remote server address parsing
3699 * incoming request. Note that this requires that a connection is
3700 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003701 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003702 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003703 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003704 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003705
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003706 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003707 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003708 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003709 txn->req.msg_state = HTTP_MSG_ERROR;
3710 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003711 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003712 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003713
Willy Tarreaue7dff022015-04-03 01:14:29 +02003714 if (!(s->flags & SF_ERR_MASK))
3715 s->flags |= SF_ERR_RESOURCE;
3716 if (!(s->flags & SF_FINST_MASK))
3717 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003718
3719 return 0;
3720 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003721
3722 path = http_get_path(txn);
Christopher Faulet11ebb202018-04-13 15:53:12 +02003723 if (url2sa(req->buf->p + msg->sl.rq.u,
3724 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
3725 &conn->addr.to, NULL) == -1)
3726 goto return_bad_req;
3727
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003728 /* if the path was found, we have to remove everything between
3729 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3730 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3731 * u_l characters by a single "/".
3732 */
3733 if (path) {
3734 char *cur_ptr = req->buf->p;
3735 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3736 int delta;
3737
3738 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3739 http_msg_move_end(&txn->req, delta);
3740 cur_end += delta;
3741 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3742 goto return_bad_req;
3743 }
3744 else {
3745 char *cur_ptr = req->buf->p;
3746 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3747 int delta;
3748
3749 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3750 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3751 http_msg_move_end(&txn->req, delta);
3752 cur_end += delta;
3753 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3754 goto return_bad_req;
3755 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003756 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003757
Willy Tarreau59234e92008-11-30 23:51:27 +01003758 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003759 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003760 * Note that doing so might move headers in the request, but
3761 * the fields will stay coherent and the URI will not move.
3762 * This should only be performed in the backend.
3763 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003764 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003765 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003766
William Lallemanda73203e2012-03-12 12:48:57 +01003767 /* add unique-id if "header-unique-id" is specified */
3768
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003769 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003770 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003771 goto return_bad_req;
3772 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003773 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003774 }
William Lallemanda73203e2012-03-12 12:48:57 +01003775
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003776 if (sess->fe->header_unique_id && s->unique_id) {
3777 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003778 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003779 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003780 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003781 goto return_bad_req;
3782 }
3783
Cyril Bontéb21570a2009-11-29 20:04:48 +01003784 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003785 * 9: add X-Forwarded-For if either the frontend or the backend
3786 * asks for it.
3787 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003788 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003789 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003790 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3791 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3792 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003793 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003794 /* The header is set to be added only if none is present
3795 * and we found it, so don't do anything.
3796 */
3797 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003798 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003799 /* Add an X-Forwarded-For header unless the source IP is
3800 * in the 'except' network range.
3801 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003802 if ((!sess->fe->except_mask.s_addr ||
3803 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3804 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003806 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003807 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003808 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003809 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003810 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003811
3812 /* Note: we rely on the backend to get the header name to be used for
3813 * x-forwarded-for, because the header is really meant for the backends.
3814 * However, if the backend did not specify any option, we have to rely
3815 * on the frontend's header name.
3816 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003817 if (s->be->fwdfor_hdr_len) {
3818 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003819 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003820 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003821 len = sess->fe->fwdfor_hdr_len;
3822 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003823 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003824 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 +01003825
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003826 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003827 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003828 }
3829 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003830 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003831 /* FIXME: for the sake of completeness, we should also support
3832 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003833 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003834 int len;
3835 char pn[INET6_ADDRSTRLEN];
3836 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003837 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003838 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003839
Willy Tarreau59234e92008-11-30 23:51:27 +01003840 /* Note: we rely on the backend to get the header name to be used for
3841 * x-forwarded-for, because the header is really meant for the backends.
3842 * However, if the backend did not specify any option, we have to rely
3843 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003844 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003845 if (s->be->fwdfor_hdr_len) {
3846 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003847 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003848 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003849 len = sess->fe->fwdfor_hdr_len;
3850 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003851 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003852 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003853
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003854 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003855 goto return_bad_req;
3856 }
3857 }
3858
3859 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003860 * 10: add X-Original-To if either the frontend or the backend
3861 * asks for it.
3862 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003863 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003864
3865 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003866 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003867 /* Add an X-Original-To header unless the destination IP is
3868 * in the 'except' network range.
3869 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003870 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003871
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003872 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003873 ((!sess->fe->except_mask_to.s_addr ||
3874 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3875 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003876 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003877 (((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 +02003878 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003879 int len;
3880 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003881 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003882
3883 /* Note: we rely on the backend to get the header name to be used for
3884 * x-original-to, because the header is really meant for the backends.
3885 * However, if the backend did not specify any option, we have to rely
3886 * on the frontend's header name.
3887 */
3888 if (s->be->orgto_hdr_len) {
3889 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003890 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003891 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003892 len = sess->fe->orgto_hdr_len;
3893 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003894 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003895 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 +02003896
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003897 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003898 goto return_bad_req;
3899 }
3900 }
3901 }
3902
Willy Tarreau50fc7772012-11-11 22:19:57 +01003903 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3904 * If an "Upgrade" token is found, the header is left untouched in order not to have
3905 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3906 * "Upgrade" is present in the Connection header.
3907 */
3908 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3909 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003910 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003911 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003912 unsigned int want_flags = 0;
3913
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003914 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003915 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003916 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003917 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003918 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003919 want_flags |= TX_CON_CLO_SET;
3920 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003921 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003922 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003923 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003924 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003925 want_flags |= TX_CON_KAL_SET;
3926 }
3927
3928 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003929 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003930 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003931
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003932
Willy Tarreau522d6c02009-12-06 18:49:18 +01003933 /* If we have no server assigned yet and we're balancing on url_param
3934 * with a POST request, we may be interested in checking the body for
3935 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003936 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003937 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003938 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003939 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003940 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003941 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003942 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003943
Christopher Fauletbe821b92017-03-30 11:21:53 +02003944 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3945 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003946#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003947 /* We expect some data from the client. Unless we know for sure
3948 * we already have a full request, we have to re-enable quick-ack
3949 * in case we previously disabled it, otherwise we might cause
3950 * the client to delay further data.
3951 */
3952 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3953 cli_conn && conn_ctrl_ready(cli_conn) &&
3954 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3955 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003956 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003957#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003958
Willy Tarreau59234e92008-11-30 23:51:27 +01003959 /*************************************************************
3960 * OK, that's finished for the headers. We have done what we *
3961 * could. Let's switch to the DATA state. *
3962 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003963 req->analyse_exp = TICK_ETERNITY;
3964 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003965
Willy Tarreau59234e92008-11-30 23:51:27 +01003966 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003967 /* OK let's go on with the BODY now */
3968 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003969
Willy Tarreau59234e92008-11-30 23:51:27 +01003970 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003971 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003972 /* we detected a parsing error. We want to archive this request
3973 * in the dedicated proxy area for later troubleshooting.
3974 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003975 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003976 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003977
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003978 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003979 txn->req.msg_state = HTTP_MSG_ERROR;
3980 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003981 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003982 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003983
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003984 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003985 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003986 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003987
Willy Tarreaue7dff022015-04-03 01:14:29 +02003988 if (!(s->flags & SF_ERR_MASK))
3989 s->flags |= SF_ERR_PRXCOND;
3990 if (!(s->flags & SF_FINST_MASK))
3991 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003992 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003993}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003994
Willy Tarreau60b85b02008-11-30 23:28:40 +01003995/* This function is an analyser which processes the HTTP tarpit. It always
3996 * returns zero, at the beginning because it prevents any other processing
3997 * from occurring, and at the end because it terminates the request.
3998 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003999int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004000{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004001 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004002
4003 /* This connection is being tarpitted. The CLIENT side has
4004 * already set the connect expiration date to the right
4005 * timeout. We just have to check that the client is still
4006 * there and that the timeout has not expired.
4007 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004008 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004009 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004010 !tick_is_expired(req->analyse_exp, now_ms))
4011 return 0;
4012
4013 /* We will set the queue timer to the time spent, just for
4014 * logging purposes. We fake a 500 server error, so that the
4015 * attacker will not suspect his connection has been tarpitted.
4016 * It will not cause trouble to the logs because we can exclude
4017 * the tarpitted connections by filtering on the 'PT' status flags.
4018 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004019 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4020
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004021 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004022 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004023
Christopher Faulet0184ea72017-01-05 14:06:34 +01004024 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004025 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004026
Willy Tarreaue7dff022015-04-03 01:14:29 +02004027 if (!(s->flags & SF_ERR_MASK))
4028 s->flags |= SF_ERR_PRXCOND;
4029 if (!(s->flags & SF_FINST_MASK))
4030 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004031 return 0;
4032}
4033
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004034/* This function is an analyser which waits for the HTTP request body. It waits
4035 * for either the buffer to be full, or the full advertised contents to have
4036 * reached the buffer. It must only be called after the standard HTTP request
4037 * processing has occurred, because it expects the request to be parsed and will
4038 * look for the Expect header. It may send a 100-Continue interim response. It
4039 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4040 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4041 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004042 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004043int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004044{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004045 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004046 struct http_txn *txn = s->txn;
4047 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004048
4049 /* We have to parse the HTTP request body to find any required data.
4050 * "balance url_param check_post" should have been the only way to get
4051 * into this. We were brought here after HTTP header analysis, so all
4052 * related structures are ready.
4053 */
4054
Willy Tarreau890988f2014-04-10 11:59:33 +02004055 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4056 /* This is the first call */
4057 if (msg->msg_state < HTTP_MSG_BODY)
4058 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004059
Willy Tarreau890988f2014-04-10 11:59:33 +02004060 if (msg->msg_state < HTTP_MSG_100_SENT) {
4061 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4062 * send an HTTP/1.1 100 Continue intermediate response.
4063 */
4064 if (msg->flags & HTTP_MSGF_VER_11) {
4065 struct hdr_ctx ctx;
4066 ctx.idx = 0;
4067 /* Expect is allowed in 1.1, look for it */
4068 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4069 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004070 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004071 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004072 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004073 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004074 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004075 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004076
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004077 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004078 * req->buf->p still points to the beginning of the message. We
4079 * must save the body in msg->next because it survives buffer
4080 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004081 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004082 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004083
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004084 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004085 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4086 else
4087 msg->msg_state = HTTP_MSG_DATA;
4088 }
4089
Willy Tarreau890988f2014-04-10 11:59:33 +02004090 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4091 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004092 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004093 goto missing_data;
4094
4095 /* OK we have everything we need now */
4096 goto http_end;
4097 }
4098
4099 /* OK here we're parsing a chunked-encoded message */
4100
Willy Tarreau522d6c02009-12-06 18:49:18 +01004101 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004102 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004103 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004104 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004105 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004106 unsigned int chunk;
4107 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004108
Willy Tarreau115acb92009-12-26 13:56:06 +01004109 if (!ret)
4110 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004111 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004112 msg->err_pos = req->buf->i + ret;
4113 if (msg->err_pos < 0)
4114 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004115 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004116 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004117 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004118
4119 msg->chunk_len = chunk;
4120 msg->body_len += chunk;
4121
4122 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004123 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004124 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004125 }
4126
Willy Tarreaud98cf932009-12-27 22:54:55 +01004127 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004128 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4129 * for at least a whole chunk or the whole content length bytes after
4130 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004131 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004132 if (msg->msg_state == HTTP_MSG_TRAILERS)
4133 goto http_end;
4134
Willy Tarreaue115b492015-05-01 23:05:14 +02004135 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004136 goto http_end;
4137
4138 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004139 /* we get here if we need to wait for more data. If the buffer is full,
4140 * we have the maximum we can expect.
4141 */
4142 if (buffer_full(req->buf, global.tune.maxrewrite))
4143 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004144
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004145 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004146 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004147 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004148
Willy Tarreaue7dff022015-04-03 01:14:29 +02004149 if (!(s->flags & SF_ERR_MASK))
4150 s->flags |= SF_ERR_CLITO;
4151 if (!(s->flags & SF_FINST_MASK))
4152 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004153 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004154 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004155
4156 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004157 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004158 /* Not enough data. We'll re-use the http-request
4159 * timeout here. Ideally, we should set the timeout
4160 * relative to the accept() date. We just set the
4161 * request timeout once at the beginning of the
4162 * request.
4163 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004164 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004165 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004166 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004167 return 0;
4168 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004169
4170 http_end:
4171 /* The situation will not evolve, so let's give up on the analysis. */
4172 s->logs.tv_request = now; /* update the request timer to reflect full request */
4173 req->analysers &= ~an_bit;
4174 req->analyse_exp = TICK_ETERNITY;
4175 return 1;
4176
4177 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004178 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004179 txn->req.msg_state = HTTP_MSG_ERROR;
4180 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004181 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004182
Willy Tarreaue7dff022015-04-03 01:14:29 +02004183 if (!(s->flags & SF_ERR_MASK))
4184 s->flags |= SF_ERR_PRXCOND;
4185 if (!(s->flags & SF_FINST_MASK))
4186 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004187
Willy Tarreau522d6c02009-12-06 18:49:18 +01004188 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004189 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004190 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004191 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004192 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004193 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004194}
4195
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004196/* send a server's name with an outgoing request over an established connection.
4197 * Note: this function is designed to be called once the request has been scheduled
4198 * for being forwarded. This is the reason why it rewinds the buffer before
4199 * proceeding.
4200 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004201int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004202
4203 struct hdr_ctx ctx;
4204
Mark Lamourinec2247f02012-01-04 13:02:01 -05004205 char *hdr_name = be->server_id_hdr_name;
4206 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004207 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004208 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004209 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004210
William Lallemandd9e90662012-01-30 17:27:17 +01004211 ctx.idx = 0;
4212
Willy Tarreau211cdec2014-04-17 20:18:08 +02004213 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004214 if (old_o) {
4215 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004216 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004217 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004218 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004219 }
4220
Willy Tarreau9b28e032012-10-12 23:49:43 +02004221 old_i = chn->buf->i;
4222 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 -05004223 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004224 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004225 }
4226
4227 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004228 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004229 memcpy(hdr_val, hdr_name, hdr_name_len);
4230 hdr_val += hdr_name_len;
4231 *hdr_val++ = ':';
4232 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004233 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4234 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004235
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004236 if (old_o) {
4237 /* If this was a forwarded request, we must readjust the amount of
4238 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004239 * variations. Note that the current state is >= HTTP_MSG_BODY,
4240 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004241 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004242 old_o += chn->buf->i - old_i;
4243 b_adv(chn->buf, old_o);
4244 txn->req.next -= old_o;
4245 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004246 }
4247
Mark Lamourinec2247f02012-01-04 13:02:01 -05004248 return 0;
4249}
4250
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251/* Terminate current transaction and prepare a new one. This is very tricky
4252 * right now but it works.
4253 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004254void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004255{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004256 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004257 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004258 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004259 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004260 struct connection *srv_conn;
4261 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004262 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004263
Willy Tarreau610ecce2010-01-04 21:15:02 +01004264 /* FIXME: We need a more portable way of releasing a backend's and a
4265 * server's connections. We need a safer way to reinitialize buffer
4266 * flags. We also need a more accurate method for computing per-request
4267 * data.
4268 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004269 /*
4270 * XXX cognet: This is probably wrong, this is killing a whole
4271 * connection, in the new world order, we probably want to just kill
4272 * the stream, this is to be revisited the day we handle multiple
4273 * streams in one server connection.
4274 */
4275 cs = objt_cs(s->si[1].end);
4276 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004277
Willy Tarreau4213a112013-12-15 10:25:42 +01004278 /* unless we're doing keep-alive, we want to quickly close the connection
4279 * to the server.
4280 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004281 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004282 !si_conn_ready(&s->si[1])) {
4283 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4284 si_shutr(&s->si[1]);
4285 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004286 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287
Willy Tarreaue7dff022015-04-03 01:14:29 +02004288 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004289 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004290 if (unlikely(s->srv_conn))
4291 sess_change_server(s, NULL);
4292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293
4294 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004295 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296
Willy Tarreaueee5b512015-04-03 23:46:31 +02004297 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298 int n;
4299
Willy Tarreaueee5b512015-04-03 23:46:31 +02004300 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 if (n < 1 || n > 5)
4302 n = 0;
4303
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004304 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004305 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004306 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004307 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004308 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004309 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4310 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004311 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004312 }
4313
4314 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004315 s->logs.bytes_in -= s->req.buf->i;
4316 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004317
4318 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004319 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004320 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004321 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004322 s->do_log(s);
4323 }
4324
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004325 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004326 stream_stop_content_counters(s);
4327 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004328
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 s->logs.accept_date = date; /* user-visible date for logging */
4330 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004331 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4332 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004333 tv_zero(&s->logs.tv_request);
4334 s->logs.t_queue = -1;
4335 s->logs.t_connect = -1;
4336 s->logs.t_data = -1;
4337 s->logs.t_close = 0;
4338 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4339 s->logs.srv_queue_size = 0; /* we will get this number soon */
4340
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004341 s->logs.bytes_in = s->req.total = s->req.buf->i;
4342 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343
4344 if (s->pend_pos)
4345 pendconn_free(s->pend_pos);
4346
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004347 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004348 if (s->flags & SF_CURR_SESS) {
4349 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004350 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004351 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004352 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004353 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004354 }
4355
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004356 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004357
Willy Tarreau4213a112013-12-15 10:25:42 +01004358 /* only release our endpoint if we don't intend to reuse the
4359 * connection.
4360 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004361 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004362 !si_conn_ready(&s->si[1])) {
4363 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004364 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004365 }
4366
Willy Tarreau350f4872014-11-28 14:42:25 +01004367 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4368 s->si[1].err_type = SI_ET_NONE;
4369 s->si[1].conn_retries = 0; /* used for logging too */
4370 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004371 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 +01004372 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 +01004373 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 +02004374 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4375 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4376 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004377
Willy Tarreaueee5b512015-04-03 23:46:31 +02004378 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004379 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004380 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004381
4382 if (prev_status == 401 || prev_status == 407) {
4383 /* In HTTP keep-alive mode, if we receive a 401, we still have
4384 * a chance of being able to send the visitor again to the same
4385 * server over the same connection. This is required by some
4386 * broken protocols such as NTLM, and anyway whenever there is
4387 * an opportunity for sending the challenge to the proper place,
4388 * it's better to do it (at least it helps with debugging).
4389 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004390 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004391 if (srv_conn)
4392 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004393 }
4394
Willy Tarreau53f96852016-02-02 18:50:47 +01004395 /* Never ever allow to reuse a connection from a non-reuse backend */
4396 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4397 srv_conn->flags |= CO_FL_PRIVATE;
4398
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004399 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004400 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004402 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004403 s->req.flags |= CF_NEVER_WAIT;
4404 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004405 }
4406
Willy Tarreau714ea782015-11-25 20:11:11 +01004407 /* we're removing the analysers, we MUST re-enable events detection.
4408 * We don't enable close on the response channel since it's either
4409 * already closed, or in keep-alive with an idle connection handler.
4410 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004411 channel_auto_read(&s->req);
4412 channel_auto_close(&s->req);
4413 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004414
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004415 /* we're in keep-alive with an idle connection, monitor it if not already done */
4416 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004417 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004418 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004419 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004420 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004421 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004422 else if (prev_flags & TX_NOT_FIRST)
4423 /* note: we check the request, not the connection, but
4424 * this is valid for strategies SAFE and AGGR, and in
4425 * case of ALWS, we don't care anyway.
4426 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004427 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004428 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004429 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004430 }
Christopher Faulete6006242017-03-10 11:52:44 +01004431 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4432 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004433}
4434
4435
4436/* This function updates the request state machine according to the response
4437 * state machine and buffer flags. It returns 1 if it changes anything (flag
4438 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4439 * it is only used to find when a request/response couple is complete. Both
4440 * this function and its equivalent should loop until both return zero. It
4441 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4442 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004443int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004445 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004446 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004447 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448 unsigned int old_state = txn->req.msg_state;
4449
Christopher Faulet4be98032017-07-18 10:48:24 +02004450 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004451 return 0;
4452
4453 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004454 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004455 * We can shut the read side unless we want to abort_on_close,
4456 * or we have a POST request. The issue with POST requests is
4457 * that some browsers still send a CRLF after the request, and
4458 * this CRLF must be read so that it does not remain in the kernel
4459 * buffers, otherwise a close could cause an RST on some systems
4460 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004461 * Note that if we're using keep-alive on the client side, we'd
4462 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004463 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004464 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004465 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004466 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4467 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004468 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4469 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004470 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004471 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004472
Willy Tarreau40f151a2012-12-20 12:10:09 +01004473 /* if the server closes the connection, we want to immediately react
4474 * and close the socket to save packets and syscalls.
4475 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004476 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004477
Willy Tarreau7f876a12015-11-18 11:59:55 +01004478 /* In any case we've finished parsing the request so we must
4479 * disable Nagle when sending data because 1) we're not going
4480 * to shut this side, and 2) the server is waiting for us to
4481 * send pending data.
4482 */
4483 chn->flags |= CF_NEVER_WAIT;
4484
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4486 goto wait_other_side;
4487
4488 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4489 /* The server has not finished to respond, so we
4490 * don't want to move in order not to upset it.
4491 */
4492 goto wait_other_side;
4493 }
4494
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495 /* When we get here, it means that both the request and the
4496 * response have finished receiving. Depending on the connection
4497 * mode, we'll have to wait for the last bytes to leave in either
4498 * direction, and sometimes for a close to be effective.
4499 */
4500
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004501 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4502 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004503 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4504 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004505 }
4506 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4507 /* Option forceclose is set, or either side wants to close,
4508 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004509 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004510 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004511 *
4512 * However, there is an exception if the response
4513 * length is undefined. In this case, we need to wait
4514 * the close from the server. The response will be
4515 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004516 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004517 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4518 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4519 goto check_channel_flags;
4520
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004521 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4522 channel_shutr_now(chn);
4523 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004524 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004525 }
4526 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004527 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4528 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004529 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004530 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4531 channel_auto_read(chn);
4532 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004533 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004534 }
4535
Christopher Faulet4be98032017-07-18 10:48:24 +02004536 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004537 }
4538
4539 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4540 http_msg_closing:
4541 /* nothing else to forward, just waiting for the output buffer
4542 * to be empty and for the shutw_now to take effect.
4543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004544 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 txn->req.msg_state = HTTP_MSG_CLOSED;
4546 goto http_msg_closed;
4547 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004548 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004549 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004552 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553 }
4554
4555 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4556 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004557 /* if we don't know whether the server will close, we need to hard close */
4558 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4559 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4560
Willy Tarreau3988d932013-12-27 23:03:08 +01004561 /* see above in MSG_DONE why we only do this in these states */
4562 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4563 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004564 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4565 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004566 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 goto wait_other_side;
4568 }
4569
Christopher Faulet4be98032017-07-18 10:48:24 +02004570 check_channel_flags:
4571 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4572 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4573 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004574 txn->req.msg_state = HTTP_MSG_CLOSING;
4575 goto http_msg_closing;
4576 }
4577
4578
Willy Tarreau610ecce2010-01-04 21:15:02 +01004579 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004580 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004581}
4582
4583
4584/* This function updates the response state machine according to the request
4585 * state machine and buffer flags. It returns 1 if it changes anything (flag
4586 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4587 * it is only used to find when a request/response couple is complete. Both
4588 * this function and its equivalent should loop until both return zero. It
4589 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4590 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004591int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004593 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004594 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004595 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 unsigned int old_state = txn->rsp.msg_state;
4597
Christopher Faulet4be98032017-07-18 10:48:24 +02004598 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004599 return 0;
4600
4601 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4602 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004603 * still monitor the server connection for a possible close
4604 * while the request is being uploaded, so we don't disable
4605 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004606 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004607 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004608
4609 if (txn->req.msg_state == HTTP_MSG_ERROR)
4610 goto wait_other_side;
4611
4612 if (txn->req.msg_state < HTTP_MSG_DONE) {
4613 /* The client seems to still be sending data, probably
4614 * because we got an error response during an upload.
4615 * We have the choice of either breaking the connection
4616 * or letting it pass through. Let's do the later.
4617 */
4618 goto wait_other_side;
4619 }
4620
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621 /* When we get here, it means that both the request and the
4622 * response have finished receiving. Depending on the connection
4623 * mode, we'll have to wait for the last bytes to leave in either
4624 * direction, and sometimes for a close to be effective.
4625 */
4626
4627 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4628 /* Server-close mode : shut read and wait for the request
4629 * side to close its output buffer. The caller will detect
4630 * when we're in DONE and the other is in CLOSED and will
4631 * catch that for the final cleanup.
4632 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004633 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4634 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004636 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4637 /* Option forceclose is set, or either side wants to close,
4638 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004639 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004640 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004642 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004643 channel_shutr_now(chn);
4644 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004645 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004646 }
4647 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004648 /* The last possible modes are keep-alive and tunnel. Tunnel will
4649 * need to forward remaining data. Keep-alive will need to monitor
4650 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004652 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004653 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004654 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4655 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 }
4657
Christopher Faulet4be98032017-07-18 10:48:24 +02004658 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 }
4660
4661 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4662 http_msg_closing:
4663 /* nothing else to forward, just waiting for the output buffer
4664 * to be empty and for the shutw_now to take effect.
4665 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004666 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004667 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4668 goto http_msg_closed;
4669 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004670 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004671 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004672 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004673 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004674 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004675 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004677 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 }
4679
4680 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4681 http_msg_closed:
4682 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004683 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004684 channel_auto_close(chn);
4685 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004686 goto wait_other_side;
4687 }
4688
Christopher Faulet4be98032017-07-18 10:48:24 +02004689 check_channel_flags:
4690 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4691 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4692 /* if we've just closed an output, let's switch */
4693 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4694 goto http_msg_closing;
4695 }
4696
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004698 /* We force the response to leave immediately if we're waiting for the
4699 * other side, since there is no pending shutdown to push it out.
4700 */
4701 if (!channel_is_empty(chn))
4702 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004703 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704}
4705
4706
Christopher Faulet894da4c2017-07-18 11:29:07 +02004707/* Resync the request and response state machines. */
4708void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004709{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004710 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004711#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004712 int old_req_state = txn->req.msg_state;
4713 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004714#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004715
Willy Tarreau610ecce2010-01-04 21:15:02 +01004716 http_sync_req_state(s);
4717 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004718 if (!http_sync_res_state(s))
4719 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004720 if (!http_sync_req_state(s))
4721 break;
4722 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004723
Christopher Faulet894da4c2017-07-18 11:29:07 +02004724 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4725 "req->analysers=0x%08x res->analysers=0x%08x\n",
4726 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004727 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4728 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004729 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004730
4731
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 /* OK, both state machines agree on a compatible state.
4733 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004734 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4735 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004736 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4737 * means we must abort the request.
4738 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4739 * corresponding channel.
4740 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4741 * on the response with server-close mode means we've completed one
4742 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004744 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4745 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004746 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004747 channel_auto_close(&s->req);
4748 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004749 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004750 channel_auto_close(&s->res);
4751 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004752 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004753 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4754 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004755 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004756 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004757 channel_auto_close(&s->res);
4758 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004759 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004760 channel_abort(&s->req);
4761 channel_auto_close(&s->req);
4762 channel_auto_read(&s->req);
4763 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004764 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004765 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4766 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4767 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4768 s->req.analysers &= AN_REQ_FLT_END;
4769 if (HAS_REQ_DATA_FILTERS(s))
4770 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4771 }
4772 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4773 s->res.analysers &= AN_RES_FLT_END;
4774 if (HAS_RSP_DATA_FILTERS(s))
4775 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4776 }
4777 channel_auto_close(&s->req);
4778 channel_auto_read(&s->req);
4779 channel_auto_close(&s->res);
4780 channel_auto_read(&s->res);
4781 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004782 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4783 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004784 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004785 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4786 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4787 /* server-close/keep-alive: terminate this transaction,
4788 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004789 * a fresh-new transaction, but only once we're sure there's
4790 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004791 * another request. They must not hold any pending output data
4792 * and the response buffer must realigned
4793 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004794 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004795 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004796 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004797 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004798 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004799 else {
4800 s->req.analysers = AN_REQ_FLT_END;
4801 s->res.analysers = AN_RES_FLT_END;
4802 txn->flags |= TX_WAIT_CLEANUP;
4803 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004805}
4806
Willy Tarreaud98cf932009-12-27 22:54:55 +01004807/* This function is an analyser which forwards request body (including chunk
4808 * sizes if any). It is called as soon as we must forward, even if we forward
4809 * zero byte. The only situation where it must not be called is when we're in
4810 * tunnel mode and we want to forward till the close. It's used both to forward
4811 * remaining data and to resync after end of body. It expects the msg_state to
4812 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004813 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004814 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004815 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004816 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004817int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004818{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004819 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004820 struct http_txn *txn = s->txn;
4821 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004822 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004823
Christopher Faulet814d2702017-03-30 11:33:44 +02004824 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4825 now_ms, __FUNCTION__,
4826 s,
4827 req,
4828 req->rex, req->wex,
4829 req->flags,
4830 req->buf->i,
4831 req->analysers);
4832
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004833 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4834 return 0;
4835
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004836 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004837 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004838 /* Output closed while we were sending data. We must abort and
4839 * wake the other side up.
4840 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004841 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004842 msg->msg_state = HTTP_MSG_ERROR;
4843 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004844 return 1;
4845 }
4846
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847 /* Note that we don't have to send 100-continue back because we don't
4848 * need the data to complete our job, and it's up to the server to
4849 * decide whether to return 100, 417 or anything else in return of
4850 * an "Expect: 100-continue" header.
4851 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004852 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004853 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4854 ? HTTP_MSG_CHUNK_SIZE
4855 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004856
4857 /* TODO/filters: when http-buffer-request option is set or if a
4858 * rule on url_param exists, the first chunk size could be
4859 * already parsed. In that case, msg->next is after the chunk
4860 * size (including the CRLF after the size). So this case should
4861 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004862 }
4863
Willy Tarreau7ba23542014-04-17 21:50:00 +02004864 /* Some post-connect processing might want us to refrain from starting to
4865 * forward data. Currently, the only reason for this is "balance url_param"
4866 * whichs need to parse/process the request after we've enabled forwarding.
4867 */
4868 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004869 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004870 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004871 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004872 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004873 }
4874 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4875 }
4876
Willy Tarreau80a92c02014-03-12 10:41:13 +01004877 /* in most states, we should abort in case of early close */
4878 channel_auto_close(req);
4879
Willy Tarreauefdf0942014-04-24 20:08:57 +02004880 if (req->to_forward) {
4881 /* We can't process the buffer's contents yet */
4882 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004883 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004884 }
4885
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004886 if (msg->msg_state < HTTP_MSG_DONE) {
4887 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4888 ? http_msg_forward_chunked_body(s, msg)
4889 : http_msg_forward_body(s, msg));
4890 if (!ret)
4891 goto missing_data_or_waiting;
4892 if (ret < 0)
4893 goto return_bad_req;
4894 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004895
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004896 /* other states, DONE...TUNNEL */
4897 /* we don't want to forward closes on DONE except in tunnel mode. */
4898 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4899 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004900
Christopher Faulet894da4c2017-07-18 11:29:07 +02004901 http_resync_states(s);
4902 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004903 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4904 if (req->flags & CF_SHUTW) {
4905 /* request errors are most likely due to the
4906 * server aborting the transfer. */
4907 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004908 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004909 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004910 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004911 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004912 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004913 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004914 }
4915
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004916 /* If "option abortonclose" is set on the backend, we want to monitor
4917 * the client's connection and forward any shutdown notification to the
4918 * server, which will decide whether to close or to go on processing the
4919 * request. We only do that in tunnel mode, and not in other modes since
4920 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004921 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004922 channel_auto_read(req);
4923 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4924 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4925 s->si[1].flags |= SI_FL_NOLINGER;
4926 channel_auto_close(req);
4927 }
4928 else if (s->txn->meth == HTTP_METH_POST) {
4929 /* POST requests may require to read extra CRLF sent by broken
4930 * browsers and which could cause an RST to be sent upon close
4931 * on some systems (eg: Linux). */
4932 channel_auto_read(req);
4933 }
4934 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004935
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004936 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004937 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004938 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004939 if (!(s->flags & SF_ERR_MASK))
4940 s->flags |= SF_ERR_CLICL;
4941 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004942 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004943 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004944 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004945 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004946 }
4947
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004948 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4949 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004950 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004951 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004952
4953 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004954 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004955
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004956 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004957 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004958 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004960 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004961 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004962 * And when content-length is used, we never want to let the possible
4963 * shutdown be forwarded to the other side, as the state machine will
4964 * take care of it once the client responds. It's also important to
4965 * prevent TIME_WAITs from accumulating on the backend side, and for
4966 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004967 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004968 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004969 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004970
Willy Tarreau5c620922011-05-11 19:56:11 +02004971 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004972 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004973 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004974 * modes are already handled by the stream sock layer. We must not do
4975 * this in content-length mode because it could present the MSG_MORE
4976 * flag with the last block of forwarded data, which would cause an
4977 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004978 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004979 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004980 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004981
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004982 return 0;
4983
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004985 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004986 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004987 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004988
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004989 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004990 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004991 txn->req.msg_state = HTTP_MSG_ERROR;
4992 if (txn->status) {
4993 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004994 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004995 } else {
4996 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004997 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004998 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004999 req->analysers &= AN_REQ_FLT_END;
5000 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 +01005001
Willy Tarreaue7dff022015-04-03 01:14:29 +02005002 if (!(s->flags & SF_ERR_MASK))
5003 s->flags |= SF_ERR_PRXCOND;
5004 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005005 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005006 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005007 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005008 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005009 }
5010 return 0;
5011
5012 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005013 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005014 txn->req.msg_state = HTTP_MSG_ERROR;
5015 if (txn->status) {
5016 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005017 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005018 } else {
5019 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005020 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005021 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005022 req->analysers &= AN_REQ_FLT_END;
5023 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 +01005024
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005025 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5026 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005027 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005028 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005029
Willy Tarreaue7dff022015-04-03 01:14:29 +02005030 if (!(s->flags & SF_ERR_MASK))
5031 s->flags |= SF_ERR_SRVCL;
5032 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005033 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005034 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005035 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005036 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005037 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005038 return 0;
5039}
5040
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5042 * processing can continue on next analysers, or zero if it either needs more
5043 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005044 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005045 * when it has nothing left to do, and may remove any analyser when it wants to
5046 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005047 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005048int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005049{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005050 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005051 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005053 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005054 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005055 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005056 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005057
Willy Tarreau87b09662015-04-03 00:22:06 +02005058 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 +02005059 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005060 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005061 rep,
5062 rep->rex, rep->wex,
5063 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005064 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005065 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005066
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005067 /*
5068 * Now parse the partial (or complete) lines.
5069 * We will check the response syntax, and also join multi-line
5070 * headers. An index of all the lines will be elaborated while
5071 * parsing.
5072 *
5073 * For the parsing, we use a 28 states FSM.
5074 *
5075 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005076 * rep->buf->p = beginning of response
5077 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5078 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005079 * msg->eol = end of current header or line (LF or CRLF)
5080 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005081 */
5082
Willy Tarreau628c40c2014-04-24 19:11:26 +02005083 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005084 /* There's a protected area at the end of the buffer for rewriting
5085 * purposes. We don't want to start to parse the request if the
5086 * protected area is affected, because we may have to move processed
5087 * data later, which is much more complicated.
5088 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005089 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005090 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005091 /* some data has still not left the buffer, wake us once that's done */
5092 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5093 goto abort_response;
5094 channel_dont_close(rep);
5095 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005096 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005097 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005098 }
5099
Willy Tarreau379357a2013-06-08 12:55:46 +02005100 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5101 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5102 buffer_slow_realign(rep->buf);
5103
Willy Tarreau9b28e032012-10-12 23:49:43 +02005104 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005105 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005106 }
5107
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005108 /* 1: we might have to print this header in debug mode */
5109 if (unlikely((global.mode & MODE_DEBUG) &&
5110 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005111 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005112 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005113
Willy Tarreau9b28e032012-10-12 23:49:43 +02005114 sol = rep->buf->p;
5115 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005116 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005117
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005118 sol += hdr_idx_first_pos(&txn->hdr_idx);
5119 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005120
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 while (cur_idx) {
5122 eol = sol + txn->hdr_idx.v[cur_idx].len;
5123 debug_hdr("srvhdr", s, sol, eol);
5124 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5125 cur_idx = txn->hdr_idx.v[cur_idx].next;
5126 }
5127 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005128
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005129 /*
5130 * Now we quickly check if we have found a full valid response.
5131 * If not so, we check the FD and buffer states before leaving.
5132 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005133 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 * responses are checked first.
5135 *
5136 * Depending on whether the client is still there or not, we
5137 * may send an error response back or not. Note that normally
5138 * we should only check for HTTP status there, and check I/O
5139 * errors somewhere else.
5140 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005141
Willy Tarreau655dce92009-11-08 13:10:58 +01005142 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 /* Invalid response */
5144 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5145 /* we detected a parsing error. We want to archive this response
5146 * in the dedicated proxy area for later troubleshooting.
5147 */
5148 hdr_response_bad:
5149 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005150 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005152 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005153 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005154 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005155 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005156 }
Willy Tarreau64648412010-03-05 10:41:54 +01005157 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005158 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005159 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005160 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005161 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005162 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005163 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005164
Willy Tarreaue7dff022015-04-03 01:14:29 +02005165 if (!(s->flags & SF_ERR_MASK))
5166 s->flags |= SF_ERR_PRXCOND;
5167 if (!(s->flags & SF_FINST_MASK))
5168 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005169
5170 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005171 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005172
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005173 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005174 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005175 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005176 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005177 goto hdr_response_bad;
5178 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005180 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005181 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005183 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005184 else if (txn->flags & TX_NOT_FIRST)
5185 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005186
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005187 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005188 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005189 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005190 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005191 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005192
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005193 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005194 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005195 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01005196
5197 /* Check to see if the server refused the early data.
5198 * If so, just send a 425
5199 */
5200 if (objt_cs(s->si[1].end)) {
5201 struct connection *conn = objt_cs(s->si[1].end)->conn;
5202
5203 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
5204 txn->status = 425;
5205 }
5206
Willy Tarreau350f4872014-11-28 14:42:25 +01005207 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005208 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005209 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005210
Willy Tarreaue7dff022015-04-03 01:14:29 +02005211 if (!(s->flags & SF_ERR_MASK))
5212 s->flags |= SF_ERR_SRVCL;
5213 if (!(s->flags & SF_FINST_MASK))
5214 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005215 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005216 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005217
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005218 /* read timeout : return a 504 to the client. */
5219 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005221 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005222
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005223 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005224 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005225 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005226 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005227 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005228
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005229 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005230 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005231 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005232 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005233 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005234 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005235
Willy Tarreaue7dff022015-04-03 01:14:29 +02005236 if (!(s->flags & SF_ERR_MASK))
5237 s->flags |= SF_ERR_SRVTO;
5238 if (!(s->flags & SF_FINST_MASK))
5239 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005240 return 0;
5241 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005242
Willy Tarreauf003d372012-11-26 13:35:37 +01005243 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005244 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005245 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5246 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005247 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005248 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005249
Christopher Faulet0184ea72017-01-05 14:06:34 +01005250 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005251 channel_auto_close(rep);
5252
5253 txn->status = 400;
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 Tarreauf003d372012-11-26 13:35:37 +01005256
Willy Tarreaue7dff022015-04-03 01:14:29 +02005257 if (!(s->flags & SF_ERR_MASK))
5258 s->flags |= SF_ERR_CLICL;
5259 if (!(s->flags & SF_FINST_MASK))
5260 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005261
Willy Tarreau87b09662015-04-03 00:22:06 +02005262 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005263 return 0;
5264 }
5265
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005266 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005267 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005268 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005269 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005270 else if (txn->flags & TX_NOT_FIRST)
5271 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005272
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005273 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005274 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005275 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005276 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005277 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005278
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005279 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005280 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005281 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005282 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005283 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005284 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005285
Willy Tarreaue7dff022015-04-03 01:14:29 +02005286 if (!(s->flags & SF_ERR_MASK))
5287 s->flags |= SF_ERR_SRVCL;
5288 if (!(s->flags & SF_FINST_MASK))
5289 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290 return 0;
5291 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005292
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005293 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005294 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005295 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005296 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005297 else if (txn->flags & TX_NOT_FIRST)
5298 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005299
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005300 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005301 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005302 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005303
Willy Tarreaue7dff022015-04-03 01:14:29 +02005304 if (!(s->flags & SF_ERR_MASK))
5305 s->flags |= SF_ERR_CLICL;
5306 if (!(s->flags & SF_FINST_MASK))
5307 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005308
Willy Tarreau87b09662015-04-03 00:22:06 +02005309 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005310 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005311 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005312
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005313 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005314 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005315 return 0;
5316 }
5317
5318 /* More interesting part now : we know that we have a complete
5319 * response which at least looks like HTTP. We have an indicator
5320 * of each header's length, so we can parse them quickly.
5321 */
5322
5323 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005324 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005325
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005326 /*
5327 * 1: get the status code
5328 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005329 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 if (n < 1 || n > 5)
5331 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005332 /* when the client triggers a 4xx from the server, it's most often due
5333 * to a missing object or permission. These events should be tracked
5334 * because if they happen often, it may indicate a brute force or a
5335 * vulnerability scan.
5336 */
5337 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005338 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005339
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005340 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005341 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005342
Willy Tarreau91852eb2015-05-01 13:26:00 +02005343 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5344 * exactly one digit "." one digit. This check may be disabled using
5345 * option accept-invalid-http-response.
5346 */
5347 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5348 if (msg->sl.st.v_l != 8) {
5349 msg->err_pos = 0;
5350 goto hdr_response_bad;
5351 }
5352
5353 if (rep->buf->p[4] != '/' ||
5354 !isdigit((unsigned char)rep->buf->p[5]) ||
5355 rep->buf->p[6] != '.' ||
5356 !isdigit((unsigned char)rep->buf->p[7])) {
5357 msg->err_pos = 4;
5358 goto hdr_response_bad;
5359 }
5360 }
5361
Willy Tarreau5b154472009-12-21 20:11:07 +01005362 /* check if the response is HTTP/1.1 or above */
5363 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005364 ((rep->buf->p[5] > '1') ||
5365 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005366 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005367
5368 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005369 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 +01005370
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005371 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005372 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005373
Willy Tarreau9b28e032012-10-12 23:49:43 +02005374 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005375
Willy Tarreau39650402010-03-15 19:44:39 +01005376 /* Adjust server's health based on status code. Note: status codes 501
5377 * and 505 are triggered on demand by client request, so we must not
5378 * count them as server failures.
5379 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005380 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005381 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005382 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005383 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005384 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005385 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005386
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005387 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005388 * We may be facing a 100-continue response, or any other informational
5389 * 1xx response which is non-final, in which case this is not the right
5390 * response, and we're waiting for the next one. Let's allow this response
5391 * to go to the client and wait for the next one. There's an exception for
5392 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005393 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005394 if (txn->status < 200 &&
5395 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005396 hdr_idx_init(&txn->hdr_idx);
5397 msg->next -= channel_forward(rep, msg->next);
5398 msg->msg_state = HTTP_MSG_RPBEFORE;
5399 txn->status = 0;
5400 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005401 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005402 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005403 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005404
Willy Tarreaua14ad722017-07-07 11:36:32 +02005405 /*
5406 * 2: check for cacheability.
5407 */
5408
5409 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005410 case 200:
5411 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005412 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005413 case 206:
5414 case 300:
5415 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005416 case 404:
5417 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005418 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005419 case 414:
5420 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005421 break;
5422 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005423 /* RFC7231#6.1:
5424 * Responses with status codes that are defined as
5425 * cacheable by default (e.g., 200, 203, 204, 206,
5426 * 300, 301, 404, 405, 410, 414, and 501 in this
5427 * specification) can be reused by a cache with
5428 * heuristic expiration unless otherwise indicated
5429 * by the method definition or explicit cache
5430 * controls [RFC7234]; all other status codes are
5431 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005432 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005433 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005434 break;
5435 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005436
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005437 /*
5438 * 3: we may need to capture headers
5439 */
5440 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005441 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005442 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005443 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005444
Willy Tarreau557f1992015-05-01 10:05:17 +02005445 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5446 * by RFC7230#3.3.3 :
5447 *
5448 * The length of a message body is determined by one of the following
5449 * (in order of precedence):
5450 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005451 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5452 * the connection will become a tunnel immediately after the empty
5453 * line that concludes the header fields. A client MUST ignore
5454 * any Content-Length or Transfer-Encoding header fields received
5455 * in such a message. Any 101 response (Switching Protocols) is
5456 * managed in the same manner.
5457 *
5458 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005459 * (Informational), 204 (No Content), or 304 (Not Modified) status
5460 * code is always terminated by the first empty line after the
5461 * header fields, regardless of the header fields present in the
5462 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005463 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005464 * 3. If a Transfer-Encoding header field is present and the chunked
5465 * transfer coding (Section 4.1) is the final encoding, the message
5466 * body length is determined by reading and decoding the chunked
5467 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005468 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005469 * If a Transfer-Encoding header field is present in a response and
5470 * the chunked transfer coding is not the final encoding, the
5471 * message body length is determined by reading the connection until
5472 * it is closed by the server. If a Transfer-Encoding header field
5473 * is present in a request and the chunked transfer coding is not
5474 * the final encoding, the message body length cannot be determined
5475 * reliably; the server MUST respond with the 400 (Bad Request)
5476 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005477 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005478 * If a message is received with both a Transfer-Encoding and a
5479 * Content-Length header field, the Transfer-Encoding overrides the
5480 * Content-Length. Such a message might indicate an attempt to
5481 * perform request smuggling (Section 9.5) or response splitting
5482 * (Section 9.4) and ought to be handled as an error. A sender MUST
5483 * remove the received Content-Length field prior to forwarding such
5484 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005485 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005486 * 4. If a message is received without Transfer-Encoding and with
5487 * either multiple Content-Length header fields having differing
5488 * field-values or a single Content-Length header field having an
5489 * invalid value, then the message framing is invalid and the
5490 * recipient MUST treat it as an unrecoverable error. If this is a
5491 * request message, the server MUST respond with a 400 (Bad Request)
5492 * status code and then close the connection. If this is a response
5493 * message received by a proxy, the proxy MUST close the connection
5494 * to the server, discard the received response, and send a 502 (Bad
5495 * Gateway) response to the client. If this is a response message
5496 * received by a user agent, the user agent MUST close the
5497 * connection to the server and discard the received response.
5498 *
5499 * 5. If a valid Content-Length header field is present without
5500 * Transfer-Encoding, its decimal value defines the expected message
5501 * body length in octets. If the sender closes the connection or
5502 * the recipient times out before the indicated number of octets are
5503 * received, the recipient MUST consider the message to be
5504 * incomplete and close the connection.
5505 *
5506 * 6. If this is a request message and none of the above are true, then
5507 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005508 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005509 * 7. Otherwise, this is a response message without a declared message
5510 * body length, so the message body length is determined by the
5511 * number of octets received prior to the server closing the
5512 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005513 */
5514
5515 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005516 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005517 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005518 * FIXME: should we parse anyway and return an error on chunked encoding ?
5519 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005520 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5521 txn->status == 101)) {
5522 /* Either we've established an explicit tunnel, or we're
5523 * switching the protocol. In both cases, we're very unlikely
5524 * to understand the next protocols. We have to switch to tunnel
5525 * mode, so that we transfer the request and responses then let
5526 * this protocol pass unmodified. When we later implement specific
5527 * parsers for such protocols, we'll want to check the Upgrade
5528 * header which contains information about that protocol for
5529 * responses with status 101 (eg: see RFC2817 about TLS).
5530 */
5531 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5532 msg->flags |= HTTP_MSGF_XFER_LEN;
5533 goto end;
5534 }
5535
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005536 if (txn->meth == HTTP_METH_HEAD ||
5537 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005538 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005539 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005540 goto skip_content_length;
5541 }
5542
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005543 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005544 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005545 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005546 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005547 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5548 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005549 /* bad transfer-encoding (chunked followed by something else) */
5550 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005551 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005552 break;
5553 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005554 }
5555
Willy Tarreau1c913912015-04-30 10:57:51 +02005556 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005557 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005558 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005559 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5560 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5561 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005562 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005563 signed long long cl;
5564
Willy Tarreauad14f752011-09-02 20:33:27 +02005565 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005566 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005567 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005568 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005569
Willy Tarreauad14f752011-09-02 20:33:27 +02005570 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005571 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005572 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005573 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005574
Willy Tarreauad14f752011-09-02 20:33:27 +02005575 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005576 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005577 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005578 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005579
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005580 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005581 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005582 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005583 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005584
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005585 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005586 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005587 }
5588
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005589 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005590 /* Now we have to check if we need to modify the Connection header.
5591 * This is more difficult on the response than it is on the request,
5592 * because we can have two different HTTP versions and we don't know
5593 * how the client will interprete a response. For instance, let's say
5594 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5595 * HTTP/1.1 response without any header. Maybe it will bound itself to
5596 * HTTP/1.0 because it only knows about it, and will consider the lack
5597 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5598 * the lack of header as a keep-alive. Thus we will use two flags
5599 * indicating how a request MAY be understood by the client. In case
5600 * of multiple possibilities, we'll fix the header to be explicit. If
5601 * ambiguous cases such as both close and keepalive are seen, then we
5602 * will fall back to explicit close. Note that we won't take risks with
5603 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005604 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005605 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005606 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5607 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5608 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5609 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005610 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005611
Willy Tarreau70dffda2014-01-30 03:07:23 +01005612 /* this situation happens when combining pretend-keepalive with httpclose. */
5613 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005614 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005615 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005616 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5617
Willy Tarreau60466522010-01-18 19:08:45 +01005618 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005619 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005620 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5621 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005622
Willy Tarreau60466522010-01-18 19:08:45 +01005623 /* now adjust header transformations depending on current state */
5624 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5625 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5626 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005627 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005628 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005629 }
Willy Tarreau60466522010-01-18 19:08:45 +01005630 else { /* SCL / KAL */
5631 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005632 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005633 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005634 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005635
Willy Tarreau60466522010-01-18 19:08:45 +01005636 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005637 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005638
Willy Tarreau60466522010-01-18 19:08:45 +01005639 /* Some keep-alive responses are converted to Server-close if
5640 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005641 */
Willy Tarreau60466522010-01-18 19:08:45 +01005642 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5643 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005644 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005645 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005646 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005647 }
5648
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005649 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005650 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005651 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005652
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005653 /* end of job, return OK */
5654 rep->analysers &= ~an_bit;
5655 rep->analyse_exp = TICK_ETERNITY;
5656 channel_auto_close(rep);
5657 return 1;
5658
5659 abort_keep_alive:
5660 /* A keep-alive request to the server failed on a network error.
5661 * The client is required to retry. We need to close without returning
5662 * any other information so that the client retries.
5663 */
5664 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005665 rep->analysers &= AN_RES_FLT_END;
5666 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005667 channel_auto_close(rep);
5668 s->logs.logwait = 0;
5669 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005670 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005671 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005672 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005673 return 0;
5674}
5675
5676/* This function performs all the processing enabled for the current response.
5677 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005678 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005679 * other functions. It works like process_request (see indications above).
5680 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005681int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005682{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005683 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005684 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005685 struct http_msg *msg = &txn->rsp;
5686 struct proxy *cur_proxy;
5687 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005688 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005689
Willy Tarreau87b09662015-04-03 00:22:06 +02005690 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 +02005691 now_ms, __FUNCTION__,
5692 s,
5693 rep,
5694 rep->rex, rep->wex,
5695 rep->flags,
5696 rep->buf->i,
5697 rep->analysers);
5698
5699 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5700 return 0;
5701
Willy Tarreau70730dd2014-04-24 18:06:27 +02005702 /* The stats applet needs to adjust the Connection header but we don't
5703 * apply any filter there.
5704 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005705 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5706 rep->analysers &= ~an_bit;
5707 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005708 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005709 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005710
Willy Tarreau58975672014-04-24 21:13:57 +02005711 /*
5712 * We will have to evaluate the filters.
5713 * As opposed to version 1.2, now they will be evaluated in the
5714 * filters order and not in the header order. This means that
5715 * each filter has to be validated among all headers.
5716 *
5717 * Filters are tried with ->be first, then with ->fe if it is
5718 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005719 *
5720 * Maybe we are in resume condiion. In this case I choose the
5721 * "struct proxy" which contains the rule list matching the resume
5722 * pointer. If none of theses "struct proxy" match, I initialise
5723 * the process with the first one.
5724 *
5725 * In fact, I check only correspondance betwwen the current list
5726 * pointer and the ->fe rule list. If it doesn't match, I initialize
5727 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005728 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005729 if (s->current_rule_list == &sess->fe->http_res_rules)
5730 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005731 else
5732 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005733 while (1) {
5734 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005735
Willy Tarreau58975672014-04-24 21:13:57 +02005736 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005737 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005738 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005739
Willy Tarreau51d861a2015-05-22 17:30:48 +02005740 if (ret == HTTP_RULE_RES_BADREQ)
5741 goto return_srv_prx_502;
5742
5743 if (ret == HTTP_RULE_RES_DONE) {
5744 rep->analysers &= ~an_bit;
5745 rep->analyse_exp = TICK_ETERNITY;
5746 return 1;
5747 }
5748 }
5749
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005750 /* we need to be called again. */
5751 if (ret == HTTP_RULE_RES_YIELD) {
5752 channel_dont_close(rep);
5753 return 0;
5754 }
5755
Willy Tarreau58975672014-04-24 21:13:57 +02005756 /* try headers filters */
5757 if (rule_set->rsp_exp != NULL) {
5758 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5759 return_bad_resp:
5760 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005761 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005762 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005763 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005764 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005765 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005766 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005767 txn->status = 502;
5768 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005769 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005770 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005771 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005772 if (!(s->flags & SF_ERR_MASK))
5773 s->flags |= SF_ERR_PRXCOND;
5774 if (!(s->flags & SF_FINST_MASK))
5775 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005776 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005777 }
Willy Tarreau58975672014-04-24 21:13:57 +02005778 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005779
Willy Tarreau58975672014-04-24 21:13:57 +02005780 /* has the response been denied ? */
5781 if (txn->flags & TX_SVDENY) {
5782 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005783 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005784
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005785 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5786 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005787 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005788 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005789
Willy Tarreau58975672014-04-24 21:13:57 +02005790 goto return_srv_prx_502;
5791 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005792
Willy Tarreau58975672014-04-24 21:13:57 +02005793 /* add response headers from the rule sets in the same order */
5794 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005795 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005796 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005797 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005798 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005799 ret = acl_pass(ret);
5800 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5801 ret = !ret;
5802 if (!ret)
5803 continue;
5804 }
5805 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5806 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005807 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005808
Willy Tarreau58975672014-04-24 21:13:57 +02005809 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005810 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005811 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005812 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005813 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005814
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005815 /* After this point, this anayzer can't return yield, so we can
5816 * remove the bit corresponding to this analyzer from the list.
5817 *
5818 * Note that the intermediate returns and goto found previously
5819 * reset the analyzers.
5820 */
5821 rep->analysers &= ~an_bit;
5822 rep->analyse_exp = TICK_ETERNITY;
5823
Willy Tarreau58975672014-04-24 21:13:57 +02005824 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005825 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005826 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005827
Willy Tarreau58975672014-04-24 21:13:57 +02005828 /*
5829 * Now check for a server cookie.
5830 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005831 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005832 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005833
Willy Tarreau58975672014-04-24 21:13:57 +02005834 /*
5835 * Check for cache-control or pragma headers if required.
5836 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005837 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005838 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005839
Willy Tarreau58975672014-04-24 21:13:57 +02005840 /*
5841 * Add server cookie in the response if needed
5842 */
5843 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5844 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005845 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005846 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5847 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5848 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5849 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5850 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005851 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005852 /* the server is known, it's not the one the client requested, or the
5853 * cookie's last seen date needs to be refreshed. We have to
5854 * insert a set-cookie here, except if we want to insert only on POST
5855 * requests and this one isn't. Note that servers which don't have cookies
5856 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005857 */
Willy Tarreau58975672014-04-24 21:13:57 +02005858 if (!objt_server(s->target)->cookie) {
5859 chunk_printf(&trash,
5860 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5861 s->be->cookie_name);
5862 }
5863 else {
5864 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005865
Willy Tarreau58975672014-04-24 21:13:57 +02005866 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5867 /* emit last_date, which is mandatory */
5868 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5869 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5870 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005871
Willy Tarreau58975672014-04-24 21:13:57 +02005872 if (s->be->cookie_maxlife) {
5873 /* emit first_date, which is either the original one or
5874 * the current date.
5875 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005876 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005877 s30tob64(txn->cookie_first_date ?
5878 txn->cookie_first_date >> 2 :
5879 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005880 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005881 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005882 }
Willy Tarreau58975672014-04-24 21:13:57 +02005883 chunk_appendf(&trash, "; path=/");
5884 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005885
Willy Tarreau58975672014-04-24 21:13:57 +02005886 if (s->be->cookie_domain)
5887 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005888
Willy Tarreau58975672014-04-24 21:13:57 +02005889 if (s->be->ck_opts & PR_CK_HTTPONLY)
5890 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005891
Willy Tarreau58975672014-04-24 21:13:57 +02005892 if (s->be->ck_opts & PR_CK_SECURE)
5893 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005894
Willy Tarreau58975672014-04-24 21:13:57 +02005895 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5896 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005897
Willy Tarreau58975672014-04-24 21:13:57 +02005898 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005899 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005900 /* the server did not change, only the date was updated */
5901 txn->flags |= TX_SCK_UPDATED;
5902 else
5903 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005904
Willy Tarreau58975672014-04-24 21:13:57 +02005905 /* Here, we will tell an eventual cache on the client side that we don't
5906 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5907 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5908 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005909 */
Willy Tarreau58975672014-04-24 21:13:57 +02005910 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005911
Willy Tarreau58975672014-04-24 21:13:57 +02005912 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005913
Willy Tarreau58975672014-04-24 21:13:57 +02005914 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5915 "Cache-control: private", 22) < 0))
5916 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005917 }
Willy Tarreau58975672014-04-24 21:13:57 +02005918 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005919
Willy Tarreau58975672014-04-24 21:13:57 +02005920 /*
5921 * Check if result will be cacheable with a cookie.
5922 * We'll block the response if security checks have caught
5923 * nasty things such as a cacheable cookie.
5924 */
5925 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5926 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5927 (s->be->options & PR_O_CHK_CACHE)) {
5928 /* we're in presence of a cacheable response containing
5929 * a set-cookie header. We'll block it as requested by
5930 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005931 */
Willy Tarreau58975672014-04-24 21:13:57 +02005932 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005933 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005934
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005935 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5936 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005937 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005938 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939
Christopher Faulet767a84b2017-11-24 16:50:31 +01005940 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5941 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005942 send_log(s->be, LOG_ALERT,
5943 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5944 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5945 goto return_srv_prx_502;
5946 }
Willy Tarreau03945942009-12-22 16:50:27 +01005947
Willy Tarreau70730dd2014-04-24 18:06:27 +02005948 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005949 /*
5950 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5951 * If an "Upgrade" token is found, the header is left untouched in order
5952 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005953 * if anything but "Upgrade" is present in the Connection header. We don't
5954 * want to touch any 101 response either since it's switching to another
5955 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005956 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005957 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005958 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005959 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005960 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5961 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005962
Willy Tarreau58975672014-04-24 21:13:57 +02005963 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5964 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5965 /* we want a keep-alive response here. Keep-alive header
5966 * required if either side is not 1.1.
5967 */
5968 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5969 want_flags |= TX_CON_KAL_SET;
5970 }
5971 else {
5972 /* we want a close response here. Close header required if
5973 * the server is 1.1, regardless of the client.
5974 */
5975 if (msg->flags & HTTP_MSGF_VER_11)
5976 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005977 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005978
Willy Tarreau58975672014-04-24 21:13:57 +02005979 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5980 http_change_connection_header(txn, msg, want_flags);
5981 }
5982
5983 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005984 /* Always enter in the body analyzer */
5985 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5986 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005987
Willy Tarreau58975672014-04-24 21:13:57 +02005988 /* if the user wants to log as soon as possible, without counting
5989 * bytes from the server, then this is the right moment. We have
5990 * to temporarily assign bytes_out to log what we currently have.
5991 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005992 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005993 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5994 s->logs.bytes_out = txn->rsp.eoh;
5995 s->do_log(s);
5996 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005997 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005998 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005999}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006000
Willy Tarreaud98cf932009-12-27 22:54:55 +01006001/* This function is an analyser which forwards response body (including chunk
6002 * sizes if any). It is called as soon as we must forward, even if we forward
6003 * zero byte. The only situation where it must not be called is when we're in
6004 * tunnel mode and we want to forward till the close. It's used both to forward
6005 * remaining data and to resync after end of body. It expects the msg_state to
6006 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006007 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006008 *
6009 * It is capable of compressing response data both in content-length mode and
6010 * in chunked mode. The state machines follows different flows depending on
6011 * whether content-length and chunked modes are used, since there are no
6012 * trailers in content-length :
6013 *
6014 * chk-mode cl-mode
6015 * ,----- BODY -----.
6016 * / \
6017 * V size > 0 V chk-mode
6018 * .--> SIZE -------------> DATA -------------> CRLF
6019 * | | size == 0 | last byte |
6020 * | v final crlf v inspected |
6021 * | TRAILERS -----------> DONE |
6022 * | |
6023 * `----------------------------------------------'
6024 *
6025 * Compression only happens in the DATA state, and must be flushed in final
6026 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6027 * is performed at once on final states for all bytes parsed, or when leaving
6028 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006029 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006030int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006031{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006032 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006033 struct http_txn *txn = s->txn;
6034 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006035 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006036
Christopher Faulet814d2702017-03-30 11:33:44 +02006037 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6038 now_ms, __FUNCTION__,
6039 s,
6040 res,
6041 res->rex, res->wex,
6042 res->flags,
6043 res->buf->i,
6044 res->analysers);
6045
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006046 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6047 return 0;
6048
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006049 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006050 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006051 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006052 /* Output closed while we were sending data. We must abort and
6053 * wake the other side up.
6054 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006055 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006056 msg->msg_state = HTTP_MSG_ERROR;
6057 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006058 return 1;
6059 }
6060
Willy Tarreau4fe41902010-06-07 22:27:41 +02006061 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006062 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006063
Christopher Fauletd7c91962015-04-30 11:48:27 +02006064 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006065 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6066 ? HTTP_MSG_CHUNK_SIZE
6067 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006068 }
6069
Willy Tarreauefdf0942014-04-24 20:08:57 +02006070 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006071 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006072 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006073 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006074 }
6075
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006076 if (msg->msg_state < HTTP_MSG_DONE) {
6077 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6078 ? http_msg_forward_chunked_body(s, msg)
6079 : http_msg_forward_body(s, msg));
6080 if (!ret)
6081 goto missing_data_or_waiting;
6082 if (ret < 0)
6083 goto return_bad_res;
6084 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006085
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006086 /* other states, DONE...TUNNEL */
6087 /* for keep-alive we don't want to forward closes on DONE */
6088 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6089 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6090 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006091
Christopher Faulet894da4c2017-07-18 11:29:07 +02006092 http_resync_states(s);
6093 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006094 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6095 if (res->flags & CF_SHUTW) {
6096 /* response errors are most likely due to the
6097 * client aborting the transfer. */
6098 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006099 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006100 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006101 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 +01006102 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006103 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006104 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006105 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006106 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006107
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006108 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006109 if (res->flags & CF_SHUTW)
6110 goto aborted_xfer;
6111
6112 /* stop waiting for data if the input is closed before the end. If the
6113 * client side was already closed, it means that the client has aborted,
6114 * so we don't want to count this as a server abort. Otherwise it's a
6115 * server abort.
6116 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006117 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006118 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006119 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006120 /* If we have some pending data, we continue the processing */
6121 if (!buffer_pending(res->buf)) {
6122 if (!(s->flags & SF_ERR_MASK))
6123 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006124 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006125 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006126 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006127 goto return_bad_res_stats_ok;
6128 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006129 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006130
Willy Tarreau40dba092010-03-04 18:14:51 +01006131 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006132 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006133 goto return_bad_res;
6134
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006135 /* When TE: chunked is used, we need to get there again to parse
6136 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006137 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6138 * or if there are filters registered on the stream, we don't want to
6139 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006140 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006141 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006142 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006143 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6144 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006145 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006146
Willy Tarreau5c620922011-05-11 19:56:11 +02006147 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006148 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006149 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006150 * modes are already handled by the stream sock layer. We must not do
6151 * this in content-length mode because it could present the MSG_MORE
6152 * flag with the last block of forwarded data, which would cause an
6153 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006154 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006155 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006156 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006157
Willy Tarreau87b09662015-04-03 00:22:06 +02006158 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006159 return 0;
6160
Willy Tarreau40dba092010-03-04 18:14:51 +01006161 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006162 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006163 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006164 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006165
6166 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006167 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006168 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006169 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006170 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006171 res->analysers &= AN_RES_FLT_END;
6172 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 +01006173 if (objt_server(s->target))
6174 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006175
Willy Tarreaue7dff022015-04-03 01:14:29 +02006176 if (!(s->flags & SF_ERR_MASK))
6177 s->flags |= SF_ERR_PRXCOND;
6178 if (!(s->flags & SF_FINST_MASK))
6179 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006180 return 0;
6181
6182 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006183 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006184 txn->rsp.msg_state = HTTP_MSG_ERROR;
6185 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006186 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006187 res->analysers &= AN_RES_FLT_END;
6188 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 +01006189
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006190 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6191 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006192 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006193 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006194
Willy Tarreaue7dff022015-04-03 01:14:29 +02006195 if (!(s->flags & SF_ERR_MASK))
6196 s->flags |= SF_ERR_CLICL;
6197 if (!(s->flags & SF_FINST_MASK))
6198 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006199 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006200}
6201
6202
6203static inline int
6204http_msg_forward_body(struct stream *s, struct http_msg *msg)
6205{
6206 struct channel *chn = msg->chn;
6207 int ret;
6208
6209 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6210
6211 if (msg->msg_state == HTTP_MSG_ENDING)
6212 goto ending;
6213
6214 /* Neither content-length, nor transfer-encoding was found, so we must
6215 * read the body until the server connection is closed. In that case, we
6216 * eat data as they come. Of course, this happens for response only. */
6217 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6218 unsigned long long len = (chn->buf->i - msg->next);
6219 msg->chunk_len += len;
6220 msg->body_len += len;
6221 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006222 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6223 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6224 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006225 msg->next += ret;
6226 msg->chunk_len -= ret;
6227 if (msg->chunk_len) {
6228 /* input empty or output full */
6229 if (chn->buf->i > msg->next)
6230 chn->flags |= CF_WAKE_WRITE;
6231 goto missing_data_or_waiting;
6232 }
6233
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006234 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6235 * always set for a request. */
6236 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6237 /* The server still sending data that should be filtered */
6238 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6239 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006240 msg->msg_state = HTTP_MSG_TUNNEL;
6241 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006242 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006243
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006244 msg->msg_state = HTTP_MSG_ENDING;
6245
6246 ending:
6247 /* we may have some pending data starting at res->buf->p such as a last
6248 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006249 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6250 /* default_ret */ msg->next,
6251 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006252 b_adv(chn->buf, ret);
6253 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006254 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6255 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006256 if (msg->next)
6257 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006258
Christopher Fauletda02e172015-12-04 09:25:05 +01006259 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6260 /* default_ret */ 1,
6261 /* on_error */ goto error,
6262 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01006263 if (msg->msg_state == HTTP_MSG_ENDING)
6264 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006265 return 1;
6266
6267 missing_data_or_waiting:
6268 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006269 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6270 /* default_ret */ msg->next,
6271 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006272 b_adv(chn->buf, ret);
6273 msg->next -= ret;
6274 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6275 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006276 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006277 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006278 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006279 return 0;
6280 error:
6281 return -1;
6282}
6283
6284static inline int
6285http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6286{
6287 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006288 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006289 int ret;
6290
6291 /* Here we have the guarantee to be in one of the following state:
6292 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6293 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6294
6295 switch_states:
6296 switch (msg->msg_state) {
6297 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006298 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6299 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6300 /* on_error */ goto error);
6301 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006302 msg->chunk_len -= ret;
6303 if (msg->chunk_len) {
6304 /* input empty or output full */
6305 if (chn->buf->i > msg->next)
6306 chn->flags |= CF_WAKE_WRITE;
6307 goto missing_data_or_waiting;
6308 }
6309
6310 /* nothing left to forward for this chunk*/
6311 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6312 /* fall through for HTTP_MSG_CHUNK_CRLF */
6313
6314 case HTTP_MSG_CHUNK_CRLF:
6315 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006316 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006317 if (ret == 0)
6318 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006319 if (ret < 0) {
6320 msg->err_pos = chn->buf->i + ret;
6321 if (msg->err_pos < 0)
6322 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006323 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006324 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006325 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006326 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6327 /* fall through for HTTP_MSG_CHUNK_SIZE */
6328
6329 case HTTP_MSG_CHUNK_SIZE:
6330 /* read the chunk size and assign it to ->chunk_len,
6331 * then set ->next to point to the body and switch to
6332 * DATA or TRAILERS state.
6333 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006334 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006335 if (ret == 0)
6336 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006337 if (ret < 0) {
6338 msg->err_pos = chn->buf->i + ret;
6339 if (msg->err_pos < 0)
6340 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006341 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006342 }
6343
6344 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006345 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006346 msg->chunk_len = chunk;
6347 msg->body_len += chunk;
6348
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006349 if (msg->chunk_len) {
6350 msg->msg_state = HTTP_MSG_DATA;
6351 goto switch_states;
6352 }
6353 msg->msg_state = HTTP_MSG_TRAILERS;
6354 /* fall through for HTTP_MSG_TRAILERS */
6355
6356 case HTTP_MSG_TRAILERS:
6357 ret = http_forward_trailers(msg);
6358 if (ret < 0)
6359 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006360 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6361 /* default_ret */ 1,
6362 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006363 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006364 if (!ret)
6365 goto missing_data_or_waiting;
6366 break;
6367
6368 case HTTP_MSG_ENDING:
6369 goto ending;
6370
6371 default:
6372 /* This should no happen in this function */
6373 goto error;
6374 }
6375
6376 msg->msg_state = HTTP_MSG_ENDING;
6377 ending:
6378 /* we may have some pending data starting at res->buf->p such as a last
6379 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006380 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006381 /* default_ret */ msg->next,
6382 /* on_error */ goto error);
6383 b_adv(chn->buf, ret);
6384 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006385 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6386 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006387 if (msg->next)
6388 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006389
Christopher Fauletda02e172015-12-04 09:25:05 +01006390 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006391 /* default_ret */ 1,
6392 /* on_error */ goto error,
6393 /* on_wait */ goto waiting);
6394 msg->msg_state = HTTP_MSG_DONE;
6395 return 1;
6396
6397 missing_data_or_waiting:
6398 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006399 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006400 /* default_ret */ msg->next,
6401 /* on_error */ goto error);
6402 b_adv(chn->buf, ret);
6403 msg->next -= ret;
6404 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6405 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006406 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006407 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006408 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006409 return 0;
6410
6411 chunk_parsing_error:
6412 if (msg->err_pos >= 0) {
6413 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006414 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006415 msg->msg_state, strm_fe(s));
6416 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006417 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006418 msg, msg->msg_state, s->be);
6419 }
6420 error:
6421 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006422}
6423
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006425/* Iterate the same filter through all request headers.
6426 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006427 * Since it can manage the switch to another backend, it updates the per-proxy
6428 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006429 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006430int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006431{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 char *cur_ptr, *cur_end, *cur_next;
6433 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006434 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006435 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006436 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006438 last_hdr = 0;
6439
Willy Tarreau9b28e032012-10-12 23:49:43 +02006440 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006441 old_idx = 0;
6442
6443 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006444 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006445 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006446 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447 (exp->action == ACT_ALLOW ||
6448 exp->action == ACT_DENY ||
6449 exp->action == ACT_TARPIT))
6450 return 0;
6451
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006452 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006453 if (!cur_idx)
6454 break;
6455
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006456 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006457 cur_ptr = cur_next;
6458 cur_end = cur_ptr + cur_hdr->len;
6459 cur_next = cur_end + cur_hdr->cr + 1;
6460
6461 /* Now we have one header between cur_ptr and cur_end,
6462 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463 */
6464
Willy Tarreau15a53a42015-01-21 13:39:42 +01006465 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006468 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 last_hdr = 1;
6470 break;
6471
6472 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006473 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006474 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 break;
6476
6477 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006478 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006479 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 break;
6481
6482 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006483 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6484 if (trash.len < 0)
6485 return -1;
6486
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006487 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 /* FIXME: if the user adds a newline in the replacement, the
6489 * index will not be recalculated for now, and the new line
6490 * will not be counted as a new header.
6491 */
6492
6493 cur_end += delta;
6494 cur_next += delta;
6495 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006496 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006497 break;
6498
6499 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006500 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006501 cur_next += delta;
6502
Willy Tarreaufa355d42009-11-29 18:12:29 +01006503 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006504 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6505 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 cur_hdr->len = 0;
6507 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006508 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006509 break;
6510
6511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006512 }
6513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 /* keep the link from this header to next one in case of later
6515 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006516 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006517 old_idx = cur_idx;
6518 }
6519 return 0;
6520}
6521
6522
6523/* Apply the filter to the request line.
6524 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6525 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 * Since it can manage the switch to another backend, it updates the per-proxy
6527 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006528 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006529int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 char *cur_ptr, *cur_end;
6532 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006533 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006534 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006535
Willy Tarreau3d300592007-03-18 18:34:41 +01006536 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006537 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006538 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 (exp->action == ACT_ALLOW ||
6540 exp->action == ACT_DENY ||
6541 exp->action == ACT_TARPIT))
6542 return 0;
6543 else if (exp->action == ACT_REMOVE)
6544 return 0;
6545
6546 done = 0;
6547
Willy Tarreau9b28e032012-10-12 23:49:43 +02006548 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006549 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550
6551 /* Now we have the request line between cur_ptr and cur_end */
6552
Willy Tarreau15a53a42015-01-21 13:39:42 +01006553 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006554 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006556 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006557 done = 1;
6558 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006561 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006562 done = 1;
6563 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006565 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006566 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 done = 1;
6568 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006570 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006571 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6572 if (trash.len < 0)
6573 return -1;
6574
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006575 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006576 /* FIXME: if the user adds a newline in the replacement, the
6577 * index will not be recalculated for now, and the new line
6578 * will not be counted as a new header.
6579 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006580
Willy Tarreaufa355d42009-11-29 18:12:29 +01006581 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006582 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006583 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006584 HTTP_MSG_RQMETH,
6585 cur_ptr, cur_end + 1,
6586 NULL, NULL);
6587 if (unlikely(!cur_end))
6588 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006590 /* we have a full request and we know that we have either a CR
6591 * or an LF at <ptr>.
6592 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006593 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6594 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 /* there is no point trying this regex on headers */
6596 return 1;
6597 }
6598 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006599 return done;
6600}
Willy Tarreau97de6242006-12-27 17:18:38 +01006601
Willy Tarreau58f10d72006-12-04 02:26:12 +01006602
Willy Tarreau58f10d72006-12-04 02:26:12 +01006603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006604/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006605 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006606 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 * unparsable request. Since it can manage the switch to another backend, it
6608 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006609 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006610int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006611{
Willy Tarreau192252e2015-04-04 01:47:55 +02006612 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006613 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006614 struct hdr_exp *exp;
6615
6616 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006617 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006619 /*
6620 * The interleaving of transformations and verdicts
6621 * makes it difficult to decide to continue or stop
6622 * the evaluation.
6623 */
6624
Willy Tarreau6c123b12010-01-28 20:22:06 +01006625 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6626 break;
6627
Willy Tarreau3d300592007-03-18 18:34:41 +01006628 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006629 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006630 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006631 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006632
6633 /* if this filter had a condition, evaluate it now and skip to
6634 * next filter if the condition does not match.
6635 */
6636 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006637 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006638 ret = acl_pass(ret);
6639 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6640 ret = !ret;
6641
6642 if (!ret)
6643 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006644 }
6645
6646 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006647 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006648 if (unlikely(ret < 0))
6649 return -1;
6650
6651 if (likely(ret == 0)) {
6652 /* The filter did not match the request, it can be
6653 * iterated through all headers.
6654 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006655 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6656 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006657 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006658 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006660}
6661
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006662
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006663/* Find the end of a cookie value contained between <s> and <e>. It works the
6664 * same way as with headers above except that the semi-colon also ends a token.
6665 * See RFC2965 for more information. Note that it requires a valid header to
6666 * return a valid result.
6667 */
6668char *find_cookie_value_end(char *s, const char *e)
6669{
6670 int quoted, qdpair;
6671
6672 quoted = qdpair = 0;
6673 for (; s < e; s++) {
6674 if (qdpair) qdpair = 0;
6675 else if (quoted) {
6676 if (*s == '\\') qdpair = 1;
6677 else if (*s == '"') quoted = 0;
6678 }
6679 else if (*s == '"') quoted = 1;
6680 else if (*s == ',' || *s == ';') return s;
6681 }
6682 return s;
6683}
6684
6685/* Delete a value in a header between delimiters <from> and <next> in buffer
6686 * <buf>. The number of characters displaced is returned, and the pointer to
6687 * the first delimiter is updated if required. The function tries as much as
6688 * possible to respect the following principles :
6689 * - replace <from> delimiter by the <next> one unless <from> points to a
6690 * colon, in which case <next> is simply removed
6691 * - set exactly one space character after the new first delimiter, unless
6692 * there are not enough characters in the block being moved to do so.
6693 * - remove unneeded spaces before the previous delimiter and after the new
6694 * one.
6695 *
6696 * It is the caller's responsibility to ensure that :
6697 * - <from> points to a valid delimiter or the colon ;
6698 * - <next> points to a valid delimiter or the final CR/LF ;
6699 * - there are non-space chars before <from> ;
6700 * - there is a CR/LF at or after <next>.
6701 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006702int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006703{
6704 char *prev = *from;
6705
6706 if (*prev == ':') {
6707 /* We're removing the first value, preserve the colon and add a
6708 * space if possible.
6709 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006710 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006711 next++;
6712 prev++;
6713 if (prev < next)
6714 *prev++ = ' ';
6715
Willy Tarreau2235b262016-11-05 15:50:20 +01006716 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006717 next++;
6718 } else {
6719 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006720 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 prev--;
6722 *from = prev;
6723
6724 /* copy the delimiter and if possible a space if we're
6725 * not at the end of the line.
6726 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006727 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 *prev++ = *next++;
6729 if (prev + 1 < next)
6730 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006731 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 next++;
6733 }
6734 }
6735 return buffer_replace2(buf, prev, next, NULL, 0);
6736}
6737
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006738/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006739 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006740 * desirable to call it only when needed. This code is quite complex because
6741 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6742 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006743 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006744void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006745{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006746 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006747 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006748 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006749 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006750 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6751 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006753 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006754 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006755 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006757 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006759 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006761 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006762 hdr_beg = hdr_next;
6763 hdr_end = hdr_beg + cur_hdr->len;
6764 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006765
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006766 /* We have one full header between hdr_beg and hdr_end, and the
6767 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006768 * "Cookie:" headers.
6769 */
6770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006772 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006773 old_idx = cur_idx;
6774 continue;
6775 }
6776
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006777 del_from = NULL; /* nothing to be deleted */
6778 preserve_hdr = 0; /* assume we may kill the whole header */
6779
Willy Tarreau58f10d72006-12-04 02:26:12 +01006780 /* Now look for cookies. Conforming to RFC2109, we have to support
6781 * attributes whose name begin with a '$', and associate them with
6782 * the right cookie, if we want to delete this cookie.
6783 * So there are 3 cases for each cookie read :
6784 * 1) it's a special attribute, beginning with a '$' : ignore it.
6785 * 2) it's a server id cookie that we *MAY* want to delete : save
6786 * some pointers on it (last semi-colon, beginning of cookie...)
6787 * 3) it's an application cookie : we *MAY* have to delete a previous
6788 * "special" cookie.
6789 * At the end of loop, if a "special" cookie remains, we may have to
6790 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 * *MUST* delete it.
6792 *
6793 * Note: RFC2965 is unclear about the processing of spaces around
6794 * the equal sign in the ATTR=VALUE form. A careful inspection of
6795 * the RFC explicitly allows spaces before it, and not within the
6796 * tokens (attrs or values). An inspection of RFC2109 allows that
6797 * too but section 10.1.3 lets one think that spaces may be allowed
6798 * after the equal sign too, resulting in some (rare) buggy
6799 * implementations trying to do that. So let's do what servers do.
6800 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6801 * allowed quoted strings in values, with any possible character
6802 * after a backslash, including control chars and delimitors, which
6803 * causes parsing to become ambiguous. Browsers also allow spaces
6804 * within values even without quotes.
6805 *
6806 * We have to keep multiple pointers in order to support cookie
6807 * removal at the beginning, middle or end of header without
6808 * corrupting the header. All of these headers are valid :
6809 *
6810 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6811 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6812 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6813 * | | | | | | | | |
6814 * | | | | | | | | hdr_end <--+
6815 * | | | | | | | +--> next
6816 * | | | | | | +----> val_end
6817 * | | | | | +-----------> val_beg
6818 * | | | | +--------------> equal
6819 * | | | +----------------> att_end
6820 * | | +---------------------> att_beg
6821 * | +--------------------------> prev
6822 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006823 */
6824
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006825 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6826 /* Iterate through all cookies on this line */
6827
6828 /* find att_beg */
6829 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006830 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006831 att_beg++;
6832
6833 /* find att_end : this is the first character after the last non
6834 * space before the equal. It may be equal to hdr_end.
6835 */
6836 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006837
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006838 while (equal < hdr_end) {
6839 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006840 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006841 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006842 continue;
6843 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006844 }
6845
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006846 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6847 * is between <att_beg> and <equal>, both may be identical.
6848 */
6849
6850 /* look for end of cookie if there is an equal sign */
6851 if (equal < hdr_end && *equal == '=') {
6852 /* look for the beginning of the value */
6853 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006854 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006855 val_beg++;
6856
6857 /* find the end of the value, respecting quotes */
6858 next = find_cookie_value_end(val_beg, hdr_end);
6859
6860 /* make val_end point to the first white space or delimitor after the value */
6861 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006862 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006863 val_end--;
6864 } else {
6865 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006866 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006867
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006868 /* We have nothing to do with attributes beginning with '$'. However,
6869 * they will automatically be removed if a header before them is removed,
6870 * since they're supposed to be linked together.
6871 */
6872 if (*att_beg == '$')
6873 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006874
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006875 /* Ignore cookies with no equal sign */
6876 if (equal == next) {
6877 /* This is not our cookie, so we must preserve it. But if we already
6878 * scheduled another cookie for removal, we cannot remove the
6879 * complete header, but we can remove the previous block itself.
6880 */
6881 preserve_hdr = 1;
6882 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006883 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006884 val_end += delta;
6885 next += delta;
6886 hdr_end += delta;
6887 hdr_next += delta;
6888 cur_hdr->len += delta;
6889 http_msg_move_end(&txn->req, delta);
6890 prev = del_from;
6891 del_from = NULL;
6892 }
6893 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006894 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006895
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 /* if there are spaces around the equal sign, we need to
6897 * strip them otherwise we'll get trouble for cookie captures,
6898 * or even for rewrites. Since this happens extremely rarely,
6899 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6902 int stripped_before = 0;
6903 int stripped_after = 0;
6904
6905 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006906 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006907 equal += stripped_before;
6908 val_beg += stripped_before;
6909 }
6910
6911 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006912 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006913 val_beg += stripped_after;
6914 stripped_before += stripped_after;
6915 }
6916
6917 val_end += stripped_before;
6918 next += stripped_before;
6919 hdr_end += stripped_before;
6920 hdr_next += stripped_before;
6921 cur_hdr->len += stripped_before;
6922 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* First, let's see if we want to capture this cookie. We check
6927 * that we don't already have a client side cookie, because we
6928 * can only capture one. Also as an optimisation, we ignore
6929 * cookies shorter than the declared name.
6930 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006931 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6932 (val_end - att_beg >= sess->fe->capture_namelen) &&
6933 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006934 int log_len = val_end - att_beg;
6935
Willy Tarreaubafbe012017-11-24 17:34:44 +01006936 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006937 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006938 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006939 if (log_len > sess->fe->capture_len)
6940 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 memcpy(txn->cli_cookie, att_beg, log_len);
6942 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945
Willy Tarreaubca99692010-10-06 19:25:55 +02006946 /* Persistence cookies in passive, rewrite or insert mode have the
6947 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006948 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006949 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006950 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006951 * For cookies in prefix mode, the form is :
6952 *
6953 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006954 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006955 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6956 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6957 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006958 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006959
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006960 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6961 * have the server ID between val_beg and delim, and the original cookie between
6962 * delim+1 and val_end. Otherwise, delim==val_end :
6963 *
6964 * Cookie: NAME=SRV; # in all but prefix modes
6965 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6966 * | || || | |+-> next
6967 * | || || | +--> val_end
6968 * | || || +---------> delim
6969 * | || |+------------> val_beg
6970 * | || +-------------> att_end = equal
6971 * | |+-----------------> att_beg
6972 * | +------------------> prev
6973 * +-------------------------> hdr_beg
6974 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006976 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006977 for (delim = val_beg; delim < val_end; delim++)
6978 if (*delim == COOKIE_DELIM)
6979 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006980 } else {
6981 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006982 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006983 /* Now check if the cookie contains a date field, which would
6984 * appear after a vertical bar ('|') just after the server name
6985 * and before the delimiter.
6986 */
6987 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6988 if (vbar1) {
6989 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006990 * right is the last seen date. It is a base64 encoded
6991 * 30-bit value representing the UNIX date since the
6992 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006993 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006994 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006995 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006996 if (val_end - vbar1 >= 5) {
6997 val = b64tos30(vbar1);
6998 if (val > 0)
6999 txn->cookie_last_date = val << 2;
7000 }
7001 /* look for a second vertical bar */
7002 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7003 if (vbar1 && (val_end - vbar1 > 5)) {
7004 val = b64tos30(vbar1 + 1);
7005 if (val > 0)
7006 txn->cookie_first_date = val << 2;
7007 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007008 }
7009 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007010
Willy Tarreauf64d1412010-10-07 20:06:11 +02007011 /* if the cookie has an expiration date and the proxy wants to check
7012 * it, then we do that now. We first check if the cookie is too old,
7013 * then only if it has expired. We detect strict overflow because the
7014 * time resolution here is not great (4 seconds). Cookies with dates
7015 * in the future are ignored if their offset is beyond one day. This
7016 * allows an admin to fix timezone issues without expiring everyone
7017 * and at the same time avoids keeping unwanted side effects for too
7018 * long.
7019 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007020 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7021 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007022 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007023 txn->flags &= ~TX_CK_MASK;
7024 txn->flags |= TX_CK_OLD;
7025 delim = val_beg; // let's pretend we have not found the cookie
7026 txn->cookie_first_date = 0;
7027 txn->cookie_last_date = 0;
7028 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007029 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7030 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007031 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007032 txn->flags &= ~TX_CK_MASK;
7033 txn->flags |= TX_CK_EXPIRED;
7034 delim = val_beg; // let's pretend we have not found the cookie
7035 txn->cookie_first_date = 0;
7036 txn->cookie_last_date = 0;
7037 }
7038
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007039 /* Here, we'll look for the first running server which supports the cookie.
7040 * This allows to share a same cookie between several servers, for example
7041 * to dedicate backup servers to specific servers only.
7042 * However, to prevent clients from sticking to cookie-less backup server
7043 * when they have incidentely learned an empty cookie, we simply ignore
7044 * empty cookies and mark them as invalid.
7045 * The same behaviour is applied when persistence must be ignored.
7046 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007047 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007048 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007050 while (srv) {
7051 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7052 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02007053 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007054 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007055 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007056 /* we found the server and we can use it */
7057 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02007058 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007059 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007060 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007061 break;
7062 } else {
7063 /* we found a server, but it's down,
7064 * mark it as such and go on in case
7065 * another one is available.
7066 */
7067 txn->flags &= ~TX_CK_MASK;
7068 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007070 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 srv = srv->next;
7072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073
Willy Tarreauf64d1412010-10-07 20:06:11 +02007074 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007075 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007076 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007077 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007078 txn->flags |= TX_CK_UNUSED;
7079 else
7080 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007082
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007083 /* depending on the cookie mode, we may have to either :
7084 * - delete the complete cookie if we're in insert+indirect mode, so that
7085 * the server never sees it ;
7086 * - remove the server id from the cookie value, and tag the cookie as an
7087 * application cookie so that it does not get accidentely removed later,
7088 * if we're in cookie prefix mode
7089 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007090 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007091 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007092
Willy Tarreau9b28e032012-10-12 23:49:43 +02007093 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007094 val_end += delta;
7095 next += delta;
7096 hdr_end += delta;
7097 hdr_next += delta;
7098 cur_hdr->len += delta;
7099 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 del_from = NULL;
7102 preserve_hdr = 1; /* we want to keep this cookie */
7103 }
7104 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007105 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007106 del_from = prev;
7107 }
7108 } else {
7109 /* This is not our cookie, so we must preserve it. But if we already
7110 * scheduled another cookie for removal, we cannot remove the
7111 * complete header, but we can remove the previous block itself.
7112 */
7113 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007115 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007116 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007117 if (att_beg >= del_from)
7118 att_beg += delta;
7119 if (att_end >= del_from)
7120 att_end += delta;
7121 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007122 val_end += delta;
7123 next += delta;
7124 hdr_end += delta;
7125 hdr_next += delta;
7126 cur_hdr->len += delta;
7127 http_msg_move_end(&txn->req, delta);
7128 prev = del_from;
7129 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007131 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007132
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007133 /* continue with next cookie on this header line */
7134 att_beg = next;
7135 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007136
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007137 /* There are no more cookies on this line.
7138 * We may still have one (or several) marked for deletion at the
7139 * end of the line. We must do this now in two ways :
7140 * - if some cookies must be preserved, we only delete from the
7141 * mark to the end of line ;
7142 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007143 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007144 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007146 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007147 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007148 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007149 cur_hdr->len += delta;
7150 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007151 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152
7153 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007154 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7155 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007156 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007157 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007158 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007159 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007160 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161 }
7162
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007163 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007166}
7167
7168
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169/* Iterate the same filter through all response headers contained in <rtr>.
7170 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7171 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007172int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 char *cur_ptr, *cur_end, *cur_next;
7175 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007176 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007178 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179
7180 last_hdr = 0;
7181
Willy Tarreau9b28e032012-10-12 23:49:43 +02007182 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 old_idx = 0;
7184
7185 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007186 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007188 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189 (exp->action == ACT_ALLOW ||
7190 exp->action == ACT_DENY))
7191 return 0;
7192
7193 cur_idx = txn->hdr_idx.v[old_idx].next;
7194 if (!cur_idx)
7195 break;
7196
7197 cur_hdr = &txn->hdr_idx.v[cur_idx];
7198 cur_ptr = cur_next;
7199 cur_end = cur_ptr + cur_hdr->len;
7200 cur_next = cur_end + cur_hdr->cr + 1;
7201
7202 /* Now we have one header between cur_ptr and cur_end,
7203 * and the next header starts at cur_next.
7204 */
7205
Willy Tarreau15a53a42015-01-21 13:39:42 +01007206 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 switch (exp->action) {
7208 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007209 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007210 last_hdr = 1;
7211 break;
7212
7213 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007214 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007215 last_hdr = 1;
7216 break;
7217
7218 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007219 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7220 if (trash.len < 0)
7221 return -1;
7222
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007223 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224 /* FIXME: if the user adds a newline in the replacement, the
7225 * index will not be recalculated for now, and the new line
7226 * will not be counted as a new header.
7227 */
7228
7229 cur_end += delta;
7230 cur_next += delta;
7231 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007232 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 break;
7234
7235 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007236 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 cur_next += delta;
7238
Willy Tarreaufa355d42009-11-29 18:12:29 +01007239 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7241 txn->hdr_idx.used--;
7242 cur_hdr->len = 0;
7243 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007244 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 break;
7246
7247 }
7248 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249
7250 /* keep the link from this header to next one in case of later
7251 * removal of next header.
7252 */
7253 old_idx = cur_idx;
7254 }
7255 return 0;
7256}
7257
7258
7259/* Apply the filter to the status line in the response buffer <rtr>.
7260 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7261 * or -1 if a replacement resulted in an invalid status line.
7262 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007263int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007265 char *cur_ptr, *cur_end;
7266 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007267 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007268 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269
7270
Willy Tarreau3d300592007-03-18 18:34:41 +01007271 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 (exp->action == ACT_ALLOW ||
7275 exp->action == ACT_DENY))
7276 return 0;
7277 else if (exp->action == ACT_REMOVE)
7278 return 0;
7279
7280 done = 0;
7281
Willy Tarreau9b28e032012-10-12 23:49:43 +02007282 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007283 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284
7285 /* Now we have the status line between cur_ptr and cur_end */
7286
Willy Tarreau15a53a42015-01-21 13:39:42 +01007287 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288 switch (exp->action) {
7289 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007290 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 done = 1;
7292 break;
7293
7294 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007295 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296 done = 1;
7297 break;
7298
7299 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007300 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7301 if (trash.len < 0)
7302 return -1;
7303
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007304 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305 /* FIXME: if the user adds a newline in the replacement, the
7306 * index will not be recalculated for now, and the new line
7307 * will not be counted as a new header.
7308 */
7309
Willy Tarreaufa355d42009-11-29 18:12:29 +01007310 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007311 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007312 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007313 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 cur_ptr, cur_end + 1,
7315 NULL, NULL);
7316 if (unlikely(!cur_end))
7317 return -1;
7318
7319 /* we have a full respnse and we know that we have either a CR
7320 * or an LF at <ptr>.
7321 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007322 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007323 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 /* there is no point trying this regex on headers */
7325 return 1;
7326 }
7327 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007328 return done;
7329}
7330
7331
7332
7333/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007334 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7336 * unparsable response.
7337 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007338int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339{
Willy Tarreau192252e2015-04-04 01:47:55 +02007340 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007341 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007342 struct hdr_exp *exp;
7343
7344 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345 int ret;
7346
7347 /*
7348 * The interleaving of transformations and verdicts
7349 * makes it difficult to decide to continue or stop
7350 * the evaluation.
7351 */
7352
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007353 if (txn->flags & TX_SVDENY)
7354 break;
7355
Willy Tarreau3d300592007-03-18 18:34:41 +01007356 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007357 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7358 exp->action == ACT_PASS)) {
7359 exp = exp->next;
7360 continue;
7361 }
7362
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007363 /* if this filter had a condition, evaluate it now and skip to
7364 * next filter if the condition does not match.
7365 */
7366 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007367 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007368 ret = acl_pass(ret);
7369 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7370 ret = !ret;
7371 if (!ret)
7372 continue;
7373 }
7374
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007376 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377 if (unlikely(ret < 0))
7378 return -1;
7379
7380 if (likely(ret == 0)) {
7381 /* The filter did not match the response, it can be
7382 * iterated through all headers.
7383 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007384 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7385 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007386 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007387 }
7388 return 0;
7389}
7390
7391
Willy Tarreaua15645d2007-03-18 16:22:39 +01007392/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007393 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007394 * desirable to call it only when needed. This function is also used when we
7395 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007397void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007399 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007400 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007401 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007402 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007404 char *hdr_beg, *hdr_end, *hdr_next;
7405 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 /* Iterate through the headers.
7408 * we start with the start line.
7409 */
7410 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007411 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412
7413 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7414 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007415 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007416
7417 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007418 hdr_beg = hdr_next;
7419 hdr_end = hdr_beg + cur_hdr->len;
7420 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007421
Willy Tarreau24581ba2010-08-31 22:39:35 +02007422 /* We have one full header between hdr_beg and hdr_end, and the
7423 * next header starts at hdr_next. We're only interested in
7424 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007425 */
7426
Willy Tarreau24581ba2010-08-31 22:39:35 +02007427 is_cookie2 = 0;
7428 prev = hdr_beg + 10;
7429 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007430 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7432 if (!val) {
7433 old_idx = cur_idx;
7434 continue;
7435 }
7436 is_cookie2 = 1;
7437 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007438 }
7439
Willy Tarreau24581ba2010-08-31 22:39:35 +02007440 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7441 * <prev> points to the colon.
7442 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007443 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444
Willy Tarreau24581ba2010-08-31 22:39:35 +02007445 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7446 * check-cache is enabled) and we are not interested in checking
7447 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007448 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007449 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007450 return;
7451
Willy Tarreau24581ba2010-08-31 22:39:35 +02007452 /* OK so now we know we have to process this response cookie.
7453 * The format of the Set-Cookie header is slightly different
7454 * from the format of the Cookie header in that it does not
7455 * support the comma as a cookie delimiter (thus the header
7456 * cannot be folded) because the Expires attribute described in
7457 * the original Netscape's spec may contain an unquoted date
7458 * with a comma inside. We have to live with this because
7459 * many browsers don't support Max-Age and some browsers don't
7460 * support quoted strings. However the Set-Cookie2 header is
7461 * clean.
7462 *
7463 * We have to keep multiple pointers in order to support cookie
7464 * removal at the beginning, middle or end of header without
7465 * corrupting the header (in case of set-cookie2). A special
7466 * pointer, <scav> points to the beginning of the set-cookie-av
7467 * fields after the first semi-colon. The <next> pointer points
7468 * either to the end of line (set-cookie) or next unquoted comma
7469 * (set-cookie2). All of these headers are valid :
7470 *
7471 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7472 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7473 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7474 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7475 * | | | | | | | | | |
7476 * | | | | | | | | +-> next hdr_end <--+
7477 * | | | | | | | +------------> scav
7478 * | | | | | | +--------------> val_end
7479 * | | | | | +--------------------> val_beg
7480 * | | | | +----------------------> equal
7481 * | | | +------------------------> att_end
7482 * | | +----------------------------> att_beg
7483 * | +------------------------------> prev
7484 * +-----------------------------------------> hdr_beg
7485 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007486
Willy Tarreau24581ba2010-08-31 22:39:35 +02007487 for (; prev < hdr_end; prev = next) {
7488 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007489
Willy Tarreau24581ba2010-08-31 22:39:35 +02007490 /* find att_beg */
7491 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007492 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007493 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007494
Willy Tarreau24581ba2010-08-31 22:39:35 +02007495 /* find att_end : this is the first character after the last non
7496 * space before the equal. It may be equal to hdr_end.
7497 */
7498 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007499
Willy Tarreau24581ba2010-08-31 22:39:35 +02007500 while (equal < hdr_end) {
7501 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7502 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007503 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007504 continue;
7505 att_end = equal;
7506 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007507
Willy Tarreau24581ba2010-08-31 22:39:35 +02007508 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7509 * is between <att_beg> and <equal>, both may be identical.
7510 */
7511
7512 /* look for end of cookie if there is an equal sign */
7513 if (equal < hdr_end && *equal == '=') {
7514 /* look for the beginning of the value */
7515 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007516 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007517 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518
Willy Tarreau24581ba2010-08-31 22:39:35 +02007519 /* find the end of the value, respecting quotes */
7520 next = find_cookie_value_end(val_beg, hdr_end);
7521
7522 /* make val_end point to the first white space or delimitor after the value */
7523 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007524 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007525 val_end--;
7526 } else {
7527 /* <equal> points to next comma, semi-colon or EOL */
7528 val_beg = val_end = next = equal;
7529 }
7530
7531 if (next < hdr_end) {
7532 /* Set-Cookie2 supports multiple cookies, and <next> points to
7533 * a colon or semi-colon before the end. So skip all attr-value
7534 * pairs and look for the next comma. For Set-Cookie, since
7535 * commas are permitted in values, skip to the end.
7536 */
7537 if (is_cookie2)
7538 next = find_hdr_value_end(next, hdr_end);
7539 else
7540 next = hdr_end;
7541 }
7542
7543 /* Now everything is as on the diagram above */
7544
7545 /* Ignore cookies with no equal sign */
7546 if (equal == val_end)
7547 continue;
7548
7549 /* If there are spaces around the equal sign, we need to
7550 * strip them otherwise we'll get trouble for cookie captures,
7551 * or even for rewrites. Since this happens extremely rarely,
7552 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007553 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007554 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7555 int stripped_before = 0;
7556 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557
Willy Tarreau24581ba2010-08-31 22:39:35 +02007558 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007559 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007560 equal += stripped_before;
7561 val_beg += stripped_before;
7562 }
7563
7564 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007565 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007566 val_beg += stripped_after;
7567 stripped_before += stripped_after;
7568 }
7569
7570 val_end += stripped_before;
7571 next += stripped_before;
7572 hdr_end += stripped_before;
7573 hdr_next += stripped_before;
7574 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007575 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007576 }
7577
7578 /* First, let's see if we want to capture this cookie. We check
7579 * that we don't already have a server side cookie, because we
7580 * can only capture one. Also as an optimisation, we ignore
7581 * cookies shorter than the declared name.
7582 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007583 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007584 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007585 (val_end - att_beg >= sess->fe->capture_namelen) &&
7586 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007587 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007588 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007589 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007590 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007591 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007592 if (log_len > sess->fe->capture_len)
7593 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007594 memcpy(txn->srv_cookie, att_beg, log_len);
7595 txn->srv_cookie[log_len] = 0;
7596 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007597 }
7598
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007599 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007601 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007602 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7603 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007604 /* assume passive cookie by default */
7605 txn->flags &= ~TX_SCK_MASK;
7606 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007607
7608 /* If the cookie is in insert mode on a known server, we'll delete
7609 * this occurrence because we'll insert another one later.
7610 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007611 * a direct access.
7612 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007613 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007614 /* The "preserve" flag was set, we don't want to touch the
7615 * server's cookie.
7616 */
7617 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007618 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007619 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007620 /* this cookie must be deleted */
7621 if (*prev == ':' && next == hdr_end) {
7622 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007623 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007624 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7625 txn->hdr_idx.used--;
7626 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007627 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007628 hdr_next += delta;
7629 http_msg_move_end(&txn->rsp, delta);
7630 /* note: while both invalid now, <next> and <hdr_end>
7631 * are still equal, so the for() will stop as expected.
7632 */
7633 } else {
7634 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007635 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007636 next = prev;
7637 hdr_end += delta;
7638 hdr_next += delta;
7639 cur_hdr->len += delta;
7640 http_msg_move_end(&txn->rsp, delta);
7641 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007642 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007643 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007644 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007645 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007646 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007647 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007648 * with this server since we know it.
7649 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007650 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007651 next += delta;
7652 hdr_end += delta;
7653 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007655 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656
Willy Tarreauf1348312010-10-07 15:54:11 +02007657 txn->flags &= ~TX_SCK_MASK;
7658 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007660 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007661 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007662 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007663 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007664 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007665 next += delta;
7666 hdr_end += delta;
7667 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007668 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007669 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670
Willy Tarreau827aee92011-03-10 16:55:02 +01007671 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007672 txn->flags &= ~TX_SCK_MASK;
7673 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674 }
7675 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007676 /* that's done for this cookie, check the next one on the same
7677 * line when next != hdr_end (only if is_cookie2).
7678 */
7679 }
7680 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007682 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683}
7684
7685
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007687 * Parses the Cache-Control and Pragma request header fields to determine if
7688 * the request may be served from the cache and/or if it is cacheable. Updates
7689 * s->txn->flags.
7690 */
7691void check_request_for_cacheability(struct stream *s, struct channel *chn)
7692{
7693 struct http_txn *txn = s->txn;
7694 char *p1, *p2;
7695 char *cur_ptr, *cur_end, *cur_next;
7696 int pragma_found;
7697 int cc_found;
7698 int cur_idx;
7699
7700 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7701 return; /* nothing more to do here */
7702
7703 cur_idx = 0;
7704 pragma_found = cc_found = 0;
7705 cur_next = chn->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
7706
7707 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7708 struct hdr_idx_elem *cur_hdr;
7709 int val;
7710
7711 cur_hdr = &txn->hdr_idx.v[cur_idx];
7712 cur_ptr = cur_next;
7713 cur_end = cur_ptr + cur_hdr->len;
7714 cur_next = cur_end + cur_hdr->cr + 1;
7715
7716 /* We have one full header between cur_ptr and cur_end, and the
7717 * next header starts at cur_next.
7718 */
7719
7720 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7721 if (val) {
7722 if ((cur_end - (cur_ptr + val) >= 8) &&
7723 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7724 pragma_found = 1;
7725 continue;
7726 }
7727 }
7728
7729 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7730 if (!val)
7731 continue;
7732
7733 /* OK, right now we know we have a cache-control header at cur_ptr */
7734 cc_found = 1;
7735 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7736
7737 if (p1 >= cur_end) /* no more info */
7738 continue;
7739
7740 /* p1 is at the beginning of the value */
7741 p2 = p1;
7742 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7743 p2++;
7744
7745 /* we have a complete value between p1 and p2. We don't check the
7746 * values after max-age, max-stale nor min-fresh, we simply don't
7747 * use the cache when they're specified.
7748 */
7749 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7750 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7751 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7752 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7753 txn->flags |= TX_CACHE_IGNORE;
7754 continue;
7755 }
7756
7757 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7758 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7759 continue;
7760 }
7761 }
7762
7763 /* RFC7234#5.4:
7764 * When the Cache-Control header field is also present and
7765 * understood in a request, Pragma is ignored.
7766 * When the Cache-Control header field is not present in a
7767 * request, caches MUST consider the no-cache request
7768 * pragma-directive as having the same effect as if
7769 * "Cache-Control: no-cache" were present.
7770 */
7771 if (!cc_found && pragma_found)
7772 txn->flags |= TX_CACHE_IGNORE;
7773}
7774
7775/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007776 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007778void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007780 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781 char *p1, *p2;
7782
7783 char *cur_ptr, *cur_end, *cur_next;
7784 int cur_idx;
7785
Willy Tarreau12b32f22017-12-21 16:08:09 +01007786 if (txn->status < 200) {
7787 /* do not try to cache interim responses! */
7788 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007789 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007790 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791
7792 /* Iterate through the headers.
7793 * we start with the start line.
7794 */
7795 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007796 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797
7798 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7799 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007800 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007801
7802 cur_hdr = &txn->hdr_idx.v[cur_idx];
7803 cur_ptr = cur_next;
7804 cur_end = cur_ptr + cur_hdr->len;
7805 cur_next = cur_end + cur_hdr->cr + 1;
7806
7807 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007808 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 */
7810
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007811 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7812 if (val) {
7813 if ((cur_end - (cur_ptr + val) >= 8) &&
7814 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7815 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7816 return;
7817 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818 }
7819
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007820 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7821 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007822 continue;
7823
7824 /* OK, right now we know we have a cache-control header at cur_ptr */
7825
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007826 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007827
7828 if (p1 >= cur_end) /* no more info */
7829 continue;
7830
7831 /* p1 is at the beginning of the value */
7832 p2 = p1;
7833
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007834 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 p2++;
7836
7837 /* we have a complete value between p1 and p2 */
7838 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007839 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7840 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7841 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7842 continue;
7843 }
7844
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 /* we have something of the form no-cache="set-cookie" */
7846 if ((cur_end - p1 >= 21) &&
7847 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7848 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007849 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 continue;
7851 }
7852
7853 /* OK, so we know that either p2 points to the end of string or to a comma */
7854 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007855 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007856 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007857 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 return;
7859 }
7860
7861 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007862 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007863 continue;
7864 }
7865 }
7866}
7867
Willy Tarreau58f10d72006-12-04 02:26:12 +01007868
Willy Tarreaub2513902006-12-17 14:52:38 +01007869/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007870 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007871 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007872 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007873 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007874 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007875 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007876 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007877 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007878int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007879{
7880 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007881 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007882 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007883
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007884 if (!uri_auth)
7885 return 0;
7886
Cyril Bonté70be45d2010-10-12 00:14:35 +02007887 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007888 return 0;
7889
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007890 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007891 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007892 return 0;
7893
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007894 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007895 return 0;
7896
Willy Tarreaub2513902006-12-17 14:52:38 +01007897 return 1;
7898}
7899
Willy Tarreau4076a152009-04-02 15:18:36 +02007900/*
7901 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007902 * By default it tries to report the error position as msg->err_pos. However if
7903 * this one is not set, it will then report msg->next, which is the last known
7904 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007905 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007906 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007907void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007908 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007909 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007910{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007911 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007912 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007913 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007914
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007915 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007916 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007917 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007918 len1 = MIN(len1, es->len);
7919 len2 = es->len - len1; /* remaining data if buffer wraps */
7920
Willy Tarreauf3764b72016-03-31 13:45:10 +02007921 if (!es->buf)
7922 es->buf = malloc(global.tune.bufsize);
7923
7924 if (es->buf) {
7925 memcpy(es->buf, chn->buf->p, len1);
7926 if (len2)
7927 memcpy(es->buf + len1, chn->buf->data, len2);
7928 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007929
Willy Tarreau4076a152009-04-02 15:18:36 +02007930 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007931 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007932 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007933 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007934
Willy Tarreau4076a152009-04-02 15:18:36 +02007935 es->when = date; // user-visible date
7936 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007937 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007938 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007939 if (objt_conn(sess->origin))
7940 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007941 else
7942 memset(&es->src, 0, sizeof(es->src));
7943
Willy Tarreau078272e2010-12-12 12:46:33 +01007944 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007945 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007946 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007947 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007948 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007949 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007950 es->b_out = chn->buf->o;
7951 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007952 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007953 es->m_clen = msg->chunk_len;
7954 es->m_blen = msg->body_len;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007955 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007956}
Willy Tarreaub2513902006-12-17 14:52:38 +01007957
Willy Tarreau294c4732011-12-16 21:35:50 +01007958/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7959 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7960 * performed over the whole headers. Otherwise it must contain a valid header
7961 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7962 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7963 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7964 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007965 * -1. The value fetch stops at commas, so this function is suited for use with
7966 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007967 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007968 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007969unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007970 struct hdr_idx *idx, int occ,
7971 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007972{
Willy Tarreau294c4732011-12-16 21:35:50 +01007973 struct hdr_ctx local_ctx;
7974 char *ptr_hist[MAX_HDR_HISTORY];
7975 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007976 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007977 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007978
Willy Tarreau294c4732011-12-16 21:35:50 +01007979 if (!ctx) {
7980 local_ctx.idx = 0;
7981 ctx = &local_ctx;
7982 }
7983
Willy Tarreaubce70882009-09-07 11:51:47 +02007984 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007985 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007986 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007987 occ--;
7988 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007989 *vptr = ctx->line + ctx->val;
7990 *vlen = ctx->vlen;
7991 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007992 }
7993 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007994 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007995 }
7996
7997 /* negative occurrence, we scan all the list then walk back */
7998 if (-occ > MAX_HDR_HISTORY)
7999 return 0;
8000
Willy Tarreau294c4732011-12-16 21:35:50 +01008001 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008002 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008003 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8004 len_hist[hist_ptr] = ctx->vlen;
8005 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008006 hist_ptr = 0;
8007 found++;
8008 }
8009 if (-occ > found)
8010 return 0;
8011 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008012 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8013 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8014 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008015 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008016 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008017 if (hist_ptr >= MAX_HDR_HISTORY)
8018 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008019 *vptr = ptr_hist[hist_ptr];
8020 *vlen = len_hist[hist_ptr];
8021 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008022}
8023
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008024/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8025 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8026 * performed over the whole headers. Otherwise it must contain a valid header
8027 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8028 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8029 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8030 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8031 * -1. This function differs from http_get_hdr() in that it only returns full
8032 * line header values and does not stop at commas.
8033 * The return value is 0 if nothing was found, or non-zero otherwise.
8034 */
8035unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8036 struct hdr_idx *idx, int occ,
8037 struct hdr_ctx *ctx, char **vptr, int *vlen)
8038{
8039 struct hdr_ctx local_ctx;
8040 char *ptr_hist[MAX_HDR_HISTORY];
8041 int len_hist[MAX_HDR_HISTORY];
8042 unsigned int hist_ptr;
8043 int found;
8044
8045 if (!ctx) {
8046 local_ctx.idx = 0;
8047 ctx = &local_ctx;
8048 }
8049
8050 if (occ >= 0) {
8051 /* search from the beginning */
8052 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8053 occ--;
8054 if (occ <= 0) {
8055 *vptr = ctx->line + ctx->val;
8056 *vlen = ctx->vlen;
8057 return 1;
8058 }
8059 }
8060 return 0;
8061 }
8062
8063 /* negative occurrence, we scan all the list then walk back */
8064 if (-occ > MAX_HDR_HISTORY)
8065 return 0;
8066
8067 found = hist_ptr = 0;
8068 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8069 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8070 len_hist[hist_ptr] = ctx->vlen;
8071 if (++hist_ptr >= MAX_HDR_HISTORY)
8072 hist_ptr = 0;
8073 found++;
8074 }
8075 if (-occ > found)
8076 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008077
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008078 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008079 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8080 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8081 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008082 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008083 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008084 if (hist_ptr >= MAX_HDR_HISTORY)
8085 hist_ptr -= MAX_HDR_HISTORY;
8086 *vptr = ptr_hist[hist_ptr];
8087 *vlen = len_hist[hist_ptr];
8088 return 1;
8089}
8090
Willy Tarreaubaaee002006-06-26 02:48:02 +02008091/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008092 * Print a debug line with a header. Always stop at the first CR or LF char,
8093 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8094 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008095 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008096void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008097{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008098 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008099 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008100
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008101 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008102 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02008103 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02008104 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 +02008105
8106 for (max = 0; start + max < end; max++)
8107 if (start[max] == '\r' || start[max] == '\n')
8108 break;
8109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008110 UBOUND(max, trash.size - trash.len - 3);
8111 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8112 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008113 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008114}
8115
Willy Tarreaueee5b512015-04-03 23:46:31 +02008116
8117/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8118 * The hdr_idx is allocated as well. In case of allocation failure, everything
8119 * allocated is freed and NULL is returned. Otherwise the new transaction is
8120 * assigned to the stream and returned.
8121 */
8122struct http_txn *http_alloc_txn(struct stream *s)
8123{
8124 struct http_txn *txn = s->txn;
8125
8126 if (txn)
8127 return txn;
8128
Willy Tarreaubafbe012017-11-24 17:34:44 +01008129 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008130 if (!txn)
8131 return txn;
8132
8133 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01008134 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008135 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01008136 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02008137 return NULL;
8138 }
8139
8140 s->txn = txn;
8141 return txn;
8142}
8143
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008144void http_txn_reset_req(struct http_txn *txn)
8145{
8146 txn->req.flags = 0;
8147 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8148 txn->req.next = 0;
8149 txn->req.chunk_len = 0LL;
8150 txn->req.body_len = 0LL;
8151 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8152}
8153
8154void http_txn_reset_res(struct http_txn *txn)
8155{
8156 txn->rsp.flags = 0;
8157 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8158 txn->rsp.next = 0;
8159 txn->rsp.chunk_len = 0LL;
8160 txn->rsp.body_len = 0LL;
8161 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8162}
8163
Willy Tarreau0937bc42009-12-22 15:03:09 +01008164/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008165 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008166 * the required fields are properly allocated and that we only need to (re)init
8167 * them. This should be used before processing any new request.
8168 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008169void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008170{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008171 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008172 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008173
8174 txn->flags = 0;
8175 txn->status = -1;
8176
Willy Tarreauf64d1412010-10-07 20:06:11 +02008177 txn->cookie_first_date = 0;
8178 txn->cookie_last_date = 0;
8179
Willy Tarreaueee5b512015-04-03 23:46:31 +02008180 txn->srv_cookie = NULL;
8181 txn->cli_cookie = NULL;
8182 txn->uri = NULL;
8183
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008184 http_txn_reset_req(txn);
8185 http_txn_reset_res(txn);
8186
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008187 txn->req.chn = &s->req;
8188 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008189
8190 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008191
8192 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8193 if (fe->options2 & PR_O2_REQBUG_OK)
8194 txn->req.err_pos = -1; /* let buggy requests pass */
8195
Willy Tarreau0937bc42009-12-22 15:03:09 +01008196 if (txn->hdr_idx.v)
8197 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008198
8199 vars_init(&s->vars_txn, SCOPE_TXN);
8200 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008201}
8202
8203/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008204void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008205{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008206 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008207 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008208
8209 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01008210 pool_free(pool_head_requri, txn->uri);
8211 pool_free(pool_head_capture, txn->cli_cookie);
8212 pool_free(pool_head_capture, txn->srv_cookie);
8213 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008214
William Lallemanda73203e2012-03-12 12:48:57 +01008215 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008216 txn->uri = NULL;
8217 txn->srv_cookie = NULL;
8218 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008219
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008220 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008221 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008222 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008223 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008224 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008225 }
8226
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008227 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008228 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008229 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01008230 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008231 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008232 }
8233
Willy Tarreau6204cd92016-03-10 16:33:04 +01008234 vars_prune(&s->vars_txn, s->sess, s);
8235 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008236}
8237
8238/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008239void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008240{
8241 http_end_txn(s);
8242 http_init_txn(s);
8243
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008244 /* reinitialise the current rule list pointer to NULL. We are sure that
8245 * any rulelist match the NULL pointer.
8246 */
8247 s->current_rule_list = NULL;
8248
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008249 s->be = strm_fe(s);
8250 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008251 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008252 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008253 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008254 /* re-init store persistence */
8255 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008256 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008257
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008258 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008259
Willy Tarreau739cfba2010-01-25 23:11:14 +01008260 /* We must trim any excess data from the response buffer, because we
8261 * may have blocked an invalid response from a server that we don't
8262 * want to accidentely forward once we disable the analysers, nor do
8263 * we want those data to come along with next response. A typical
8264 * example of such data would be from a buggy server responding to
8265 * a HEAD with some data, or sending more than the advertised
8266 * content-length.
8267 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008268 if (unlikely(s->res.buf->i))
8269 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008270
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008271 /* Now we can realign the response buffer */
8272 buffer_realign(s->res.buf);
8273
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008274 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008275 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008276
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008277 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008278 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008279
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008280 s->req.rex = TICK_ETERNITY;
8281 s->req.wex = TICK_ETERNITY;
8282 s->req.analyse_exp = TICK_ETERNITY;
8283 s->res.rex = TICK_ETERNITY;
8284 s->res.wex = TICK_ETERNITY;
8285 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008286 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008287}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008288
Sasha Pachev218f0642014-06-16 12:05:59 -06008289void free_http_res_rules(struct list *r)
8290{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008291 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008292
8293 list_for_each_entry_safe(pr, tr, r, list) {
8294 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008295 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008296 free(pr);
8297 }
8298}
8299
8300void free_http_req_rules(struct list *r)
8301{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008302 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008303
8304 list_for_each_entry_safe(pr, tr, r, list) {
8305 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008306 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008307 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008308
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008309 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008310 free(pr);
8311 }
8312}
8313
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008314/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008315struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008316{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008317 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008318 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008319 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008320 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008321
Vincent Bernat02779b62016-04-03 13:48:43 +02008322 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008323 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008324 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008325 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008326 }
8327
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008328 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008329 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008330 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008331 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008332 int code;
8333 int hc;
8334
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008335 if (!strcmp(args[0], "tarpit")) {
8336 rule->action = ACT_HTTP_REQ_TARPIT;
8337 rule->deny_status = HTTP_ERR_500;
8338 }
8339 else {
8340 rule->action = ACT_ACTION_DENY;
8341 rule->deny_status = HTTP_ERR_403;
8342 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008343 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008344 if (strcmp(args[cur_arg], "deny_status") == 0) {
8345 cur_arg++;
8346 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008347 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8348 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008349 goto out_err;
8350 }
8351
8352 code = atol(args[cur_arg]);
8353 cur_arg++;
8354 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8355 if (http_err_codes[hc] == code) {
8356 rule->deny_status = hc;
8357 break;
8358 }
8359 }
8360
8361 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008362 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8363 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008364 }
8365 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008366 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008367 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008368 cur_arg = 1;
8369
8370 while(*args[cur_arg]) {
8371 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008372 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008373 cur_arg+=2;
8374 continue;
8375 } else
8376 break;
8377 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008378 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008379 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008380 cur_arg = 1;
8381
8382 if (!*args[cur_arg] ||
8383 (*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 +01008384 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8385 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008386 goto out_err;
8387 }
8388 rule->arg.nice = atoi(args[cur_arg]);
8389 if (rule->arg.nice < -1024)
8390 rule->arg.nice = -1024;
8391 else if (rule->arg.nice > 1024)
8392 rule->arg.nice = 1024;
8393 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008394 } else if (!strcmp(args[0], "set-tos")) {
8395#ifdef IP_TOS
8396 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008397 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008398 cur_arg = 1;
8399
8400 if (!*args[cur_arg] ||
8401 (*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 +01008402 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8403 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008404 goto out_err;
8405 }
8406
8407 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8408 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008409 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8410 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008411 goto out_err;
8412 }
8413 cur_arg++;
8414#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008415 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 +02008416 goto out_err;
8417#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008418 } else if (!strcmp(args[0], "set-mark")) {
8419#ifdef SO_MARK
8420 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008421 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008422 cur_arg = 1;
8423
8424 if (!*args[cur_arg] ||
8425 (*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 +01008426 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8427 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008428 goto out_err;
8429 }
8430
8431 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8432 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008433 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8434 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008435 goto out_err;
8436 }
8437 cur_arg++;
8438 global.last_checks |= LSTCHK_NETADM;
8439#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008440 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 +02008441 goto out_err;
8442#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008443 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008444 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008445 cur_arg = 1;
8446
8447 if (!*args[cur_arg] ||
8448 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8449 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008450 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8451 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008452 goto out_err;
8453 }
8454 if (strcmp(args[cur_arg], "silent") == 0)
8455 rule->arg.loglevel = -1;
8456 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8457 goto bad_log_level;
8458 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008459 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008460 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008461 cur_arg = 1;
8462
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008463 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8464 (*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 +01008465 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8466 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008467 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008468 }
8469
8470 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8471 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8472 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008473
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008474 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008475 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008476 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 +01008477 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008478 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8479 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008480 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008481 goto out_err;
8482 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008483 free(proxy->conf.lfs_file);
8484 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8485 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008486 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008487 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008488 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008489 cur_arg = 1;
8490
8491 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008492 (*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 +01008493 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8494 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008495 goto out_err;
8496 }
8497
8498 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8499 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8500 LIST_INIT(&rule->arg.hdr_add.fmt);
8501
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008502 error = NULL;
8503 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008504 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8505 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008506 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008507 goto out_err;
8508 }
8509
8510 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008511 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008512 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 +01008513 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008514 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8515 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008516 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008517 goto out_err;
8518 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008519
8520 free(proxy->conf.lfs_file);
8521 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8522 proxy->conf.lfs_line = proxy->conf.args.line;
8523 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008524 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008525 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008526 cur_arg = 1;
8527
8528 if (!*args[cur_arg] ||
8529 (*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 +01008530 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8531 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008532 goto out_err;
8533 }
8534
8535 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8536 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8537
8538 proxy->conf.args.ctx = ARGC_HRQ;
8539 free(proxy->conf.lfs_file);
8540 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8541 proxy->conf.lfs_line = proxy->conf.args.line;
8542 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008543 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008544 struct sample_expr *expr;
8545 unsigned int where;
8546 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008547 unsigned int tsc_num;
8548 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008549
8550 cur_arg = 1;
8551 proxy->conf.args.ctx = ARGC_TRK;
8552
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008553 tsc_num_str = &args[0][8];
8554 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8555 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8556 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8557 free(err);
8558 goto out_err;
8559 }
8560
Willy Tarreau09448f72014-06-25 18:12:15 +02008561 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8562 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008563 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8564 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008565 free(err);
8566 goto out_err;
8567 }
8568
8569 where = 0;
8570 if (proxy->cap & PR_CAP_FE)
8571 where |= SMP_VAL_FE_HRQ_HDR;
8572 if (proxy->cap & PR_CAP_BE)
8573 where |= SMP_VAL_BE_HRQ_HDR;
8574
8575 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008576 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8577 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8578 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8579 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008580 free(expr);
8581 goto out_err;
8582 }
8583
8584 if (strcmp(args[cur_arg], "table") == 0) {
8585 cur_arg++;
8586 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008587 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8588 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008589 free(expr);
8590 goto out_err;
8591 }
8592 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008593 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008594 cur_arg++;
8595 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008596 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008597 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008598 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008599 } else if (strcmp(args[0], "redirect") == 0) {
8600 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008601 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008602
Willy Tarreaube4653b2015-05-28 15:26:58 +02008603 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008604 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8605 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008606 goto out_err;
8607 }
8608
8609 /* this redirect rule might already contain a parsed condition which
8610 * we'll pass to the http-request rule.
8611 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008612 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008613 rule->arg.redir = redir;
8614 rule->cond = redir->cond;
8615 redir->cond = NULL;
8616 cur_arg = 2;
8617 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008618 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8619 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008620 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008621 /*
8622 * '+ 8' for 'add-acl('
8623 * '- 9' for 'add-acl(' + trailing ')'
8624 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008625 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008626
8627 cur_arg = 1;
8628
8629 if (!*args[cur_arg] ||
8630 (*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 +01008631 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8632 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008633 goto out_err;
8634 }
8635
8636 LIST_INIT(&rule->arg.map.key);
8637 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008638 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008639 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008640 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008641 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8642 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008643 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008644 goto out_err;
8645 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008646 free(proxy->conf.lfs_file);
8647 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8648 proxy->conf.lfs_line = proxy->conf.args.line;
8649 cur_arg += 1;
8650 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8651 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008652 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008653 /*
8654 * '+ 8' for 'del-acl('
8655 * '- 9' for 'del-acl(' + trailing ')'
8656 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008657 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008658
8659 cur_arg = 1;
8660
8661 if (!*args[cur_arg] ||
8662 (*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 +01008663 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8664 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008665 goto out_err;
8666 }
8667
8668 LIST_INIT(&rule->arg.map.key);
8669 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008670 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008671 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008672 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008673 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8674 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008675 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008676 goto out_err;
8677 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008678 free(proxy->conf.lfs_file);
8679 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8680 proxy->conf.lfs_line = proxy->conf.args.line;
8681 cur_arg += 1;
8682 } else if (strncmp(args[0], "del-map", 7) == 0) {
8683 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008684 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008685 /*
8686 * '+ 8' for 'del-map('
8687 * '- 9' for 'del-map(' + trailing ')'
8688 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008689 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008690
8691 cur_arg = 1;
8692
8693 if (!*args[cur_arg] ||
8694 (*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 +01008695 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8696 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008697 goto out_err;
8698 }
8699
8700 LIST_INIT(&rule->arg.map.key);
8701 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008702 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008703 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008704 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008705 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8706 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008707 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008708 goto out_err;
8709 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008710 free(proxy->conf.lfs_file);
8711 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8712 proxy->conf.lfs_line = proxy->conf.args.line;
8713 cur_arg += 1;
8714 } else if (strncmp(args[0], "set-map", 7) == 0) {
8715 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008716 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008717 /*
8718 * '+ 8' for 'set-map('
8719 * '- 9' for 'set-map(' + trailing ')'
8720 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008721 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008722
8723 cur_arg = 1;
8724
8725 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8726 (*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 +01008727 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8728 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008729 goto out_err;
8730 }
8731
8732 LIST_INIT(&rule->arg.map.key);
8733 LIST_INIT(&rule->arg.map.value);
8734 proxy->conf.args.ctx = ARGC_HRQ;
8735
8736 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008737 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008738 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008739 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008740 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8741 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008742 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008743 goto out_err;
8744 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008745
8746 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008747 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008748 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 +01008749 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008750 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8751 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008752 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008753 goto out_err;
8754 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008755 free(proxy->conf.lfs_file);
8756 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8757 proxy->conf.lfs_line = proxy->conf.args.line;
8758
8759 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008760 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8761 char *errmsg = NULL;
8762 cur_arg = 1;
8763 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008764 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008765 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008766 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008767 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8768 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008769 free(errmsg);
8770 goto out_err;
8771 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008772 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008773 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008774 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8775 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8776 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8777 "%s%s, but got '%s'%s.\n",
8778 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008779 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008780 }
8781
8782 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8783 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008784 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008785
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008786 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008787 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8788 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008789 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008790 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008791 }
8792 rule->cond = cond;
8793 }
8794 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008795 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8796 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8797 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008798 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008799 }
8800
8801 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008802 out_err:
8803 free(rule);
8804 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008805}
8806
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008807/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008808struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008809{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008810 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008811 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008812 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008813 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008814
8815 rule = calloc(1, sizeof(*rule));
8816 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008817 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008818 goto out_err;
8819 }
8820
8821 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008822 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008823 cur_arg = 1;
8824 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008825 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008826 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008827 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008828 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008829 cur_arg = 1;
8830
8831 if (!*args[cur_arg] ||
8832 (*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 +01008833 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8834 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008835 goto out_err;
8836 }
8837 rule->arg.nice = atoi(args[cur_arg]);
8838 if (rule->arg.nice < -1024)
8839 rule->arg.nice = -1024;
8840 else if (rule->arg.nice > 1024)
8841 rule->arg.nice = 1024;
8842 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008843 } else if (!strcmp(args[0], "set-tos")) {
8844#ifdef IP_TOS
8845 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008846 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008847 cur_arg = 1;
8848
8849 if (!*args[cur_arg] ||
8850 (*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 +01008851 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8852 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008853 goto out_err;
8854 }
8855
8856 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8857 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008858 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8859 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008860 goto out_err;
8861 }
8862 cur_arg++;
8863#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008864 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 +02008865 goto out_err;
8866#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008867 } else if (!strcmp(args[0], "set-mark")) {
8868#ifdef SO_MARK
8869 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008870 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008871 cur_arg = 1;
8872
8873 if (!*args[cur_arg] ||
8874 (*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 +01008875 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8876 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008877 goto out_err;
8878 }
8879
8880 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8881 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008882 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8883 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008884 goto out_err;
8885 }
8886 cur_arg++;
8887 global.last_checks |= LSTCHK_NETADM;
8888#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008889 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 +02008890 goto out_err;
8891#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008892 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008893 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008894 cur_arg = 1;
8895
8896 if (!*args[cur_arg] ||
8897 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8898 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008899 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8900 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008901 goto out_err;
8902 }
8903 if (strcmp(args[cur_arg], "silent") == 0)
8904 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008905 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008906 goto bad_log_level;
8907 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008908 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008909 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008910 cur_arg = 1;
8911
8912 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8913 (*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 +01008914 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8915 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008916 goto out_err;
8917 }
8918
8919 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8920 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8921 LIST_INIT(&rule->arg.hdr_add.fmt);
8922
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008923 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008924 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008925 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 +01008926 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008927 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8928 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008929 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008930 goto out_err;
8931 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008932 free(proxy->conf.lfs_file);
8933 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8934 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008935 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008936 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008937 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008938 cur_arg = 1;
8939
8940 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008941 (*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 +01008942 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8943 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008944 goto out_err;
8945 }
8946
8947 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8948 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8949 LIST_INIT(&rule->arg.hdr_add.fmt);
8950
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008951 error = NULL;
8952 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008953 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8954 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008955 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008956 goto out_err;
8957 }
8958
8959 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008960 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008961 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008962 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008963 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8964 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008965 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008966 goto out_err;
8967 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008968
8969 free(proxy->conf.lfs_file);
8970 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8971 proxy->conf.lfs_line = proxy->conf.args.line;
8972 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008973 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008974 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008975 cur_arg = 1;
8976
8977 if (!*args[cur_arg] ||
8978 (*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 +01008979 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8980 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008981 goto out_err;
8982 }
8983
8984 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8985 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8986
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008987 proxy->conf.args.ctx = ARGC_HRS;
8988 free(proxy->conf.lfs_file);
8989 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8990 proxy->conf.lfs_line = proxy->conf.args.line;
8991 cur_arg += 1;
8992 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8993 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008994 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008995 /*
8996 * '+ 8' for 'add-acl('
8997 * '- 9' for 'add-acl(' + trailing ')'
8998 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008999 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009000
9001 cur_arg = 1;
9002
9003 if (!*args[cur_arg] ||
9004 (*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 +01009005 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9006 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009007 goto out_err;
9008 }
9009
9010 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009011 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009012 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009013 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009014 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009015 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9016 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009017 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009018 goto out_err;
9019 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009020 free(proxy->conf.lfs_file);
9021 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9022 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009023
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009024 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009025 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9026 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009027 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009028 /*
9029 * '+ 8' for 'del-acl('
9030 * '- 9' for 'del-acl(' + trailing ')'
9031 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009032 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009033
9034 cur_arg = 1;
9035
9036 if (!*args[cur_arg] ||
9037 (*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 +01009038 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9039 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009040 goto out_err;
9041 }
9042
9043 LIST_INIT(&rule->arg.map.key);
9044 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009045 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009046 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009047 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009048 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9049 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009050 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009051 goto out_err;
9052 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009053 free(proxy->conf.lfs_file);
9054 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9055 proxy->conf.lfs_line = proxy->conf.args.line;
9056 cur_arg += 1;
9057 } else if (strncmp(args[0], "del-map", 7) == 0) {
9058 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009059 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009060 /*
9061 * '+ 8' for 'del-map('
9062 * '- 9' for 'del-map(' + trailing ')'
9063 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009064 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009065
9066 cur_arg = 1;
9067
9068 if (!*args[cur_arg] ||
9069 (*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 +01009070 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9071 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009072 goto out_err;
9073 }
9074
9075 LIST_INIT(&rule->arg.map.key);
9076 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009077 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009078 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009079 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009080 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9081 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009082 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009083 goto out_err;
9084 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009085 free(proxy->conf.lfs_file);
9086 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9087 proxy->conf.lfs_line = proxy->conf.args.line;
9088 cur_arg += 1;
9089 } else if (strncmp(args[0], "set-map", 7) == 0) {
9090 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009091 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009092 /*
9093 * '+ 8' for 'set-map('
9094 * '- 9' for 'set-map(' + trailing ')'
9095 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009096 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009097
9098 cur_arg = 1;
9099
9100 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9101 (*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 +01009102 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9103 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009104 goto out_err;
9105 }
9106
9107 LIST_INIT(&rule->arg.map.key);
9108 LIST_INIT(&rule->arg.map.value);
9109
9110 proxy->conf.args.ctx = ARGC_HRS;
9111
9112 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009113 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009114 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009115 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009116 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9117 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009118 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009119 goto out_err;
9120 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009121
9122 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009123 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009124 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 +01009125 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009126 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9127 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009128 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009129 goto out_err;
9130 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009131
9132 free(proxy->conf.lfs_file);
9133 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9134 proxy->conf.lfs_line = proxy->conf.args.line;
9135
9136 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009137 } else if (strcmp(args[0], "redirect") == 0) {
9138 struct redirect_rule *redir;
9139 char *errmsg = NULL;
9140
9141 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009142 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9143 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02009144 goto out_err;
9145 }
9146
9147 /* this redirect rule might already contain a parsed condition which
9148 * we'll pass to the http-request rule.
9149 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009150 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009151 rule->arg.redir = redir;
9152 rule->cond = redir->cond;
9153 redir->cond = NULL;
9154 cur_arg = 2;
9155 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009156 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009157 struct sample_expr *expr;
9158 unsigned int where;
9159 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009160 unsigned int tsc_num;
9161 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009162
9163 cur_arg = 1;
9164 proxy->conf.args.ctx = ARGC_TRK;
9165
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009166 tsc_num_str = &args[0][8];
9167 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
9168 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9169 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9170 free(err);
9171 goto out_err;
9172 }
9173
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009174 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9175 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009176 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9177 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009178 free(err);
9179 goto out_err;
9180 }
9181
9182 where = 0;
9183 if (proxy->cap & PR_CAP_FE)
9184 where |= SMP_VAL_FE_HRS_HDR;
9185 if (proxy->cap & PR_CAP_BE)
9186 where |= SMP_VAL_BE_HRS_HDR;
9187
9188 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009189 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9190 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9191 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9192 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009193 free(expr);
9194 goto out_err;
9195 }
9196
9197 if (strcmp(args[cur_arg], "table") == 0) {
9198 cur_arg++;
9199 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009200 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9201 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009202 free(expr);
9203 goto out_err;
9204 }
9205 /* we copy the table name for now, it will be resolved later */
9206 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9207 cur_arg++;
9208 }
9209 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01009210 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02009211 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009212 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9213 char *errmsg = NULL;
9214 cur_arg = 1;
9215 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009216 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009217 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009218 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009219 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9220 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02009221 free(errmsg);
9222 goto out_err;
9223 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009224 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009225 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01009226 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9227 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
9228 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
9229 "%s%s, but got '%s'%s.\n",
9230 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009231 goto out_err;
9232 }
9233
9234 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9235 struct acl_cond *cond;
9236 char *errmsg = NULL;
9237
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009238 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009239 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9240 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009241 free(errmsg);
9242 goto out_err;
9243 }
9244 rule->cond = cond;
9245 }
9246 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009247 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
9248 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9249 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009250 goto out_err;
9251 }
9252
9253 return rule;
9254 out_err:
9255 free(rule);
9256 return NULL;
9257}
9258
Willy Tarreau4baae242012-12-27 12:00:31 +01009259/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009260 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009261 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9262 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009263 */
9264struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009265 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009266{
9267 struct redirect_rule *rule;
9268 int cur_arg;
9269 int type = REDIRECT_TYPE_NONE;
9270 int code = 302;
9271 const char *destination = NULL;
9272 const char *cookie = NULL;
9273 int cookie_set = 0;
9274 unsigned int flags = REDIRECT_FLAG_NONE;
9275 struct acl_cond *cond = NULL;
9276
9277 cur_arg = 0;
9278 while (*(args[cur_arg])) {
9279 if (strcmp(args[cur_arg], "location") == 0) {
9280 if (!*args[cur_arg + 1])
9281 goto missing_arg;
9282
9283 type = REDIRECT_TYPE_LOCATION;
9284 cur_arg++;
9285 destination = args[cur_arg];
9286 }
9287 else if (strcmp(args[cur_arg], "prefix") == 0) {
9288 if (!*args[cur_arg + 1])
9289 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009290 type = REDIRECT_TYPE_PREFIX;
9291 cur_arg++;
9292 destination = args[cur_arg];
9293 }
9294 else if (strcmp(args[cur_arg], "scheme") == 0) {
9295 if (!*args[cur_arg + 1])
9296 goto missing_arg;
9297
9298 type = REDIRECT_TYPE_SCHEME;
9299 cur_arg++;
9300 destination = args[cur_arg];
9301 }
9302 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9303 if (!*args[cur_arg + 1])
9304 goto missing_arg;
9305
9306 cur_arg++;
9307 cookie = args[cur_arg];
9308 cookie_set = 1;
9309 }
9310 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9311 if (!*args[cur_arg + 1])
9312 goto missing_arg;
9313
9314 cur_arg++;
9315 cookie = args[cur_arg];
9316 cookie_set = 0;
9317 }
9318 else if (strcmp(args[cur_arg], "code") == 0) {
9319 if (!*args[cur_arg + 1])
9320 goto missing_arg;
9321
9322 cur_arg++;
9323 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009324 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009325 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009326 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009327 args[cur_arg - 1], args[cur_arg]);
9328 return NULL;
9329 }
9330 }
9331 else if (!strcmp(args[cur_arg],"drop-query")) {
9332 flags |= REDIRECT_FLAG_DROP_QS;
9333 }
9334 else if (!strcmp(args[cur_arg],"append-slash")) {
9335 flags |= REDIRECT_FLAG_APPEND_SLASH;
9336 }
9337 else if (strcmp(args[cur_arg], "if") == 0 ||
9338 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009339 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009340 if (!cond) {
9341 memprintf(errmsg, "error in condition: %s", *errmsg);
9342 return NULL;
9343 }
9344 break;
9345 }
9346 else {
9347 memprintf(errmsg,
9348 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9349 args[cur_arg]);
9350 return NULL;
9351 }
9352 cur_arg++;
9353 }
9354
9355 if (type == REDIRECT_TYPE_NONE) {
9356 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9357 return NULL;
9358 }
9359
Willy Tarreaube4653b2015-05-28 15:26:58 +02009360 if (dir && type != REDIRECT_TYPE_LOCATION) {
9361 memprintf(errmsg, "response only supports redirect type 'location'");
9362 return NULL;
9363 }
9364
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009365 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009366 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009367 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009368
9369 if (!use_fmt) {
9370 /* old-style static redirect rule */
9371 rule->rdr_str = strdup(destination);
9372 rule->rdr_len = strlen(destination);
9373 }
9374 else {
9375 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009376
9377 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9378 * if prefix == "/", we don't want to add anything, otherwise it
9379 * makes it hard for the user to configure a self-redirection.
9380 */
Godbachd9722032014-12-18 15:44:58 +08009381 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009382 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009383 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9384 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 +01009385 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9386 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009387 return NULL;
9388 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009389 free(curproxy->conf.lfs_file);
9390 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9391 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009392 }
9393 }
9394
Willy Tarreau4baae242012-12-27 12:00:31 +01009395 if (cookie) {
9396 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9397 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9398 */
9399 rule->cookie_len = strlen(cookie);
9400 if (cookie_set) {
9401 rule->cookie_str = malloc(rule->cookie_len + 10);
9402 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9403 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9404 rule->cookie_len += 9;
9405 } else {
9406 rule->cookie_str = malloc(rule->cookie_len + 21);
9407 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9408 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9409 rule->cookie_len += 20;
9410 }
9411 }
9412 rule->type = type;
9413 rule->code = code;
9414 rule->flags = flags;
9415 LIST_INIT(&rule->list);
9416 return rule;
9417
9418 missing_arg:
9419 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9420 return NULL;
9421}
9422
Willy Tarreau8797c062007-05-07 00:55:35 +02009423/************************************************************************/
9424/* The code below is dedicated to ACL parsing and matching */
9425/************************************************************************/
9426
9427
Willy Tarreau14174bc2012-04-16 14:34:04 +02009428/* This function ensures that the prerequisites for an L7 fetch are ready,
9429 * which means that a request or response is ready. If some data is missing,
9430 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009431 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9432 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009433 *
9434 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009435 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9436 * decide whether or not an HTTP message is present ;
9437 * 0 if the requested data cannot be fetched or if it is certain that
9438 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009439 * 1 if an HTTP message is ready
9440 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009441int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009442 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009443{
Willy Tarreau192252e2015-04-04 01:47:55 +02009444 struct http_txn *txn;
9445 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009446
Willy Tarreaube508f12016-03-10 11:47:01 +01009447 /* Note: it is possible that <s> is NULL when called before stream
9448 * initialization (eg: tcp-request connection), so this function is the
9449 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009450 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009451 if (!s)
9452 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009453
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009454 if (!s->txn) {
9455 if (unlikely(!http_alloc_txn(s)))
9456 return 0; /* not enough memory */
9457 http_init_txn(s);
9458 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009459 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009460 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009461
9462 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009463 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009464
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009465 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009466 /* If the buffer does not leave enough free space at the end,
9467 * we must first realign it.
9468 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009469 if (s->req.buf->p > s->req.buf->data &&
9470 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9471 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009472
Willy Tarreau14174bc2012-04-16 14:34:04 +02009473 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009474 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009475 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009476
9477 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009478 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009479 http_msg_analyzer(msg, &txn->hdr_idx);
9480
9481 /* Still no valid request ? */
9482 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009483 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009484 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009485 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009486 }
9487 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009488 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009489 return 0;
9490 }
9491
9492 /* OK we just got a valid HTTP request. We have some minor
9493 * preparation to perform so that further checks can rely
9494 * on HTTP tests.
9495 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009496
9497 /* If the request was parsed but was too large, we must absolutely
9498 * return an error so that it is not processed. At the moment this
9499 * cannot happen, but if the parsers are to change in the future,
9500 * we want this check to be maintained.
9501 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009502 if (unlikely(s->req.buf->i + s->req.buf->p >
9503 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009504 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009505 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009506 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009507 return 1;
9508 }
9509
Willy Tarreau9b28e032012-10-12 23:49:43 +02009510 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009511 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009512 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009513
Willy Tarreau506d0502013-07-06 13:29:24 +02009514 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9515 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009516 }
9517
Willy Tarreau506d0502013-07-06 13:29:24 +02009518 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009519 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009520 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009521
9522 /* otherwise everything's ready for the request */
9523 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009524 else {
9525 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009526 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9527 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009528 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009529 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009530 }
9531
9532 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009533 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009534 return 1;
9535}
Willy Tarreau8797c062007-05-07 00:55:35 +02009536
Willy Tarreau8797c062007-05-07 00:55:35 +02009537/* 1. Check on METHOD
9538 * We use the pre-parsed method if it is known, and store its number as an
9539 * integer. If it is unknown, we use the pointer and the length.
9540 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009541static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009542{
9543 int len, meth;
9544
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009545 len = strlen(text);
9546 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009547
9548 pattern->val.i = meth;
9549 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009550 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009551 pattern->len = len;
9552 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009553 else {
9554 pattern->ptr.str = NULL;
9555 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009556 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009557 return 1;
9558}
9559
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009560/* This function fetches the method of current HTTP request and stores
9561 * it in the global pattern struct as a chunk. There are two possibilities :
9562 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9563 * in <len> and <ptr> is NULL ;
9564 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9565 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009566 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009567 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009568static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009569smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009570{
9571 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009572 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009573
Willy Tarreau24e32d82012-04-23 23:55:44 +02009574 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009575
Willy Tarreaube508f12016-03-10 11:47:01 +01009576 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009577 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009578 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009579 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009580 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009581 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9582 /* ensure the indexes are not affected */
9583 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009584 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009585 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9586 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009587 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009588 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009589 return 1;
9590}
9591
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009592/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009593static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009594{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009595 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009596 struct pattern_list *lst;
9597 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009598
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009599 list_for_each_entry(lst, &expr->patterns, list) {
9600 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009601
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009602 /* well-known method */
9603 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009604 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009605 return pattern;
9606 else
9607 continue;
9608 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009609
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009610 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009611 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009612 continue;
9613
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009614 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009615 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9616 (!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 +01009617 return pattern;
9618 }
9619 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009620}
9621
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009622static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009623smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009624{
Willy Tarreaube508f12016-03-10 11:47:01 +01009625 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009626 char *ptr;
9627 int len;
9628
Willy Tarreauc0239e02012-04-16 14:42:55 +02009629 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009630
Willy Tarreaube508f12016-03-10 11:47:01 +01009631 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009632 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009633 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009634
9635 while ((len-- > 0) && (*ptr++ != '/'));
9636 if (len <= 0)
9637 return 0;
9638
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009639 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009640 smp->data.u.str.str = ptr;
9641 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009642
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009643 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009644 return 1;
9645}
9646
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009647static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009648smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009649{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009650 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009651 char *ptr;
9652 int len;
9653
Willy Tarreauc0239e02012-04-16 14:42:55 +02009654 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009655
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009656 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009657 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9658 return 0;
9659
Willy Tarreau8797c062007-05-07 00:55:35 +02009660 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009661 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009662
9663 while ((len-- > 0) && (*ptr++ != '/'));
9664 if (len <= 0)
9665 return 0;
9666
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009667 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009668 smp->data.u.str.str = ptr;
9669 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009670
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009671 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009672 return 1;
9673}
9674
9675/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009676static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009677smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009678{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009679 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009680 char *ptr;
9681 int len;
9682
Willy Tarreauc0239e02012-04-16 14:42:55 +02009683 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009684
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009685 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009686 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9687 return 0;
9688
Willy Tarreau8797c062007-05-07 00:55:35 +02009689 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009690 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009691
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009692 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009693 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009694 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009695 return 1;
9696}
9697
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009698static int
9699smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9700{
9701 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9702 return 0;
9703
9704 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009705 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009706 return 0;
9707 smp->strm->unique_id[0] = '\0';
9708 }
9709 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9710 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9711
9712 smp->data.type = SMP_T_STR;
9713 smp->data.u.str.str = smp->strm->unique_id;
9714 smp->flags = SMP_F_CONST;
9715 return 1;
9716}
9717
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009718/* Returns a string block containing all headers including the
9719 * empty line wich separes headers from the body. This is useful
9720 * form some headers analysis.
9721 */
9722static int
9723smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9724{
9725 struct http_msg *msg;
9726 struct hdr_idx *idx;
9727 struct http_txn *txn;
9728
9729 CHECK_HTTP_MESSAGE_FIRST();
9730
9731 txn = smp->strm->txn;
9732 idx = &txn->hdr_idx;
9733 msg = &txn->req;
9734
9735 smp->data.type = SMP_T_STR;
9736 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9737 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9738 (msg->chn->buf->p[msg->eoh] == '\r');
9739
9740 return 1;
9741}
9742
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009743/* Returns the header request in a length/value encoded format.
9744 * This is useful for exchanges with the SPOE.
9745 *
9746 * A "length value" is a multibyte code encoding numbers. It uses the
9747 * SPOE format. The encoding is the following:
9748 *
9749 * Each couple "header name" / "header value" is composed
9750 * like this:
9751 * "length value" "header name bytes"
9752 * "length value" "header value bytes"
9753 * When the last header is reached, the header name and the header
9754 * value are empty. Their length are 0
9755 */
9756static int
9757smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9758{
9759 struct http_msg *msg;
9760 struct chunk *temp;
9761 struct hdr_idx *idx;
9762 const char *cur_ptr, *cur_next, *p;
9763 int old_idx, cur_idx;
9764 struct hdr_idx_elem *cur_hdr;
9765 const char *hn, *hv;
9766 int hnl, hvl;
9767 int ret;
9768 struct http_txn *txn;
9769 char *buf;
9770 char *end;
9771
9772 CHECK_HTTP_MESSAGE_FIRST();
9773
9774 temp = get_trash_chunk();
9775 buf = temp->str;
9776 end = temp->str + temp->size;
9777
9778 txn = smp->strm->txn;
9779 idx = &txn->hdr_idx;
9780 msg = &txn->req;
9781
9782 /* Build array of headers. */
9783 old_idx = 0;
9784 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9785 while (1) {
9786 cur_idx = idx->v[old_idx].next;
9787 if (!cur_idx)
9788 break;
9789 old_idx = cur_idx;
9790
9791 cur_hdr = &idx->v[cur_idx];
9792 cur_ptr = cur_next;
9793 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9794
9795 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9796 * and the next header starts at cur_next. We'll check
9797 * this header in the list as well as against the default
9798 * rule.
9799 */
9800
9801 /* look for ': *'. */
9802 hn = cur_ptr;
9803 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9804 if (p >= cur_ptr+cur_hdr->len)
9805 continue;
9806 hnl = p - hn;
9807 p++;
9808 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9809 p++;
9810 if (p >= cur_ptr + cur_hdr->len)
9811 continue;
9812 hv = p;
9813 hvl = cur_ptr + cur_hdr->len-p;
9814
9815 /* encode the header name. */
9816 ret = encode_varint(hnl, &buf, end);
9817 if (ret == -1)
9818 return 0;
9819 if (buf + hnl > end)
9820 return 0;
9821 memcpy(buf, hn, hnl);
9822 buf += hnl;
9823
9824 /* encode and copy the value. */
9825 ret = encode_varint(hvl, &buf, end);
9826 if (ret == -1)
9827 return 0;
9828 if (buf + hvl > end)
9829 return 0;
9830 memcpy(buf, hv, hvl);
9831 buf += hvl;
9832 }
9833
9834 /* encode the end of the header list with empty
9835 * header name and header value.
9836 */
9837 ret = encode_varint(0, &buf, end);
9838 if (ret == -1)
9839 return 0;
9840 ret = encode_varint(0, &buf, end);
9841 if (ret == -1)
9842 return 0;
9843
9844 /* Initialise sample data which will be filled. */
9845 smp->data.type = SMP_T_BIN;
9846 smp->data.u.str.str = temp->str;
9847 smp->data.u.str.len = buf - temp->str;
9848 smp->data.u.str.size = temp->size;
9849
9850 return 1;
9851}
9852
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009853/* returns the longest available part of the body. This requires that the body
9854 * has been waited for using http-buffer-request.
9855 */
9856static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009857smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009858{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009859 struct http_msg *msg;
9860 unsigned long len;
9861 unsigned long block1;
9862 char *body;
9863 struct chunk *temp;
9864
9865 CHECK_HTTP_MESSAGE_FIRST();
9866
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009867 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009868 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009869 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009870 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009871
9872 len = http_body_bytes(msg);
9873 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9874
9875 block1 = len;
9876 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9877 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9878
9879 if (block1 == len) {
9880 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009881 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009882 smp->data.u.str.str = body;
9883 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009884 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9885 }
9886 else {
9887 /* buffer is wrapped, we need to defragment it */
9888 temp = get_trash_chunk();
9889 memcpy(temp->str, body, block1);
9890 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009891 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009892 smp->data.u.str.str = temp->str;
9893 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009894 smp->flags = SMP_F_VOL_TEST;
9895 }
9896 return 1;
9897}
9898
9899
9900/* returns the available length of the body. This requires that the body
9901 * has been waited for using http-buffer-request.
9902 */
9903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009904smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009905{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009906 struct http_msg *msg;
9907
9908 CHECK_HTTP_MESSAGE_FIRST();
9909
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009910 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009911 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009912 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009913 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009914
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009915 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009916 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009917
9918 smp->flags = SMP_F_VOL_TEST;
9919 return 1;
9920}
9921
9922
9923/* returns the advertised length of the body, or the advertised size of the
9924 * chunks available in the buffer. This requires that the body has been waited
9925 * for using http-buffer-request.
9926 */
9927static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009928smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009929{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009930 struct http_msg *msg;
9931
9932 CHECK_HTTP_MESSAGE_FIRST();
9933
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009934 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009935 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009936 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009937 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009938
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009939 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009940 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009941
9942 smp->flags = SMP_F_VOL_TEST;
9943 return 1;
9944}
9945
9946
Willy Tarreau8797c062007-05-07 00:55:35 +02009947/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009948static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009949smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009950{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009951 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009952
Willy Tarreauc0239e02012-04-16 14:42:55 +02009953 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009954
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009955 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009956 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009957 smp->data.u.str.len = txn->req.sl.rq.u_l;
9958 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009959 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009960 return 1;
9961}
9962
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009963static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009964smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009965{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009966 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009967 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009968
Willy Tarreauc0239e02012-04-16 14:42:55 +02009969 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009970
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009971 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009972 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 +02009973 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009974 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009975
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009976 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009977 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009978 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009979 return 1;
9980}
9981
9982static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009983smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009984{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009985 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009986 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009987
Willy Tarreauc0239e02012-04-16 14:42:55 +02009988 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009989
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009990 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009991 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 +02009992 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9993 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009994
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009995 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009996 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009997 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009998 return 1;
9999}
10000
Willy Tarreau185b5c42012-04-26 15:11:51 +020010001/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10002 * Accepts an optional argument of type string containing the header field name,
10003 * and an optional argument of type signed or unsigned integer to request an
10004 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010005 * headers are considered from the first one. It does not stop on commas and
10006 * returns full lines instead (useful for User-Agent or Date for example).
10007 */
10008static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010009smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010010{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010011 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010012 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010013 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010014 int occ = 0;
10015 const char *name_str = NULL;
10016 int name_len = 0;
10017
10018 if (!ctx) {
10019 /* first call */
10020 ctx = &static_hdr_ctx;
10021 ctx->idx = 0;
10022 smp->ctx.a[0] = ctx;
10023 }
10024
10025 if (args) {
10026 if (args[0].type != ARGT_STR)
10027 return 0;
10028 name_str = args[0].data.str.str;
10029 name_len = args[0].data.str.len;
10030
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010031 if (args[1].type == ARGT_SINT)
10032 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010033 }
10034
10035 CHECK_HTTP_MESSAGE_FIRST();
10036
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010037 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010038 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 +020010039
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010040 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10041 /* search for header from the beginning */
10042 ctx->idx = 0;
10043
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010044 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010045 /* no explicit occurrence and single fetch => last header by default */
10046 occ = -1;
10047
10048 if (!occ)
10049 /* prepare to report multiple occurrences for ACL fetches */
10050 smp->flags |= SMP_F_NOT_LAST;
10051
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010052 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010053 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010054 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 +020010055 return 1;
10056
10057 smp->flags &= ~SMP_F_NOT_LAST;
10058 return 0;
10059}
10060
10061/* 6. Check on HTTP header count. The number of occurrences is returned.
10062 * Accepts exactly 1 argument of type string. It does not stop on commas and
10063 * returns full lines instead (useful for User-Agent or Date for example).
10064 */
10065static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010066smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010067{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010068 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010069 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010070 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010071 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010072 const char *name = NULL;
10073 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010074
Willy Tarreau601a4d12015-04-01 19:16:09 +020010075 if (args && args->type == ARGT_STR) {
10076 name = args->data.str.str;
10077 len = args->data.str.len;
10078 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010079
10080 CHECK_HTTP_MESSAGE_FIRST();
10081
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010082 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010083 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 +020010084
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010085 ctx.idx = 0;
10086 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010087 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010088 cnt++;
10089
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010090 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010091 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010092 smp->flags = SMP_F_VOL_HDR;
10093 return 1;
10094}
10095
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010097smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010098{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010099 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010100 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010101 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010102 struct chunk *temp;
10103 char del = ',';
10104
10105 if (args && args->type == ARGT_STR)
10106 del = *args[0].data.str.str;
10107
10108 CHECK_HTTP_MESSAGE_FIRST();
10109
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010110 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010111 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 +020010112
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010113 temp = get_trash_chunk();
10114
10115 ctx.idx = 0;
10116 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10117 if (temp->len)
10118 temp->str[temp->len++] = del;
10119 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10120 temp->len += ctx.del;
10121 }
10122
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010123 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010124 smp->data.u.str.str = temp->str;
10125 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010126 smp->flags = SMP_F_VOL_HDR;
10127 return 1;
10128}
10129
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010130/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10131 * Accepts an optional argument of type string containing the header field name,
10132 * and an optional argument of type signed or unsigned integer to request an
10133 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010134 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010135 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010136static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010137smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010138{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010139 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010140 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010141 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010142 int occ = 0;
10143 const char *name_str = NULL;
10144 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010145
Willy Tarreaua890d072013-04-02 12:01:06 +020010146 if (!ctx) {
10147 /* first call */
10148 ctx = &static_hdr_ctx;
10149 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010150 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010151 }
10152
Willy Tarreau185b5c42012-04-26 15:11:51 +020010153 if (args) {
10154 if (args[0].type != ARGT_STR)
10155 return 0;
10156 name_str = args[0].data.str.str;
10157 name_len = args[0].data.str.len;
10158
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010159 if (args[1].type == ARGT_SINT)
10160 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010161 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010162
Willy Tarreaue333ec92012-04-16 16:26:40 +020010163 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010164
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010165 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010166 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 +020010167
Willy Tarreau185b5c42012-04-26 15:11:51 +020010168 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010169 /* search for header from the beginning */
10170 ctx->idx = 0;
10171
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010172 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010173 /* no explicit occurrence and single fetch => last header by default */
10174 occ = -1;
10175
10176 if (!occ)
10177 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010178 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010179
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010180 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010181 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010182 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 +020010183 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010184
Willy Tarreau37406352012-04-23 16:16:37 +020010185 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010186 return 0;
10187}
10188
Willy Tarreauc11416f2007-06-17 16:58:38 +020010189/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010190 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010191 */
10192static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010193smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010194{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010195 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010196 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010197 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010198 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010199 const char *name = NULL;
10200 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010201
Willy Tarreau601a4d12015-04-01 19:16:09 +020010202 if (args && args->type == ARGT_STR) {
10203 name = args->data.str.str;
10204 len = args->data.str.len;
10205 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010206
Willy Tarreaue333ec92012-04-16 16:26:40 +020010207 CHECK_HTTP_MESSAGE_FIRST();
10208
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010209 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010210 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 +020010211
Willy Tarreau33a7e692007-06-10 19:45:56 +020010212 ctx.idx = 0;
10213 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010214 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010215 cnt++;
10216
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010217 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010218 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010219 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010220 return 1;
10221}
10222
Willy Tarreau185b5c42012-04-26 15:11:51 +020010223/* Fetch an HTTP header's integer value. The integer value is returned. It
10224 * takes a mandatory argument of type string and an optional one of type int
10225 * to designate a specific occurrence. It returns an unsigned integer, which
10226 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010227 */
10228static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010229smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010230{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010231 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010232
Willy Tarreauf853c462012-04-23 18:53:56 +020010233 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010234 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010235 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010236 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010237
Willy Tarreaud53e2422012-04-16 17:21:11 +020010238 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010239}
10240
Cyril Bonté69fa9922012-10-25 00:01:06 +020010241/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10242 * and an optional one of type int to designate a specific occurrence.
10243 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010244 */
10245static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010246smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010247{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010248 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010249
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010250 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010251 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010252 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010253 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010254 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010255 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010256 if (smp->data.u.str.len < temp->size - 1) {
10257 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10258 temp->str[smp->data.u.str.len] = '\0';
10259 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010260 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010261 break;
10262 }
10263 }
10264 }
10265
Willy Tarreaud53e2422012-04-16 17:21:11 +020010266 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010267 if (!(smp->flags & SMP_F_NOT_LAST))
10268 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010269 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010270 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010271}
10272
Willy Tarreau737b0c12007-06-10 21:28:46 +020010273/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10274 * the first '/' after the possible hostname, and ends before the possible '?'.
10275 */
10276static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010277smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010278{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010279 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010280 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010281
Willy Tarreauc0239e02012-04-16 14:42:55 +020010282 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010283
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010284 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010285 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010286 ptr = http_get_path(txn);
10287 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010288 return 0;
10289
10290 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010291 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010292 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010293
10294 while (ptr < end && *ptr != '?')
10295 ptr++;
10296
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010297 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010298 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010299 return 1;
10300}
10301
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010302/* This produces a concatenation of the first occurrence of the Host header
10303 * followed by the path component if it begins with a slash ('/'). This means
10304 * that '*' will not be added, resulting in exactly the first Host entry.
10305 * If no Host header is found, then the path is returned as-is. The returned
10306 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010307 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010308 */
10309static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010310smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010311{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010312 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010313 char *ptr, *end, *beg;
10314 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010315 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010316
10317 CHECK_HTTP_MESSAGE_FIRST();
10318
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010319 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010320 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010321 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010322 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010323
10324 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010325 temp = get_trash_chunk();
10326 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010327 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010328 smp->data.u.str.str = temp->str;
10329 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010330
10331 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010332 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010333 beg = http_get_path(txn);
10334 if (!beg)
10335 beg = end;
10336
10337 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10338
10339 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010340 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10341 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010342 }
10343
10344 smp->flags = SMP_F_VOL_1ST;
10345 return 1;
10346}
10347
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010348/* This produces a 32-bit hash of the concatenation of the first occurrence of
10349 * the Host header followed by the path component if it begins with a slash ('/').
10350 * This means that '*' will not be added, resulting in exactly the first Host
10351 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010352 * is hashed using the path hash followed by a full avalanche hash and provides a
10353 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010354 * high-traffic sites without having to store whole paths.
10355 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010356int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010357smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010358{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010359 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010360 struct hdr_ctx ctx;
10361 unsigned int hash = 0;
10362 char *ptr, *beg, *end;
10363 int len;
10364
10365 CHECK_HTTP_MESSAGE_FIRST();
10366
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010367 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010368 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010369 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010370 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10371 ptr = ctx.line + ctx.val;
10372 len = ctx.vlen;
10373 while (len--)
10374 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10375 }
10376
10377 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010378 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010379 beg = http_get_path(txn);
10380 if (!beg)
10381 beg = end;
10382
10383 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10384
10385 if (beg < ptr && *beg == '/') {
10386 while (beg < ptr)
10387 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10388 }
10389 hash = full_hash(hash);
10390
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010391 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010392 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010393 smp->flags = SMP_F_VOL_1ST;
10394 return 1;
10395}
10396
Willy Tarreau4a550602012-12-09 14:53:32 +010010397/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010398 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10399 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10400 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010401 * that in environments where IPv6 is insignificant, truncating the output to
10402 * 8 bytes would still work.
10403 */
10404static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010405smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010406{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010407 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010408 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010409
10410 if (!cli_conn)
10411 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010412
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010413 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010414 return 0;
10415
Willy Tarreau47ca5452012-12-23 20:22:19 +010010416 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010417 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010418 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010419
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010420 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010421 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010422 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010423 temp->len += 4;
10424 break;
10425 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010426 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010427 temp->len += 16;
10428 break;
10429 default:
10430 return 0;
10431 }
10432
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010433 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010434 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010435 return 1;
10436}
10437
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010438/* Extracts the query string, which comes after the question mark '?'. If no
10439 * question mark is found, nothing is returned. Otherwise it returns a sample
10440 * of type string carrying the whole query string.
10441 */
10442static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010443smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010444{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010445 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010446 char *ptr, *end;
10447
10448 CHECK_HTTP_MESSAGE_FIRST();
10449
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010450 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010451 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10452 end = ptr + txn->req.sl.rq.u_l;
10453
10454 /* look up the '?' */
10455 do {
10456 if (ptr == end)
10457 return 0;
10458 } while (*ptr++ != '?');
10459
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010460 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010461 smp->data.u.str.str = ptr;
10462 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010463 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10464 return 1;
10465}
10466
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010467static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010468smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010469{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010470 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10471 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10472 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010473
Willy Tarreau24e32d82012-04-23 23:55:44 +020010474 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010475
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010476 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010477 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010478 return 1;
10479}
10480
Willy Tarreau7f18e522010-10-22 20:04:13 +020010481/* return a valid test if the current request is the first one on the connection */
10482static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010483smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010484{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010485 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010486 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010487 return 1;
10488}
10489
Willy Tarreau34db1082012-04-19 17:16:54 +020010490/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010491static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010492smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010493{
10494
Willy Tarreau24e32d82012-04-23 23:55:44 +020010495 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010496 return 0;
10497
Willy Tarreauc0239e02012-04-16 14:42:55 +020010498 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010499
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010500 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010501 return 0;
10502
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010503 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010504 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010505 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010506 return 1;
10507}
Willy Tarreau8797c062007-05-07 00:55:35 +020010508
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010509/* Accepts exactly 1 argument of type userlist */
10510static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010511smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010512{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010513 if (!args || args->type != ARGT_USR)
10514 return 0;
10515
10516 CHECK_HTTP_MESSAGE_FIRST();
10517
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010518 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010519 return 0;
10520
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010521 /* if the user does not belong to the userlist or has a wrong password,
10522 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010523 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010524 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010525 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10526 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010527 return 0;
10528
10529 /* pat_match_auth() will need the user list */
10530 smp->ctx.a[0] = args->data.usr;
10531
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010532 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010533 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010534 smp->data.u.str.str = smp->strm->txn->auth.user;
10535 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010536
10537 return 1;
10538}
10539
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010540/* Try to find the next occurrence of a cookie name in a cookie header value.
10541 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10542 * the cookie value is returned into *value and *value_l, and the function
10543 * returns a pointer to the next pointer to search from if the value was found.
10544 * Otherwise if the cookie was not found, NULL is returned and neither value
10545 * nor value_l are touched. The input <hdr> string should first point to the
10546 * header's value, and the <hdr_end> pointer must point to the first character
10547 * not part of the value. <list> must be non-zero if value may represent a list
10548 * of values (cookie headers). This makes it faster to abort parsing when no
10549 * list is expected.
10550 */
David Carlier4686f792015-09-25 14:10:50 +010010551char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010552extract_cookie_value(char *hdr, const char *hdr_end,
10553 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010554 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010555{
10556 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10557 char *next;
10558
10559 /* we search at least a cookie name followed by an equal, and more
10560 * generally something like this :
10561 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10562 */
10563 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10564 /* Iterate through all cookies on this line */
10565
Willy Tarreau2235b262016-11-05 15:50:20 +010010566 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010567 att_beg++;
10568
10569 /* find att_end : this is the first character after the last non
10570 * space before the equal. It may be equal to hdr_end.
10571 */
10572 equal = att_end = att_beg;
10573
10574 while (equal < hdr_end) {
10575 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10576 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010577 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010578 continue;
10579 att_end = equal;
10580 }
10581
10582 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10583 * is between <att_beg> and <equal>, both may be identical.
10584 */
10585
10586 /* look for end of cookie if there is an equal sign */
10587 if (equal < hdr_end && *equal == '=') {
10588 /* look for the beginning of the value */
10589 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010590 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010591 val_beg++;
10592
10593 /* find the end of the value, respecting quotes */
10594 next = find_cookie_value_end(val_beg, hdr_end);
10595
10596 /* make val_end point to the first white space or delimitor after the value */
10597 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010598 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010599 val_end--;
10600 } else {
10601 val_beg = val_end = next = equal;
10602 }
10603
10604 /* We have nothing to do with attributes beginning with '$'. However,
10605 * they will automatically be removed if a header before them is removed,
10606 * since they're supposed to be linked together.
10607 */
10608 if (*att_beg == '$')
10609 continue;
10610
10611 /* Ignore cookies with no equal sign */
10612 if (equal == next)
10613 continue;
10614
10615 /* Now we have the cookie name between att_beg and att_end, and
10616 * its value between val_beg and val_end.
10617 */
10618
10619 if (att_end - att_beg == cookie_name_l &&
10620 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10621 /* let's return this value and indicate where to go on from */
10622 *value = val_beg;
10623 *value_l = val_end - val_beg;
10624 return next + 1;
10625 }
10626
10627 /* Set-Cookie headers only have the name in the first attr=value part */
10628 if (!list)
10629 break;
10630 }
10631
10632 return NULL;
10633}
10634
William Lallemanda43ba4e2014-01-28 18:14:25 +010010635/* Fetch a captured HTTP request header. The index is the position of
10636 * the "capture" option in the configuration file
10637 */
10638static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010639smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010640{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010641 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010642 int idx;
10643
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010644 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010645 return 0;
10646
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010647 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010648
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010649 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 +010010650 return 0;
10651
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010652 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010653 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010654 smp->data.u.str.str = smp->strm->req_cap[idx];
10655 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010656
10657 return 1;
10658}
10659
10660/* Fetch a captured HTTP response header. The index is the position of
10661 * the "capture" option in the configuration file
10662 */
10663static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010664smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010665{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010666 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010667 int idx;
10668
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010669 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010670 return 0;
10671
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010672 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010673
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010674 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 +010010675 return 0;
10676
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010677 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010678 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010679 smp->data.u.str.str = smp->strm->res_cap[idx];
10680 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010681
10682 return 1;
10683}
10684
William Lallemand65ad6e12014-01-31 15:08:02 +010010685/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10686static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010687smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010688{
10689 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010690 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010691 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010692
Willy Tarreau15e91e12015-04-04 00:52:09 +020010693 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010694 return 0;
10695
William Lallemand96a77852014-02-05 00:30:02 +010010696 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010697
William Lallemand96a77852014-02-05 00:30:02 +010010698 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10699 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010700
William Lallemand96a77852014-02-05 00:30:02 +010010701 temp = get_trash_chunk();
10702 temp->str = txn->uri;
10703 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010704 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010705 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010706 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010707
10708 return 1;
10709
10710}
10711
10712/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10713static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010714smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010715{
10716 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010717 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010718 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010719
Willy Tarreau15e91e12015-04-04 00:52:09 +020010720 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010721 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010722
William Lallemand65ad6e12014-01-31 15:08:02 +010010723 ptr = txn->uri;
10724
10725 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10726 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010727
William Lallemand65ad6e12014-01-31 15:08:02 +010010728 if (!*ptr)
10729 return 0;
10730
10731 ptr++; /* skip the space */
10732
10733 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010734 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010735 if (!ptr)
10736 return 0;
10737 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10738 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010740 smp->data.u.str = *temp;
10741 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010742 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010743 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010744
10745 return 1;
10746}
10747
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010748/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10749 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10750 */
10751static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010752smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010753{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010754 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010755
Willy Tarreau15e91e12015-04-04 00:52:09 +020010756 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010757 return 0;
10758
10759 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010760 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010761 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010762 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010763
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010764 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010765 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010766 smp->flags = SMP_F_CONST;
10767 return 1;
10768
10769}
10770
10771/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10772 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10773 */
10774static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010775smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010776{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010777 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010778
Willy Tarreau15e91e12015-04-04 00:52:09 +020010779 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010780 return 0;
10781
10782 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010783 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010784 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010785 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010786
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010787 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010788 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010789 smp->flags = SMP_F_CONST;
10790 return 1;
10791
10792}
10793
William Lallemand65ad6e12014-01-31 15:08:02 +010010794
Willy Tarreaue333ec92012-04-16 16:26:40 +020010795/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010796 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010797 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010798 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010799 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010800 * Accepts exactly 1 argument of type string. If the input options indicate
10801 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010802 * The returned sample is of type CSTR. Can be used to parse cookies in other
10803 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010804 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010805int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010806{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010807 struct http_txn *txn;
10808 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010809 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010810 const struct http_msg *msg;
10811 const char *hdr_name;
10812 int hdr_name_len;
10813 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010814 int occ = 0;
10815 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010816
Willy Tarreau24e32d82012-04-23 23:55:44 +020010817 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010818 return 0;
10819
Willy Tarreaua890d072013-04-02 12:01:06 +020010820 if (!ctx) {
10821 /* first call */
10822 ctx = &static_hdr_ctx;
10823 ctx->idx = 0;
10824 smp->ctx.a[2] = ctx;
10825 }
10826
Willy Tarreaue333ec92012-04-16 16:26:40 +020010827 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010828
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010829 txn = smp->strm->txn;
10830 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010831
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010832 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010833 msg = &txn->req;
10834 hdr_name = "Cookie";
10835 hdr_name_len = 6;
10836 } else {
10837 msg = &txn->rsp;
10838 hdr_name = "Set-Cookie";
10839 hdr_name_len = 10;
10840 }
10841
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010842 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010843 /* no explicit occurrence and single fetch => last cookie by default */
10844 occ = -1;
10845
10846 /* OK so basically here, either we want only one value and it's the
10847 * last one, or we want to iterate over all of them and we fetch the
10848 * next one.
10849 */
10850
Willy Tarreau9b28e032012-10-12 23:49:43 +020010851 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010852 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010853 /* search for the header from the beginning, we must first initialize
10854 * the search parameters.
10855 */
Willy Tarreau37406352012-04-23 16:16:37 +020010856 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010857 ctx->idx = 0;
10858 }
10859
Willy Tarreau28376d62012-04-26 21:26:10 +020010860 smp->flags |= SMP_F_VOL_HDR;
10861
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010862 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010863 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10864 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010865 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10866 goto out;
10867
Willy Tarreau24e32d82012-04-23 23:55:44 +020010868 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010869 continue;
10870
Willy Tarreau37406352012-04-23 16:16:37 +020010871 smp->ctx.a[0] = ctx->line + ctx->val;
10872 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010873 }
10874
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010875 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010876 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010877 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010878 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010879 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010880 &smp->data.u.str.str,
10881 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010882 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010883 found = 1;
10884 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010885 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010886 smp->flags |= SMP_F_NOT_LAST;
10887 return 1;
10888 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010889 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010890 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010891 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010892 /* all cookie headers and values were scanned. If we're looking for the
10893 * last occurrence, we may return it now.
10894 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010895 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010896 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010897 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898}
10899
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010900/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010901 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010902 * multiple cookies may be parsed on the same line. The returned sample is of
10903 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010904 */
10905static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010906smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010907{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010908 struct http_txn *txn;
10909 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010910 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010911 const struct http_msg *msg;
10912 const char *hdr_name;
10913 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914 int cnt;
10915 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010916 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010917
Willy Tarreau24e32d82012-04-23 23:55:44 +020010918 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010919 return 0;
10920
Willy Tarreaue333ec92012-04-16 16:26:40 +020010921 CHECK_HTTP_MESSAGE_FIRST();
10922
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010923 txn = smp->strm->txn;
10924 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010925
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010926 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010927 msg = &txn->req;
10928 hdr_name = "Cookie";
10929 hdr_name_len = 6;
10930 } else {
10931 msg = &txn->rsp;
10932 hdr_name = "Set-Cookie";
10933 hdr_name_len = 10;
10934 }
10935
Willy Tarreau9b28e032012-10-12 23:49:43 +020010936 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010937 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010938 ctx.idx = 0;
10939 cnt = 0;
10940
10941 while (1) {
10942 /* Note: val_beg == NULL every time we need to fetch a new header */
10943 if (!val_beg) {
10944 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10945 break;
10946
Willy Tarreau24e32d82012-04-23 23:55:44 +020010947 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010948 continue;
10949
10950 val_beg = ctx.line + ctx.val;
10951 val_end = val_beg + ctx.vlen;
10952 }
10953
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010954 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010955 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010956 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010957 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010958 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010959 &smp->data.u.str.str,
10960 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010961 cnt++;
10962 }
10963 }
10964
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010965 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010966 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010967 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010968 return 1;
10969}
10970
Willy Tarreau51539362012-05-08 12:46:28 +020010971/* Fetch an cookie's integer value. The integer value is returned. It
10972 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10973 */
10974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010975smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010976{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010977 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010978
10979 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010980 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010981 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010982 }
10983
10984 return ret;
10985}
10986
Willy Tarreau8797c062007-05-07 00:55:35 +020010987/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010988/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010989/************************************************************************/
10990
David Cournapeau16023ee2010-12-23 20:55:41 +090010991/*
10992 * Given a path string and its length, find the position of beginning of the
10993 * query string. Returns NULL if no query string is found in the path.
10994 *
10995 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10996 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010997 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010998 */
bedis4c75cca2012-10-05 08:38:24 +020010999static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011000{
11001 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011002
bedis4c75cca2012-10-05 08:38:24 +020011003 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011004 return p ? p + 1 : NULL;
11005}
11006
bedis4c75cca2012-10-05 08:38:24 +020011007static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011008{
bedis4c75cca2012-10-05 08:38:24 +020011009 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011010}
11011
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011012/* after increasing a pointer value, it can exceed the first buffer
11013 * size. This function transform the value of <ptr> according with
11014 * the expected position. <chunks> is an array of the one or two
11015 * avalaible chunks. The first value is the start of the first chunk,
11016 * the second value if the end+1 of the first chunks. The third value
11017 * is NULL or the start of the second chunk and the fourth value is
11018 * the end+1 of the second chunk. The function returns 1 if does a
11019 * wrap, else returns 0.
11020 */
11021static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11022{
11023 if (*ptr < chunks[1])
11024 return 0;
11025 if (!chunks[2])
11026 return 0;
11027 *ptr = chunks[2] + ( *ptr - chunks[1] );
11028 return 1;
11029}
11030
David Cournapeau16023ee2010-12-23 20:55:41 +090011031/*
11032 * Given a url parameter, find the starting position of the first occurence,
11033 * or NULL if the parameter is not found.
11034 *
11035 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11036 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011037 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011038 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011039 * or the second chunk. The caller must be check the position before using the
11040 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011041 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011042static const char *
11043find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011044 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011045 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011046{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011047 const char *pos, *last, *equal;
11048 const char **bufs = chunks;
11049 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011050
David Cournapeau16023ee2010-12-23 20:55:41 +090011051
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011052 pos = bufs[0];
11053 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011054 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011055 /* Check the equal. */
11056 equal = pos + url_param_name_l;
11057 if (fix_pointer_if_wrap(chunks, &equal)) {
11058 if (equal >= chunks[3])
11059 return NULL;
11060 } else {
11061 if (equal >= chunks[1])
11062 return NULL;
11063 }
11064 if (*equal == '=') {
11065 if (pos + url_param_name_l > last) {
11066 /* process wrap case, we detect a wrap. In this case, the
11067 * comparison is performed in two parts.
11068 */
11069
11070 /* This is the end, we dont have any other chunk. */
11071 if (bufs != chunks || !bufs[2])
11072 return NULL;
11073
11074 /* Compute the length of each part of the comparison. */
11075 l1 = last - pos;
11076 l2 = url_param_name_l - l1;
11077
11078 /* The second buffer is too short to contain the compared string. */
11079 if (bufs[2] + l2 > bufs[3])
11080 return NULL;
11081
11082 if (memcmp(pos, url_param_name, l1) == 0 &&
11083 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11084 return pos;
11085
11086 /* Perform wrapping and jump the string who fail the comparison. */
11087 bufs += 2;
11088 pos = bufs[0] + l2;
11089 last = bufs[1];
11090
11091 } else {
11092 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011093 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11094 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011095 pos += url_param_name_l + 1;
11096 if (fix_pointer_if_wrap(chunks, &pos))
11097 last = bufs[2];
11098 }
11099 }
11100
11101 while (1) {
11102 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011103 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011104 pos++;
11105 if (pos < last)
11106 break;
11107 /* process buffer wrapping. */
11108 if (bufs != chunks || !bufs[2])
11109 return NULL;
11110 bufs += 2;
11111 pos = bufs[0];
11112 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011113 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011114 pos++;
11115 }
11116 return NULL;
11117}
11118
11119/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011120 * Given a url parameter name and a query string, find the next value.
11121 * An empty url_param_name matches the first available parameter.
11122 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11123 * respectively provide a pointer to the value and its end.
11124 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011125 */
11126static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011127find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011128 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011129 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011130{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011131 const char *arg_start, *qs_end;
11132 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011133
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011134 arg_start = chunks[0];
11135 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011136 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011137 /* Looks for an argument name. */
11138 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011139 url_param_name, url_param_name_l,
11140 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011141 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011142 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011143 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011144 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011145 if (!arg_start)
11146 return 0;
11147
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011148 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011149 while (1) {
11150 /* looks for the first argument. */
11151 value_start = memchr(arg_start, '=', qs_end - arg_start);
11152 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011153 /* Check for wrapping. */
11154 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011155 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011156 chunks[2]) {
11157 arg_start = chunks[2];
11158 qs_end = chunks[3];
11159 continue;
11160 }
11161 return 0;
11162 }
11163 break;
11164 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011165 value_start++;
11166 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011167 else {
11168 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011169 value_start = arg_start + url_param_name_l + 1;
11170
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011171 /* Check for pointer wrapping. */
11172 if (fix_pointer_if_wrap(chunks, &value_start)) {
11173 /* Update the end pointer. */
11174 qs_end = chunks[3];
11175
11176 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011177 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011178 return 0;
11179 }
11180 }
11181
David Cournapeau16023ee2010-12-23 20:55:41 +090011182 value_end = value_start;
11183
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011184 while (1) {
11185 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11186 value_end++;
11187 if (value_end < qs_end)
11188 break;
11189 /* process buffer wrapping. */
11190 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011191 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011192 chunks[2]) {
11193 value_end = chunks[2];
11194 qs_end = chunks[3];
11195 continue;
11196 }
11197 break;
11198 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011199
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011200 *vstart = value_start;
11201 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011202 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011203}
11204
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011205/* This scans a URL-encoded query string. It takes an optionally wrapping
11206 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11207 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11208 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011209 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011210static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011211smp_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 +090011212{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011213 const char *vstart, *vend;
11214 struct chunk *temp;
11215 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011216
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011217 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011218 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011219 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011220 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011221 return 0;
11222
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011223 /* Create sample. If the value is contiguous, return the pointer as CONST,
11224 * if the value is wrapped, copy-it in a buffer.
11225 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011226 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011227 if (chunks[2] &&
11228 vstart >= chunks[0] && vstart <= chunks[1] &&
11229 vend >= chunks[2] && vend <= chunks[3]) {
11230 /* Wrapped case. */
11231 temp = get_trash_chunk();
11232 memcpy(temp->str, vstart, chunks[1] - vstart);
11233 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011234 smp->data.u.str.str = temp->str;
11235 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011236 } else {
11237 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011238 smp->data.u.str.str = (char *)vstart;
11239 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011240 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11241 }
11242
11243 /* Update context, check wrapping. */
11244 chunks[0] = vend;
11245 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11246 chunks[1] = chunks[3];
11247 chunks[2] = NULL;
11248 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011249
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011250 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011251 smp->flags |= SMP_F_NOT_LAST;
11252
David Cournapeau16023ee2010-12-23 20:55:41 +090011253 return 1;
11254}
11255
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011256/* This function iterates over each parameter of the query string. It uses
11257 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011258 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11259 * An optional parameter name is passed in args[0], otherwise any parameter is
11260 * considered. It supports an optional delimiter argument for the beginning of
11261 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011262 */
11263static int
11264smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11265{
11266 struct http_msg *msg;
11267 char delim = '?';
11268 const char *name;
11269 int name_len;
11270
Dragan Dosen26f77e52015-05-25 10:02:11 +020011271 if (!args ||
11272 (args[0].type && args[0].type != ARGT_STR) ||
11273 (args[1].type && args[1].type != ARGT_STR))
11274 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011275
Dragan Dosen26f77e52015-05-25 10:02:11 +020011276 name = "";
11277 name_len = 0;
11278 if (args->type == ARGT_STR) {
11279 name = args->data.str.str;
11280 name_len = args->data.str.len;
11281 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011282
Dragan Dosen26f77e52015-05-25 10:02:11 +020011283 if (args[1].type)
11284 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011285
Dragan Dosen26f77e52015-05-25 10:02:11 +020011286 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011287 CHECK_HTTP_MESSAGE_FIRST();
11288
11289 msg = &smp->strm->txn->req;
11290
11291 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11292 msg->sl.rq.u_l, delim);
11293 if (!smp->ctx.a[0])
11294 return 0;
11295
11296 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011297
11298 /* Assume that the context is filled with NULL pointer
11299 * before the first call.
11300 * smp->ctx.a[2] = NULL;
11301 * smp->ctx.a[3] = NULL;
11302 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011303 }
11304
11305 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11306}
11307
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011308/* This function iterates over each parameter of the body. This requires
11309 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011310 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11311 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11312 * optional second part if the body wraps at the end of the buffer. An optional
11313 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011314 */
11315static int
11316smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11317{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011318 struct http_msg *msg;
11319 unsigned long len;
11320 unsigned long block1;
11321 char *body;
11322 const char *name;
11323 int name_len;
11324
11325 if (!args || (args[0].type && args[0].type != ARGT_STR))
11326 return 0;
11327
11328 name = "";
11329 name_len = 0;
11330 if (args[0].type == ARGT_STR) {
11331 name = args[0].data.str.str;
11332 name_len = args[0].data.str.len;
11333 }
11334
11335 if (!smp->ctx.a[0]) { // first call, find the query string
11336 CHECK_HTTP_MESSAGE_FIRST();
11337
11338 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011339 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011340 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011341 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011342
11343 len = http_body_bytes(msg);
11344 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11345
11346 block1 = len;
11347 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11348 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11349
11350 if (block1 == len) {
11351 /* buffer is not wrapped (or empty) */
11352 smp->ctx.a[0] = body;
11353 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011354
11355 /* Assume that the context is filled with NULL pointer
11356 * before the first call.
11357 * smp->ctx.a[2] = NULL;
11358 * smp->ctx.a[3] = NULL;
11359 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011360 }
11361 else {
11362 /* buffer is wrapped, we need to defragment it */
11363 smp->ctx.a[0] = body;
11364 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011365 smp->ctx.a[2] = msg->chn->buf->data;
11366 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011367 }
11368 }
11369 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11370}
11371
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011372/* Return the signed integer value for the specified url parameter (see url_param
11373 * above).
11374 */
11375static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011376smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011377{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011378 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011379
11380 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011381 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011382 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011383 }
11384
11385 return ret;
11386}
11387
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011388/* This produces a 32-bit hash of the concatenation of the first occurrence of
11389 * the Host header followed by the path component if it begins with a slash ('/').
11390 * This means that '*' will not be added, resulting in exactly the first Host
11391 * entry. If no Host header is found, then the path is used. The resulting value
11392 * is hashed using the url hash followed by a full avalanche hash and provides a
11393 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11394 * high-traffic sites without having to store whole paths.
11395 * this differs from the base32 functions in that it includes the url parameters
11396 * as well as the path
11397 */
11398static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011399smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011400{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011401 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011402 struct hdr_ctx ctx;
11403 unsigned int hash = 0;
11404 char *ptr, *beg, *end;
11405 int len;
11406
11407 CHECK_HTTP_MESSAGE_FIRST();
11408
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011409 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011410 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011411 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011412 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11413 ptr = ctx.line + ctx.val;
11414 len = ctx.vlen;
11415 while (len--)
11416 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11417 }
11418
11419 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011420 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 +000011421 beg = http_get_path(txn);
11422 if (!beg)
11423 beg = end;
11424
11425 for (ptr = beg; ptr < end ; ptr++);
11426
11427 if (beg < ptr && *beg == '/') {
11428 while (beg < ptr)
11429 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11430 }
11431 hash = full_hash(hash);
11432
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011433 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011434 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011435 smp->flags = SMP_F_VOL_1ST;
11436 return 1;
11437}
11438
11439/* This concatenates the source address with the 32-bit hash of the Host and
11440 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11441 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11442 * on the source address length. The URL hash is stored before the address so
11443 * that in environments where IPv6 is insignificant, truncating the output to
11444 * 8 bytes would still work.
11445 */
11446static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011447smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011448{
11449 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011450 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011451
Dragan Dosendb5af612016-06-16 11:23:01 +020011452 if (!cli_conn)
11453 return 0;
11454
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011455 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011456 return 0;
11457
11458 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011459 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11460 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011461
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011462 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011463 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011464 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011465 temp->len += 4;
11466 break;
11467 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011468 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011469 temp->len += 16;
11470 break;
11471 default:
11472 return 0;
11473 }
11474
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011475 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011476 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011477 return 1;
11478}
11479
Willy Tarreau185b5c42012-04-26 15:11:51 +020011480/* This function is used to validate the arguments passed to any "hdr" fetch
11481 * keyword. These keywords support an optional positive or negative occurrence
11482 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11483 * is assumed that the types are already the correct ones. Returns 0 on error,
11484 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11485 * error message in case of error, that the caller is responsible for freeing.
11486 * The initial location must either be freeable or NULL.
11487 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011488int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011489{
11490 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011491 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011492 return 0;
11493 }
11494 return 1;
11495}
11496
Willy Tarreau276fae92013-07-25 14:36:01 +020011497/* takes an UINT value on input supposed to represent the time since EPOCH,
11498 * adds an optional offset found in args[0] and emits a string representing
11499 * the date in RFC-1123/5322 format.
11500 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011501static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011502{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011503 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011504 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11505 struct chunk *temp;
11506 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011507 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011508 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011509
11510 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011511 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011512 curr_date += args[0].data.sint;
11513
11514 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011515 if (!tm)
11516 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011517
11518 temp = get_trash_chunk();
11519 temp->len = snprintf(temp->str, temp->size - temp->len,
11520 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11521 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11522 tm->tm_hour, tm->tm_min, tm->tm_sec);
11523
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011524 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011525 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011526 return 1;
11527}
11528
Thierry FOURNIERad903512014-04-11 17:51:01 +020011529/* Match language range with language tag. RFC2616 14.4:
11530 *
11531 * A language-range matches a language-tag if it exactly equals
11532 * the tag, or if it exactly equals a prefix of the tag such
11533 * that the first tag character following the prefix is "-".
11534 *
11535 * Return 1 if the strings match, else return 0.
11536 */
11537static inline int language_range_match(const char *range, int range_len,
11538 const char *tag, int tag_len)
11539{
11540 const char *end = range + range_len;
11541 const char *tend = tag + tag_len;
11542 while (range < end) {
11543 if (*range == '-' && tag == tend)
11544 return 1;
11545 if (*range != *tag || tag == tend)
11546 return 0;
11547 range++;
11548 tag++;
11549 }
11550 /* Return true only if the last char of the tag is matched. */
11551 return tag == tend;
11552}
11553
11554/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011555static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011556{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011557 const char *al = smp->data.u.str.str;
11558 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011559 const char *token;
11560 int toklen;
11561 int qvalue;
11562 const char *str;
11563 const char *w;
11564 int best_q = 0;
11565
11566 /* Set the constant to the sample, because the output of the
11567 * function will be peek in the constant configuration string.
11568 */
11569 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011570 smp->data.u.str.size = 0;
11571 smp->data.u.str.str = "";
11572 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011573
11574 /* Parse the accept language */
11575 while (1) {
11576
11577 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011578 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011579 al++;
11580 if (al >= end)
11581 break;
11582
11583 /* Start of the fisrt word. */
11584 token = al;
11585
11586 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011587 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011588 al++;
11589 if (al == token)
11590 goto expect_comma;
11591
11592 /* Length of the token. */
11593 toklen = al - token;
11594 qvalue = 1000;
11595
11596 /* Check if the token exists in the list. If the token not exists,
11597 * jump to the next token.
11598 */
11599 str = args[0].data.str.str;
11600 w = str;
11601 while (1) {
11602 if (*str == ';' || *str == '\0') {
11603 if (language_range_match(token, toklen, w, str-w))
11604 goto look_for_q;
11605 if (*str == '\0')
11606 goto expect_comma;
11607 w = str + 1;
11608 }
11609 str++;
11610 }
11611 goto expect_comma;
11612
11613look_for_q:
11614
11615 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011616 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011617 al++;
11618 if (al >= end)
11619 goto process_value;
11620
11621 /* If ',' is found, process the result */
11622 if (*al == ',')
11623 goto process_value;
11624
11625 /* If the character is different from ';', look
11626 * for the end of the header part in best effort.
11627 */
11628 if (*al != ';')
11629 goto expect_comma;
11630
11631 /* Assumes that the char is ';', now expect "q=". */
11632 al++;
11633
11634 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011635 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011636 al++;
11637 if (al >= end)
11638 goto process_value;
11639
11640 /* Expect 'q'. If no 'q', continue in best effort */
11641 if (*al != 'q')
11642 goto process_value;
11643 al++;
11644
11645 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011646 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011647 al++;
11648 if (al >= end)
11649 goto process_value;
11650
11651 /* Expect '='. If no '=', continue in best effort */
11652 if (*al != '=')
11653 goto process_value;
11654 al++;
11655
11656 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011657 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011658 al++;
11659 if (al >= end)
11660 goto process_value;
11661
11662 /* Parse the q value. */
11663 qvalue = parse_qvalue(al, &al);
11664
11665process_value:
11666
11667 /* If the new q value is the best q value, then store the associated
11668 * language in the response. If qvalue is the biggest value (1000),
11669 * break the process.
11670 */
11671 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011672 smp->data.u.str.str = (char *)w;
11673 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011674 if (qvalue >= 1000)
11675 break;
11676 best_q = qvalue;
11677 }
11678
11679expect_comma:
11680
11681 /* Expect comma or end. If the end is detected, quit the loop. */
11682 while (al < end && *al != ',')
11683 al++;
11684 if (al >= end)
11685 break;
11686
11687 /* Comma is found, jump it and restart the analyzer. */
11688 al++;
11689 }
11690
11691 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011692 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11693 smp->data.u.str.str = args[1].data.str.str;
11694 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011695 }
11696
11697 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011698 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011699}
11700
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011701/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011702static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011703{
11704 /* If the constant flag is set or if not size is avalaible at
11705 * the end of the buffer, copy the string in other buffer
11706 * before decoding.
11707 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011708 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011709 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011710 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11711 smp->data.u.str.str = str->str;
11712 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011713 smp->flags &= ~SMP_F_CONST;
11714 }
11715
11716 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011717 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11718 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011719 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011720}
11721
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011722static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11723{
11724 struct proxy *fe = strm_fe(smp->strm);
11725 int idx, i;
11726 struct cap_hdr *hdr;
11727 int len;
11728
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011729 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011730 return 0;
11731
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011732 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011733
11734 /* Check the availibity of the capture id. */
11735 if (idx > fe->nb_req_cap - 1)
11736 return 0;
11737
11738 /* Look for the original configuration. */
11739 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11740 hdr != NULL && i != idx ;
11741 i--, hdr = hdr->next);
11742 if (!hdr)
11743 return 0;
11744
11745 /* check for the memory allocation */
11746 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011747 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011748 if (smp->strm->req_cap[hdr->index] == NULL)
11749 return 0;
11750
11751 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011752 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011753 if (len > hdr->len)
11754 len = hdr->len;
11755
11756 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011757 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011758 smp->strm->req_cap[idx][len] = '\0';
11759
11760 return 1;
11761}
11762
11763static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11764{
11765 struct proxy *fe = strm_fe(smp->strm);
11766 int idx, i;
11767 struct cap_hdr *hdr;
11768 int len;
11769
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011770 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011771 return 0;
11772
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011773 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011774
11775 /* Check the availibity of the capture id. */
11776 if (idx > fe->nb_rsp_cap - 1)
11777 return 0;
11778
11779 /* Look for the original configuration. */
11780 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11781 hdr != NULL && i != idx ;
11782 i--, hdr = hdr->next);
11783 if (!hdr)
11784 return 0;
11785
11786 /* check for the memory allocation */
11787 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011788 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011789 if (smp->strm->res_cap[hdr->index] == NULL)
11790 return 0;
11791
11792 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011793 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011794 if (len > hdr->len)
11795 len = hdr->len;
11796
11797 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011798 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011799 smp->strm->res_cap[idx][len] = '\0';
11800
11801 return 1;
11802}
11803
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011804/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011805 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011806 * the relevant part of the request line accordingly. Then it updates various
11807 * pointers to the next elements which were moved, and the total buffer length.
11808 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011809 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11810 * error, though this can be revisited when this code is finally exploited.
11811 *
11812 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11813 * query string and 3 to replace uri.
11814 *
11815 * In query string case, the mark question '?' must be set at the start of the
11816 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011817 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011818int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011819 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011820{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011821 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011822 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011823 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011824 int delta;
11825
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011826 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011827 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011828 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011829 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11830
11831 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011832 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011833 txn->req.sl.rq.m_l += delta;
11834 txn->req.sl.rq.u += delta;
11835 txn->req.sl.rq.v += delta;
11836 break;
11837
11838 case 1: // path
11839 cur_ptr = http_get_path(txn);
11840 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011841 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011842
11843 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011844 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 +010011845 cur_end++;
11846
11847 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011848 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011849 txn->req.sl.rq.u_l += delta;
11850 txn->req.sl.rq.v += delta;
11851 break;
11852
11853 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011854 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011855 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011856 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11857 while (cur_ptr < cur_end && *cur_ptr != '?')
11858 cur_ptr++;
11859
11860 /* skip the question mark or indicate that we must insert it
11861 * (but only if the format string is not empty then).
11862 */
11863 if (cur_ptr < cur_end)
11864 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011865 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011866 offset = 0;
11867
11868 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011869 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011870 txn->req.sl.rq.u_l += delta;
11871 txn->req.sl.rq.v += delta;
11872 break;
11873
11874 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011875 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011876 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11877
11878 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011879 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011880 txn->req.sl.rq.u_l += delta;
11881 txn->req.sl.rq.v += delta;
11882 break;
11883
11884 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011885 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011886 }
11887
11888 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011889 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011890 txn->req.sl.rq.l += delta;
11891 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011892 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011893 return 0;
11894}
11895
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011896/* This function replace the HTTP status code and the associated message. The
11897 * variable <status> contains the new status code. This function never fails.
11898 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011899void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011900{
11901 struct http_txn *txn = s->txn;
11902 char *cur_ptr, *cur_end;
11903 int delta;
11904 char *res;
11905 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011906 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011907 int msg_len;
11908
11909 chunk_reset(&trash);
11910
11911 res = ultoa_o(status, trash.str, trash.size);
11912 c_l = res - trash.str;
11913
11914 trash.str[c_l] = ' ';
11915 trash.len = c_l + 1;
11916
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011917 /* Do we have a custom reason format string? */
11918 if (msg == NULL)
11919 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011920 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011921 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11922 trash.len += msg_len;
11923
11924 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11925 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11926
11927 /* commit changes and adjust message */
11928 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11929
11930 /* adjust res line offsets and lengths */
11931 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11932 txn->rsp.sl.st.c_l = c_l;
11933 txn->rsp.sl.st.r_l = msg_len;
11934
11935 delta = trash.len - (cur_end - cur_ptr);
11936 txn->rsp.sl.st.l += delta;
11937 txn->hdr_idx.v[0].len += delta;
11938 http_msg_move_end(&txn->rsp, delta);
11939}
11940
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011941/* This function executes one of the set-{method,path,query,uri} actions. It
11942 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011943 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011944 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011945 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11946 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011947 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011948enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011949 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011950{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011951 struct chunk *replace;
11952 enum act_return ret = ACT_RET_ERR;
11953
11954 replace = alloc_trash_chunk();
11955 if (!replace)
11956 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011957
11958 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011959 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011960 replace->str[replace->len++] = '?';
11961 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11962 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011963
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011964 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11965
11966 ret = ACT_RET_CONT;
11967
11968leave:
11969 free_trash_chunk(replace);
11970 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011971}
11972
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011973/* This function is just a compliant action wrapper for "set-status". */
11974enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011975 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011976{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011977 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011978 return ACT_RET_CONT;
11979}
11980
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011981/* parse an http-request action among :
11982 * set-method
11983 * set-path
11984 * set-query
11985 * set-uri
11986 *
11987 * All of them accept a single argument of type string representing a log-format.
11988 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11989 * 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 +020011990 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011991 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011992enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11993 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011994{
11995 int cur_arg = *orig_arg;
11996
Thierry FOURNIER42148732015-09-02 17:17:33 +020011997 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011998
11999 switch (args[0][4]) {
12000 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012001 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012002 rule->action_ptr = http_action_set_req_line;
12003 break;
12004 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012005 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012006 rule->action_ptr = http_action_set_req_line;
12007 break;
12008 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012009 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012010 rule->action_ptr = http_action_set_req_line;
12011 break;
12012 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012013 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012014 rule->action_ptr = http_action_set_req_line;
12015 break;
12016 default:
12017 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012018 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012019 }
12020
12021 if (!*args[cur_arg] ||
12022 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12023 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012024 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012025 }
12026
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012027 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012028 px->conf.args.ctx = ARGC_HRQ;
12029 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
12030 (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 +010012031 return ACT_RET_PRS_ERR;
12032 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012033
12034 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012035 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012036}
12037
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012038/* parse set-status action:
12039 * This action accepts a single argument of type int representing
12040 * an http status code. It returns ACT_RET_PRS_OK on success,
12041 * ACT_RET_PRS_ERR on error.
12042 */
12043enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12044 struct act_rule *rule, char **err)
12045{
12046 char *error;
12047
Thierry FOURNIER42148732015-09-02 17:17:33 +020012048 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012049 rule->action_ptr = action_http_set_status;
12050
12051 /* Check if an argument is available */
12052 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012053 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012054 return ACT_RET_PRS_ERR;
12055 }
12056
12057 /* convert status code as integer */
12058 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12059 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12060 memprintf(err, "expects an integer status code between 100 and 999");
12061 return ACT_RET_PRS_ERR;
12062 }
12063
12064 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012065
12066 /* set custom reason string */
12067 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12068 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12069 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12070 (*orig_arg)++;
12071 rule->arg.status.reason = strdup(args[*orig_arg]);
12072 (*orig_arg)++;
12073 }
12074
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012075 return ACT_RET_PRS_OK;
12076}
12077
Willy Tarreau53275e82017-11-24 07:52:01 +010012078/* This function executes the "reject" HTTP action. It clears the request and
12079 * response buffer without sending any response. It can be useful as an HTTP
12080 * alternative to the silent-drop action to defend against DoS attacks, and may
12081 * also be used with HTTP/2 to close a connection instead of just a stream.
12082 * The txn status is unchanged, indicating no response was sent. The termination
12083 * flags will indicate "PR". It always returns ACT_RET_STOP.
12084 */
12085enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
12086 struct session *sess, struct stream *s, int flags)
12087{
12088 channel_abort(&s->req);
12089 channel_abort(&s->res);
12090 s->req.analysers = 0;
12091 s->res.analysers = 0;
12092
12093 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
12094 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
12095 if (sess->listener && sess->listener->counters)
12096 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
12097
12098 if (!(s->flags & SF_ERR_MASK))
12099 s->flags |= SF_ERR_PRXCOND;
12100 if (!(s->flags & SF_FINST_MASK))
12101 s->flags |= SF_FINST_R;
12102
12103 return ACT_RET_CONT;
12104}
12105
12106/* parse the "reject" action:
12107 * This action takes no argument and returns ACT_RET_PRS_OK on success,
12108 * ACT_RET_PRS_ERR on error.
12109 */
12110enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
12111 struct act_rule *rule, char **err)
12112{
12113 rule->action = ACT_CUSTOM;
12114 rule->action_ptr = http_action_reject;
12115 return ACT_RET_PRS_OK;
12116}
12117
Willy Tarreaua9083d02015-05-08 15:27:59 +020012118/* This function executes the "capture" action. It executes a fetch expression,
12119 * turns the result into a string and puts it in a capture slot. It always
12120 * returns 1. If an error occurs the action is cancelled, but the rule
12121 * processing continues.
12122 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012123enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012124 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012125{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012126 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012127 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012128 char **cap = s->req_cap;
12129 int len;
12130
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012131 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 +020012132 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012133 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012134
12135 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012136 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012137
12138 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012139 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012140
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012141 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012142 if (len > h->len)
12143 len = h->len;
12144
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012145 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012146 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012147 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012148}
12149
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012150/* This function executes the "capture" action and store the result in a
12151 * capture slot if exists. It executes a fetch expression, turns the result
12152 * into a string and puts it in a capture slot. It always returns 1. If an
12153 * error occurs the action is cancelled, but the rule processing continues.
12154 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012155enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012156 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012157{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012158 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012159 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012160 char **cap = s->req_cap;
12161 struct proxy *fe = strm_fe(s);
12162 int len;
12163 int i;
12164
12165 /* Look for the original configuration. */
12166 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012167 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012168 i--, h = h->next);
12169 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012170 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012171
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012172 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 +020012173 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012174 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012175
12176 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012177 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012178
12179 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012180 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012181
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012182 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012183 if (len > h->len)
12184 len = h->len;
12185
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012186 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012187 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012188 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012189}
12190
Christopher Faulet29730ba2017-09-18 15:26:32 +020012191/* Check an "http-request capture" action.
12192 *
12193 * The function returns 1 in success case, otherwise, it returns 0 and err is
12194 * filled.
12195 */
12196int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
12197{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012198 if (rule->action_ptr != http_action_req_capture_by_id)
12199 return 1;
12200
Christopher Faulet29730ba2017-09-18 15:26:32 +020012201 if (rule->arg.capid.idx >= px->nb_req_cap) {
12202 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
12203 rule->arg.capid.idx);
12204 return 0;
12205 }
12206
12207 return 1;
12208}
12209
Willy Tarreaua9083d02015-05-08 15:27:59 +020012210/* parse an "http-request capture" action. It takes a single argument which is
12211 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012212 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012213 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012214 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012215enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12216 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012217{
12218 struct sample_expr *expr;
12219 struct cap_hdr *hdr;
12220 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012221 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012222
12223 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12224 if (strcmp(args[cur_arg], "if") == 0 ||
12225 strcmp(args[cur_arg], "unless") == 0)
12226 break;
12227
12228 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012229 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012230 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012231 }
12232
Willy Tarreaua9083d02015-05-08 15:27:59 +020012233 cur_arg = *orig_arg;
12234 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12235 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012236 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012237
12238 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12239 memprintf(err,
12240 "fetch method '%s' extracts information from '%s', none of which is available here",
12241 args[cur_arg-1], sample_src_names(expr->fetch->use));
12242 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012243 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012244 }
12245
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012246 if (!args[cur_arg] || !*args[cur_arg]) {
12247 memprintf(err, "expects 'len or 'id'");
12248 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012249 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012250 }
12251
Willy Tarreaua9083d02015-05-08 15:27:59 +020012252 if (strcmp(args[cur_arg], "len") == 0) {
12253 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012254
12255 if (!(px->cap & PR_CAP_FE)) {
12256 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012257 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012258 }
12259
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012260 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012261
Willy Tarreaua9083d02015-05-08 15:27:59 +020012262 if (!args[cur_arg]) {
12263 memprintf(err, "missing length value");
12264 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012265 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012266 }
12267 /* we copy the table name for now, it will be resolved later */
12268 len = atoi(args[cur_arg]);
12269 if (len <= 0) {
12270 memprintf(err, "length must be > 0");
12271 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012272 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012273 }
12274 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012275
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012276 if (!len) {
12277 memprintf(err, "a positive 'len' argument is mandatory");
12278 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012279 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012280 }
12281
Vincent Bernat02779b62016-04-03 13:48:43 +020012282 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012283 hdr->next = px->req_cap;
12284 hdr->name = NULL; /* not a header capture */
12285 hdr->namelen = 0;
12286 hdr->len = len;
12287 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12288 hdr->index = px->nb_req_cap++;
12289
12290 px->req_cap = hdr;
12291 px->to_log |= LW_REQHDR;
12292
Thierry FOURNIER42148732015-09-02 17:17:33 +020012293 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012294 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012295 rule->arg.cap.expr = expr;
12296 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012297 }
12298
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012299 else if (strcmp(args[cur_arg], "id") == 0) {
12300 int id;
12301 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012302
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012303 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012304
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012305 if (!args[cur_arg]) {
12306 memprintf(err, "missing id value");
12307 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012308 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012309 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012310
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012311 id = strtol(args[cur_arg], &error, 10);
12312 if (*error != '\0') {
12313 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12314 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012315 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012316 }
12317 cur_arg++;
12318
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012319 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012320
Thierry FOURNIER42148732015-09-02 17:17:33 +020012321 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012322 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012323 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012324 rule->arg.capid.expr = expr;
12325 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012326 }
12327
12328 else {
12329 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12330 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012331 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012332 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012333
12334 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012335 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012336}
12337
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012338/* This function executes the "capture" action and store the result in a
12339 * capture slot if exists. It executes a fetch expression, turns the result
12340 * into a string and puts it in a capture slot. It always returns 1. If an
12341 * error occurs the action is cancelled, but the rule processing continues.
12342 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012343enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012344 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012345{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012346 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012347 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012348 char **cap = s->res_cap;
12349 struct proxy *fe = strm_fe(s);
12350 int len;
12351 int i;
12352
12353 /* Look for the original configuration. */
12354 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012355 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012356 i--, h = h->next);
12357 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012358 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012359
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012360 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 +020012361 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012362 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012363
12364 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012365 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012366
12367 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012368 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012369
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012370 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012371 if (len > h->len)
12372 len = h->len;
12373
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012374 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012375 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012376 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012377}
12378
Christopher Faulet29730ba2017-09-18 15:26:32 +020012379/* Check an "http-response capture" action.
12380 *
12381 * The function returns 1 in success case, otherwise, it returns 0 and err is
12382 * filled.
12383 */
12384int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12385{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012386 if (rule->action_ptr != http_action_res_capture_by_id)
12387 return 1;
12388
Christopher Faulet29730ba2017-09-18 15:26:32 +020012389 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12390 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12391 rule->arg.capid.idx);
12392 return 0;
12393 }
12394
12395 return 1;
12396}
12397
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012398/* parse an "http-response capture" action. It takes a single argument which is
12399 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12400 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012401 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012402 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012403enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12404 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012405{
12406 struct sample_expr *expr;
12407 int cur_arg;
12408 int id;
12409 char *error;
12410
12411 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12412 if (strcmp(args[cur_arg], "if") == 0 ||
12413 strcmp(args[cur_arg], "unless") == 0)
12414 break;
12415
12416 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012417 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012418 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012419 }
12420
12421 cur_arg = *orig_arg;
12422 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12423 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012424 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012425
12426 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12427 memprintf(err,
12428 "fetch method '%s' extracts information from '%s', none of which is available here",
12429 args[cur_arg-1], sample_src_names(expr->fetch->use));
12430 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012431 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012432 }
12433
12434 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012435 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012436 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012437 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012438 }
12439
12440 if (strcmp(args[cur_arg], "id") != 0) {
12441 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12442 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012443 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012444 }
12445
12446 cur_arg++;
12447
12448 if (!args[cur_arg]) {
12449 memprintf(err, "missing id value");
12450 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012451 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012452 }
12453
12454 id = strtol(args[cur_arg], &error, 10);
12455 if (*error != '\0') {
12456 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12457 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012458 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012459 }
12460 cur_arg++;
12461
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012462 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012463
Thierry FOURNIER42148732015-09-02 17:17:33 +020012464 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012465 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012466 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012467 rule->arg.capid.expr = expr;
12468 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012469
12470 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012471 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012472}
12473
William Lallemand73025dd2014-04-24 14:38:37 +020012474/*
12475 * Return the struct http_req_action_kw associated to a keyword.
12476 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012477struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012478{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012479 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012480}
12481
12482/*
12483 * Return the struct http_res_action_kw associated to a keyword.
12484 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012485struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012486{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012487 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012488}
12489
Willy Tarreau12207b32016-11-22 19:48:51 +010012490
12491/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12492 * now.
12493 */
12494static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12495{
12496 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12497 return 1;
12498
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012499 if (*args[2]) {
12500 struct proxy *px;
12501
12502 px = proxy_find_by_name(args[2], 0, 0);
12503 if (px)
12504 appctx->ctx.errors.iid = px->uuid;
12505 else
12506 appctx->ctx.errors.iid = atoi(args[2]);
12507
12508 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012509 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012510 appctx->ctx.cli.msg = "No such proxy.\n";
12511 appctx->st0 = CLI_ST_PRINT;
12512 return 1;
12513 }
12514 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012515 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012516 appctx->ctx.errors.iid = -1; // dump all proxies
12517
Willy Tarreau35069f82016-11-25 09:16:37 +010012518 appctx->ctx.errors.flag = 0;
12519 if (strcmp(args[3], "request") == 0)
12520 appctx->ctx.errors.flag |= 4; // ignore response
12521 else if (strcmp(args[3], "response") == 0)
12522 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012523 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012524 return 0;
12525}
12526
12527/* This function dumps all captured errors onto the stream interface's
12528 * read buffer. It returns 0 if the output buffer is full and it needs
12529 * to be called again, otherwise non-zero.
12530 */
12531static int cli_io_handler_show_errors(struct appctx *appctx)
12532{
12533 struct stream_interface *si = appctx->owner;
12534 extern const char *monthname[12];
12535
12536 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12537 return 1;
12538
12539 chunk_reset(&trash);
12540
12541 if (!appctx->ctx.errors.px) {
12542 /* the function had not been called yet, let's prepare the
12543 * buffer for a response.
12544 */
12545 struct tm tm;
12546
12547 get_localtime(date.tv_sec, &tm);
12548 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12549 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12550 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12551 error_snapshot_id);
12552
Willy Tarreau06d80a92017-10-19 14:32:15 +020012553 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012554 /* Socket buffer full. Let's try again later from the same point */
12555 si_applet_cant_put(si);
12556 return 0;
12557 }
12558
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012559 appctx->ctx.errors.px = proxies_list;
Willy Tarreau12207b32016-11-22 19:48:51 +010012560 appctx->ctx.errors.bol = 0;
12561 appctx->ctx.errors.ptr = -1;
12562 }
12563
12564 /* we have two inner loops here, one for the proxy, the other one for
12565 * the buffer.
12566 */
12567 while (appctx->ctx.errors.px) {
12568 struct error_snapshot *es;
12569
Willy Tarreau35069f82016-11-25 09:16:37 +010012570 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012571 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012572 if (appctx->ctx.errors.flag & 2) // skip req
12573 goto next;
12574 }
12575 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012576 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012577 if (appctx->ctx.errors.flag & 4) // skip resp
12578 goto next;
12579 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012580
12581 if (!es->when.tv_sec)
12582 goto next;
12583
12584 if (appctx->ctx.errors.iid >= 0 &&
12585 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12586 es->oe->uuid != appctx->ctx.errors.iid)
12587 goto next;
12588
12589 if (appctx->ctx.errors.ptr < 0) {
12590 /* just print headers now */
12591
12592 char pn[INET6_ADDRSTRLEN];
12593 struct tm tm;
12594 int port;
12595
12596 get_localtime(es->when.tv_sec, &tm);
12597 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12598 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12599 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12600
12601 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12602 case AF_INET:
12603 case AF_INET6:
12604 port = get_host_port(&es->src);
12605 break;
12606 default:
12607 port = 0;
12608 }
12609
Willy Tarreau35069f82016-11-25 09:16:37 +010012610 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012611 case 0:
12612 chunk_appendf(&trash,
12613 " frontend %s (#%d): invalid request\n"
12614 " backend %s (#%d)",
12615 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12616 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12617 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12618 break;
12619 case 1:
12620 chunk_appendf(&trash,
12621 " backend %s (#%d): invalid response\n"
12622 " frontend %s (#%d)",
12623 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12624 es->oe->id, es->oe->uuid);
12625 break;
12626 }
12627
12628 chunk_appendf(&trash,
12629 ", server %s (#%d), event #%u\n"
12630 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012631 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012632 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12633 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12634 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12635 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12636 es->ev_id,
12637 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012638 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012639 es->m_clen, es->m_blen,
12640 es->b_flags, es->b_out, es->b_tot,
12641 es->len, es->b_wrap, es->pos);
12642
Willy Tarreau06d80a92017-10-19 14:32:15 +020012643 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012644 /* Socket buffer full. Let's try again later from the same point */
12645 si_applet_cant_put(si);
12646 return 0;
12647 }
12648 appctx->ctx.errors.ptr = 0;
12649 appctx->ctx.errors.sid = es->sid;
12650 }
12651
12652 if (appctx->ctx.errors.sid != es->sid) {
12653 /* the snapshot changed while we were dumping it */
12654 chunk_appendf(&trash,
12655 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012656 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012657 si_applet_cant_put(si);
12658 return 0;
12659 }
12660 goto next;
12661 }
12662
12663 /* OK, ptr >= 0, so we have to dump the current line */
12664 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12665 int newptr;
12666 int newline;
12667
12668 newline = appctx->ctx.errors.bol;
12669 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12670 if (newptr == appctx->ctx.errors.ptr)
12671 return 0;
12672
Willy Tarreau06d80a92017-10-19 14:32:15 +020012673 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012674 /* Socket buffer full. Let's try again later from the same point */
12675 si_applet_cant_put(si);
12676 return 0;
12677 }
12678 appctx->ctx.errors.ptr = newptr;
12679 appctx->ctx.errors.bol = newline;
12680 };
12681 next:
12682 appctx->ctx.errors.bol = 0;
12683 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012684 appctx->ctx.errors.flag ^= 1;
12685 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012686 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012687 }
12688
12689 /* dump complete */
12690 return 1;
12691}
12692
12693/* register cli keywords */
12694static struct cli_kw_list cli_kws = {{ },{
12695 { { "show", "errors", NULL },
12696 "show errors : report last request and response errors for each proxy",
12697 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12698 },
12699 {{},}
12700}};
12701
Willy Tarreau4a568972010-05-12 08:08:50 +020012702/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012703/* All supported ACL keywords must be declared here. */
12704/************************************************************************/
12705
12706/* Note: must not be declared <const> as its list will be overwritten.
12707 * Please take care of keeping this list alphabetically sorted.
12708 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012709static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012710 { "base", "base", PAT_MATCH_STR },
12711 { "base_beg", "base", PAT_MATCH_BEG },
12712 { "base_dir", "base", PAT_MATCH_DIR },
12713 { "base_dom", "base", PAT_MATCH_DOM },
12714 { "base_end", "base", PAT_MATCH_END },
12715 { "base_len", "base", PAT_MATCH_LEN },
12716 { "base_reg", "base", PAT_MATCH_REG },
12717 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012718
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012719 { "cook", "req.cook", PAT_MATCH_STR },
12720 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12721 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12722 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12723 { "cook_end", "req.cook", PAT_MATCH_END },
12724 { "cook_len", "req.cook", PAT_MATCH_LEN },
12725 { "cook_reg", "req.cook", PAT_MATCH_REG },
12726 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012727
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012728 { "hdr", "req.hdr", PAT_MATCH_STR },
12729 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12730 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12731 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12732 { "hdr_end", "req.hdr", PAT_MATCH_END },
12733 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12734 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12735 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012736
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012737 /* these two declarations uses strings with list storage (in place
12738 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12739 * and delete functions are relative to the list management. The parse
12740 * and match method are related to the corresponding fetch methods. This
12741 * is very particular ACL declaration mode.
12742 */
12743 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12744 { "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 +020012745
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012746 { "path", "path", PAT_MATCH_STR },
12747 { "path_beg", "path", PAT_MATCH_BEG },
12748 { "path_dir", "path", PAT_MATCH_DIR },
12749 { "path_dom", "path", PAT_MATCH_DOM },
12750 { "path_end", "path", PAT_MATCH_END },
12751 { "path_len", "path", PAT_MATCH_LEN },
12752 { "path_reg", "path", PAT_MATCH_REG },
12753 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012754
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012755 { "req_ver", "req.ver", PAT_MATCH_STR },
12756 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012757
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012758 { "scook", "res.cook", PAT_MATCH_STR },
12759 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12760 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12761 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12762 { "scook_end", "res.cook", PAT_MATCH_END },
12763 { "scook_len", "res.cook", PAT_MATCH_LEN },
12764 { "scook_reg", "res.cook", PAT_MATCH_REG },
12765 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012766
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012767 { "shdr", "res.hdr", PAT_MATCH_STR },
12768 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12769 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12770 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12771 { "shdr_end", "res.hdr", PAT_MATCH_END },
12772 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12773 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12774 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012775
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012776 { "url", "url", PAT_MATCH_STR },
12777 { "url_beg", "url", PAT_MATCH_BEG },
12778 { "url_dir", "url", PAT_MATCH_DIR },
12779 { "url_dom", "url", PAT_MATCH_DOM },
12780 { "url_end", "url", PAT_MATCH_END },
12781 { "url_len", "url", PAT_MATCH_LEN },
12782 { "url_reg", "url", PAT_MATCH_REG },
12783 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012784
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012785 { "urlp", "urlp", PAT_MATCH_STR },
12786 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12787 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12788 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12789 { "urlp_end", "urlp", PAT_MATCH_END },
12790 { "urlp_len", "urlp", PAT_MATCH_LEN },
12791 { "urlp_reg", "urlp", PAT_MATCH_REG },
12792 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012793
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012794 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012795}};
12796
12797/************************************************************************/
12798/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012799/************************************************************************/
12800/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012801static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012802 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012803 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012804 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12805
Willy Tarreau87b09662015-04-03 00:22:06 +020012806 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012807 { "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 +020012808
12809 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012810 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12811 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12812 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012813
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012814 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12815 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012816
Willy Tarreau409bcde2013-01-08 00:31:00 +010012817 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12818 * are only here to match the ACL's name, are request-only and are used
12819 * for ACL compatibility only.
12820 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012821 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12822 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012823 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12824 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012825
12826 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12827 * only here to match the ACL's name, are request-only and are used for
12828 * ACL compatibility only.
12829 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012830 { "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 +020012831 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012832 { "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 +020012833 { "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 +010012834
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012835 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012836 { "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 +010012837 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012838 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012839 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012840 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012841
12842 /* HTTP protocol on the request path */
12843 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012844 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012845
12846 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012847 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12848 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012849
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012850 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012851 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12852 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012853 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012854
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012855 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012856 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12857
Willy Tarreau18ed2562013-01-14 15:56:36 +010012858 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012859 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12860 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012861
Willy Tarreau18ed2562013-01-14 15:56:36 +010012862 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012863 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012864 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12865 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012866
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012867 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012868 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012869 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012870 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012871 { "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 +010012872 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012873 { "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 +010012874
12875 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012876 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012877 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12878 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012879
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012880 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012881 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012882 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012883 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012884 { "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 +010012885 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012886 { "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 +010012887
Willy Tarreau409bcde2013-01-08 00:31:00 +010012888 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012889 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012890 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12891 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012892 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012893
12894 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012895 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012896 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012897 { "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 +020012898 { "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 +010012899
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012900 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012901 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012902 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012903 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012904 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012905 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012906 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012907 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12908 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012909 { "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 +010012910 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012911}};
12912
Willy Tarreau8797c062007-05-07 00:55:35 +020012913
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012914/************************************************************************/
12915/* All supported converter keywords must be declared here. */
12916/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012917/* Note: must not be declared <const> as its list will be overwritten */
12918static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012919 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012920 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012921 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12922 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012923 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012924 { NULL, NULL, 0, 0, 0 },
12925}};
12926
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012927
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012928/************************************************************************/
12929/* All supported http-request action keywords must be declared here. */
12930/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012931struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012932 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012933 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012934 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012935 { "set-method", parse_set_req_line },
12936 { "set-path", parse_set_req_line },
12937 { "set-query", parse_set_req_line },
12938 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012939 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012940 }
12941};
12942
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012943struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012944 .kw = {
12945 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012946 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012947 { NULL, NULL }
12948 }
12949};
12950
Willy Tarreau8797c062007-05-07 00:55:35 +020012951__attribute__((constructor))
12952static void __http_protocol_init(void)
12953{
12954 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012955 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012956 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012957 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012958 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012959 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012960}
12961
12962
Willy Tarreau58f10d72006-12-04 02:26:12 +010012963/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012964 * Local variables:
12965 * c-indent-level: 8
12966 * c-basic-offset: 8
12967 * End:
12968 */